You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by ad...@apache.org on 2011/08/23 05:46:33 UTC

svn commit: r1160537 - in /mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain: ChecksumFilterTest.java CountFilter.java CountFilterTest.java EchoFilterTest.java

Author: adc
Date: Tue Aug 23 03:46:32 2011
New Revision: 1160537

URL: http://svn.apache.org/viewvc?rev=1160537&view=rev
Log:
More unit tests

Added:
    mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilterTest.java
Modified:
    mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/ChecksumFilterTest.java
    mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilter.java
    mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/EchoFilterTest.java

Modified: mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/ChecksumFilterTest.java
URL: http://svn.apache.org/viewvc/mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/ChecksumFilterTest.java?rev=1160537&r1=1160536&r2=1160537&view=diff
==============================================================================
--- mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/ChecksumFilterTest.java (original)
+++ mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/ChecksumFilterTest.java Tue Aug 23 03:46:32 2011
@@ -54,6 +54,7 @@ public class ChecksumFilterTest
         filter.init();
         filter.send("TEST");
         filter.receive(encode("REPLY"));
+        filter.destroy();
 
         verify(down).send(encode("TEST"));
         verify(up).receive("REPLY");

Modified: mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilter.java
URL: http://svn.apache.org/viewvc/mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilter.java?rev=1160537&r1=1160536&r2=1160537&view=diff
==============================================================================
--- mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilter.java (original)
+++ mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilter.java Tue Aug 23 03:46:32 2011
@@ -43,6 +43,7 @@ public class CountFilter implements IoFi
     static final Logger LOG = LoggerFactory.getLogger(CountFilter.class);
     @IoParent protected IoUp<String> parent;
     @IoChild protected IoDown<String> child;
+    @IoChild protected int delay;
     @IoProperty(scope = IoPropertyScope.SESSION) protected ScheduledExecutorService pool;
     @IoProperty protected ScheduledFuture<Void> handle;
     @IoProperty protected int count;
@@ -67,6 +68,24 @@ public class CountFilter implements IoFi
         this.child = child;
     }
 
+    /**
+     * Delay between calls to count listeners in milliseconds.
+     * @return the delay between calls to count listeners in milliseconds
+     */
+    public int getDelay()
+    {
+        return delay;
+    }
+
+    /**
+     * Set delay between calls to count listeners in milliseconds.
+     * @param delay the delay between calls to count listeners in milliseconds
+     */
+    public void setDelay(int delay)
+    {
+        this.delay = delay;
+    }
+
     @Override
     @SuppressWarnings({"unchecked"})
     public void init() throws Exception
@@ -84,7 +103,7 @@ public class CountFilter implements IoFi
             {
                 for (CountListener listener : listeners) listener.current(count);
             }
-        }, 5, 5, TimeUnit.SECONDS);
+        }, delay, delay, TimeUnit.MILLISECONDS);
     }
 
     @Override

Added: mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilterTest.java
URL: http://svn.apache.org/viewvc/mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilterTest.java?rev=1160537&view=auto
==============================================================================
--- mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilterTest.java (added)
+++ mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/CountFilterTest.java Tue Aug 23 03:46:32 2011
@@ -0,0 +1,224 @@
+/**
+ * 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 com.acme.chain;
+
+import javax.annotation.processing.Filer;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import static org.apache.ahc.test.Utils.extract;
+import static org.apache.ahc.test.Utils.inject;
+import static org.junit.Assert.assertEquals;
+import org.junit.Before;
+import org.junit.Test;
+import static org.mockito.Mockito.mock;
+
+import org.apache.mina.core.api.IoDown;
+import org.apache.mina.core.api.IoProperty;
+import org.apache.mina.core.api.IoPropertyScope;
+import org.apache.mina.core.api.IoUp;
+
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class CountFilterTest
+{
+    protected static volatile int COUNT;
+    protected static volatile int MAX;
+    protected final static Set<CountListener> LISTENERS = new HashSet<CountListener>();
+    protected static final CountDownLatch         LATCH = new CountDownLatch(1);
+
+    @Test
+    public void test() throws Exception
+    {
+        CountFilter filter = new CountFilter();
+        filter.setDelay(100);
+
+        IoDown<String> down = mock(IoDown.class);
+        IoUp<String> up = mock(IoUp.class);
+        ScheduledExecutorService pool = new DecoratingScheduledExecutorService(new ScheduledThreadPoolExecutor(5));
+        inject(filter, "child", down);
+        inject(filter, "parent", up);
+        inject(filter, "pool", pool);
+
+        assert !pool.isShutdown();
+        LISTENERS.add(new CountListener()
+        {
+            @Override
+            public void current(int count)
+            {
+                if (count > MAX) MAX = count;
+            }
+        });
+
+        inject(filter, "count", COUNT);
+        filter.init();
+        COUNT = (Integer)extract(filter, "count");
+
+        inject(filter, "count", COUNT);
+        filter.send("ONE");
+        COUNT = (Integer)extract(filter, "count");
+
+        inject(filter, "count", COUNT);
+        filter.receive("TWO");
+        COUNT = (Integer)extract(filter, "count");
+
+        inject(filter, "count", COUNT);
+        filter.send("THREE");
+        COUNT = (Integer)extract(filter, "count");
+
+        inject(filter, "count", COUNT);
+        filter.receive("FOUR");
+        COUNT = (Integer)extract(filter, "count");
+
+        inject(filter, "count", COUNT);
+        filter.send("FIVE");
+        COUNT = (Integer)extract(filter, "count");
+
+        inject(filter, "count", COUNT);
+        filter.receive("SIX");
+        COUNT = (Integer)extract(filter, "count");
+
+        LATCH.await();
+
+        inject(filter, "count", COUNT);
+        filter.destroy();
+        COUNT = (Integer)extract(filter, "count");
+
+        pool.shutdown();
+
+        assertEquals(6, MAX);
+    }
+
+    @Before
+    public void setUp() throws Exception
+    {
+        MAX = Integer.MIN_VALUE;
+        LISTENERS.clear();
+    }
+
+    class RunnableDecorator implements Runnable
+    {
+        final Runnable delegate;
+
+        RunnableDecorator(Runnable delegate)
+        {
+            assert delegate != null;
+            this.delegate = delegate;
+        }
+
+        @Override
+        public void run()
+        {
+            try
+            {
+                inject(delegate, "count", COUNT);
+                inject(delegate, "listeners", LISTENERS);
+
+                delegate.run();
+
+                if (COUNT >= 6) LATCH.countDown();
+            }
+            catch (NoSuchFieldException e)
+            {
+                throw new Error();
+            }
+        }
+    }
+
+    class DecoratingScheduledExecutorService implements ScheduledExecutorService
+    {
+        final ScheduledExecutorService delegate;
+
+        DecoratingScheduledExecutorService(ScheduledExecutorService delegate)
+        {
+            assert delegate != null;
+            this.delegate = delegate;
+        }
+
+        @Override
+        public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {return delegate.schedule(new RunnableDecorator(command), delay, unit);}
+
+        @Override
+        public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {return delegate.schedule(callable, delay, unit);}
+
+        @Override
+        public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
+        {
+            return delegate.scheduleAtFixedRate(new RunnableDecorator(command), initialDelay, period, unit);
+        }
+
+        @Override
+        public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
+        {
+            return delegate.scheduleWithFixedDelay(new RunnableDecorator(command), initialDelay, delay, unit);
+        }
+
+        @Override
+        public void shutdown() {delegate.shutdown();}
+
+        @Override
+        public List<Runnable> shutdownNow() {return delegate.shutdownNow();}
+
+        @Override
+        public boolean isShutdown() {return delegate.isShutdown();}
+
+        @Override
+        public boolean isTerminated() {return delegate.isTerminated();}
+
+        @Override
+        public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {return delegate.awaitTermination(timeout, unit);}
+
+        @Override
+        public <T> Future<T> submit(Callable<T> task) {return delegate.submit(task);}
+
+        @Override
+        public <T> Future<T> submit(Runnable task, T result) {return delegate.submit(task, result);}
+
+        @Override
+        public Future<?> submit(Runnable task) {return delegate.submit(task);}
+
+        @Override
+        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {return delegate.invokeAll(tasks);}
+
+        @Override
+        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {return delegate.invokeAll(tasks, timeout, unit);}
+
+        @Override
+        public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {return delegate.invokeAny(tasks);}
+
+        @Override
+        public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {return delegate.invokeAny(tasks, timeout, unit);}
+
+        @Override
+        public void execute(Runnable command) {delegate.execute(new RunnableDecorator(command));}
+    }
+}

Modified: mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/EchoFilterTest.java
URL: http://svn.apache.org/viewvc/mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/EchoFilterTest.java?rev=1160537&r1=1160536&r2=1160537&view=diff
==============================================================================
--- mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/EchoFilterTest.java (original)
+++ mina/sandbox/adc/ahc/mina3/src/test/java/com/acme/chain/EchoFilterTest.java Tue Aug 23 03:46:32 2011
@@ -44,8 +44,10 @@ public class EchoFilterTest
         IoDown<String> down = mock(IoDown.class);
         inject(filter, "child", down);
 
+        filter.init();
         filter.send("DOWN");
         filter.receive("ECHO");
+        filter.destroy();
 
         InOrder order = inOrder(down);
         order.verify(down).send("DOWN");