Author: jvermillard
Date: Sat Feb 18 09:23:57 2012
New Revision: 1245895

URL: http://svn.apache.org/viewvc?rev=1245895&view=rev
Log:
DIRMINA-850 idle checker unit tests (WIP)

Added:
    
mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java
   (with props)
    mina/trunk/core/src/test/java/org/apache/mina/service/
    mina/trunk/core/src/test/java/org/apache/mina/service/idlecheker/
    
mina/trunk/core/src/test/java/org/apache/mina/service/idlecheker/IndexedIdleChekerTest.java
   (with props)
Modified:
    
mina/trunk/core/src/main/java/org/apache/mina/service/idlechecker/IndexedIdleChecker.java
    
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/DefaultSocketSessionConfig.java

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/service/idlechecker/IndexedIdleChecker.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/service/idlechecker/IndexedIdleChecker.java?rev=1245895&r1=1245894&r2=1245895&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/service/idlechecker/IndexedIdleChecker.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/service/idlechecker/IndexedIdleChecker.java
 Sat Feb 18 09:23:57 2012
@@ -100,6 +100,7 @@ public class IndexedIdleChecker implemen
                 readIdleSessionIndex[index] = new HashSet<AbstractIoSession>();
             }
 
+            LOG.debug("marking session {} idle for index {}", session, index);
             readIdleSessionIndex[index].add(session);
             session.setAttribute(READ_IDLE_INDEX, index);
         }
@@ -153,15 +154,21 @@ public class IndexedIdleChecker implemen
             return 0;
         }
 
-        int startIdx = ((int) (Math.max(lastCheckTime, time - 
MAX_IDLE_TIME_IN_MS) / 1000L)) % MAX_IDLE_TIME_IN_SEC;
+        int startIdx = ((int) (Math.max(lastCheckTime, time - 
MAX_IDLE_TIME_IN_MS + 1) / 1000L)) % MAX_IDLE_TIME_IN_SEC;
         int endIdx = ((int) (time / 1000L)) % MAX_IDLE_TIME_IN_SEC;
 
-        for (int index = startIdx; index != endIdx; index = (index + 1) % 
MAX_IDLE_TIME_IN_SEC) {
+        LOG.debug("scaning from index {} to index {}", startIdx, endIdx);
+
+        int index = startIdx;
+        do {
+            LOG.debug("scanning index {}", index);
             // look at the read idle index
             counter += processIndex(readIdleSessionIndex, index, 
IdleStatus.READ_IDLE);
             counter += processIndex(writeIdleSessionIndex, index, 
IdleStatus.WRITE_IDLE);
 
-        }
+            index = (index + 1) % MAX_IDLE_TIME_IN_SEC;
+        } while (index != endIdx);
+
         // save last check time for next call
         lastCheckTime = time;
         LOG.debug("detected {} idleing sessions", counter);

Added: 
mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java?rev=1245895&view=auto
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java
 (added)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java
 Sat Feb 18 09:23:57 2012
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/**
+ * 
+ */
+package org.apache.mina.session;
+
+import org.apache.mina.api.IdleStatus;
+import org.apache.mina.api.IoSessionConfig;
+
+/**
+ * Base class for session configuration.
+ * Implement des session configuration properties commons to all the different 
transports.
+ * 
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public abstract class AbstractIoSessionConfig implements IoSessionConfig {
+
+    //=====================
+    // idle management
+    //=====================    
+
+    private long idleTimeRead = -1;
+
+    private long idleTimeWrite = -1;
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public long getIdleTimeInMillis(IdleStatus status) {
+        switch (status) {
+        case READ_IDLE:
+            return idleTimeRead;
+        case WRITE_IDLE:
+            return idleTimeWrite;
+        default:
+            throw new RuntimeException("unexpected excetion, unknown idle 
status : " + status);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setIdleTimeInMillis(IdleStatus status, long ildeTimeInMilli) {
+        switch (status) {
+        case READ_IDLE:
+            this.idleTimeRead = ildeTimeInMilli;
+            break;
+        case WRITE_IDLE:
+            this.idleTimeWrite = ildeTimeInMilli;
+            break;
+        default:
+            throw new RuntimeException("unexpected excetion, unknown idle 
status : " + status);
+        }
+    }
+
+}

Propchange: 
mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/DefaultSocketSessionConfig.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/DefaultSocketSessionConfig.java?rev=1245895&r1=1245894&r2=1245895&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/DefaultSocketSessionConfig.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/DefaultSocketSessionConfig.java
 Sat Feb 18 09:23:57 2012
@@ -18,7 +18,7 @@
  */
 package org.apache.mina.transport.tcp;
 
-import org.apache.mina.api.IdleStatus;
+import org.apache.mina.session.AbstractIoSessionConfig;
 
 /**
  * Implementation for the socket session configuration.
@@ -27,47 +27,7 @@ import org.apache.mina.api.IdleStatus;
  *
  * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
  */
-public class DefaultSocketSessionConfig implements SocketSessionConfig {
-
-    //=====================
-    // idle management
-    //=====================    
-
-    private long idleTimeRead = -1;
-
-    private long idleTimeWrite = -1;
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public long getIdleTimeInMillis(IdleStatus status) {
-        switch (status) {
-        case READ_IDLE:
-            return idleTimeRead;
-        case WRITE_IDLE:
-            return idleTimeWrite;
-        default:
-            throw new RuntimeException("unexpected excetion, unknown idle 
status : " + status);
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void setIdleTimeInMillis(IdleStatus status, long ildeTimeInMilli) {
-        switch (status) {
-        case READ_IDLE:
-            this.idleTimeRead = ildeTimeInMilli;
-            break;
-        case WRITE_IDLE:
-            this.idleTimeWrite = ildeTimeInMilli;
-            break;
-        default:
-            throw new RuntimeException("unexpected excetion, unknown idle 
status : " + status);
-        }
-    }
+public class DefaultSocketSessionConfig extends AbstractIoSessionConfig 
implements SocketSessionConfig {
 
     //=====================
     // buffers

Added: 
mina/trunk/core/src/test/java/org/apache/mina/service/idlecheker/IndexedIdleChekerTest.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/service/idlecheker/IndexedIdleChekerTest.java?rev=1245895&view=auto
==============================================================================
--- 
mina/trunk/core/src/test/java/org/apache/mina/service/idlecheker/IndexedIdleChekerTest.java
 (added)
+++ 
mina/trunk/core/src/test/java/org/apache/mina/service/idlecheker/IndexedIdleChekerTest.java
 Sat Feb 18 09:23:57 2012
@@ -0,0 +1,182 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/**
+ * 
+ */
+package org.apache.mina.service.idlecheker;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+
+import java.net.SocketAddress;
+
+import org.apache.mina.api.IdleStatus;
+import org.apache.mina.api.IoFuture;
+import org.apache.mina.api.IoService;
+import org.apache.mina.api.IoSessionConfig;
+import org.apache.mina.service.SelectorProcessor;
+import org.apache.mina.service.idlechecker.IndexedIdleChecker;
+import org.apache.mina.session.AbstractIoSession;
+import org.apache.mina.session.AbstractIoSessionConfig;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IndexedIdleChecker}.
+ * 
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class IndexedIdleChekerTest {
+
+    private IndexedIdleChecker idleChecker = new IndexedIdleChecker();
+
+    private long now = System.currentTimeMillis();
+
+    @Test
+    public void process_on_empty_index() {
+        assertEquals(0, idleChecker.processIdleSession(now));
+    }
+
+    @Test
+    public void read_event() {
+        IoService service = mock(IoService.class);
+        DummySession session = new DummySession(service);
+
+        session.getConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 1000L);
+
+        idleChecker.sessionRead(session, now);
+
+        // should be idle in 1 second
+        assertEquals(1, idleChecker.processIdleSession(now));
+        assertEquals(1, session.readIdleCount);
+        assertEquals(0, session.writeIdleCount);
+        assertEquals(0, idleChecker.processIdleSession(now + 2000));
+
+    }
+
+    @Test
+    public void write_event() {
+        IoService service = mock(IoService.class);
+        DummySession session = new DummySession(service);
+
+        session.getConfig().setIdleTimeInMillis(IdleStatus.WRITE_IDLE, 1000L);
+
+        idleChecker.sessionWritten(session, now);
+
+        // should be idle in 1 second
+        assertEquals(1, idleChecker.processIdleSession(now + 1000));
+        assertEquals(0, session.readIdleCount);
+        assertEquals(1, session.writeIdleCount);
+    }
+
+    private SelectorProcessor processor = mock(SelectorProcessor.class);
+
+    private class DummySession extends AbstractIoSession {
+
+        int readIdleCount = 0;
+
+        int writeIdleCount = 0;
+
+        private DummySession(IoService service) {
+            super(service, processor);
+        }
+
+        @Override
+        public IoFuture<Void> close(boolean immediately) {
+            return null;
+        }
+
+        IoSessionConfig config = new AbstractIoSessionConfig() {
+        };
+
+        @Override
+        public IoSessionConfig getConfig() {
+            return config;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public void processSessionIdle(IdleStatus status) {
+            if (status == IdleStatus.READ_IDLE) {
+                readIdleCount++;
+            }
+            if (status == IdleStatus.WRITE_IDLE) {
+                writeIdleCount++;
+            }
+        }
+
+        @Override
+        public SocketAddress getLocalAddress() {
+            return null;
+        }
+
+        @Override
+        public SocketAddress getRemoteAddress() {
+            return null;
+        }
+
+        @Override
+        public boolean isConnected() {
+            return false;
+        }
+
+        @Override
+        public boolean isReadSuspended() {
+            return false;
+        }
+
+        @Override
+        public boolean isWriteSuspended() {
+            return false;
+        }
+
+        @Override
+        public void resumeRead() {
+        }
+
+        @Override
+        public void resumeWrite() {
+        }
+
+        @Override
+        public void suspendRead() {
+        }
+
+        @Override
+        public void suspendWrite() {
+        }
+
+        @Override
+        public boolean isSecuring() {
+            return false;
+        }
+
+        @Override
+        public boolean isSecured() {
+            return false;
+        }
+
+        @Override
+        public boolean isClosed() {
+            // TODO Auto-generated method stub
+            return false;
+        }
+    }
+}

Propchange: 
mina/trunk/core/src/test/java/org/apache/mina/service/idlecheker/IndexedIdleChekerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to