From 04334dbefb4341ca30fc07c3afc4ca09412f8129 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 27 Mar 2013 12:04:15 +0100 Subject: [PATCH] [#744] Add test which I missed to commit before --- .../netty/util/HashedWheelTimerTest.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/test/java/org/jboss/netty/util/HashedWheelTimerTest.java diff --git a/src/test/java/org/jboss/netty/util/HashedWheelTimerTest.java b/src/test/java/org/jboss/netty/util/HashedWheelTimerTest.java new file mode 100644 index 0000000000..6963089272 --- /dev/null +++ b/src/test/java/org/jboss/netty/util/HashedWheelTimerTest.java @@ -0,0 +1,105 @@ +/* + * Copyright 2013 The Netty Project + * + * The Netty Project 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.jboss.netty.util; + +import org.junit.Test; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class HashedWheelTimerTest { + + @Test + public void testScheduleTimeoutShouldNotRunBeforeDelay() throws InterruptedException { + final Timer timer = new HashedWheelTimer(); + final CountDownLatch barrier = new CountDownLatch(1); + final Timeout timeout = timer.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + fail("This should not have run"); + barrier.countDown(); + } + }, 10, TimeUnit.SECONDS); + assertFalse(barrier.await(3, TimeUnit.SECONDS)); + assertFalse("timer should not expire", timeout.isExpired()); + timer.stop(); + } + + @Test + public void testScheduleTimeoutShouldRunAfterDelay() throws InterruptedException { + final Timer timer = new HashedWheelTimer(); + final CountDownLatch barrier = new CountDownLatch(1); + final Timeout timeout = timer.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + barrier.countDown(); + } + }, 2, TimeUnit.SECONDS); + assertTrue(barrier.await(3, TimeUnit.SECONDS)); + assertTrue("timer should expire", timeout.isExpired()); + timer.stop(); + } + + @Test + public void testStopTimer() throws InterruptedException { + final Timer timerProcessed = new HashedWheelTimer(); + for (int i = 0; i < 3; i ++) { + timerProcessed.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + } + }, 1, TimeUnit.MILLISECONDS); + } + Thread.sleep(1000L); // sleep for a second + assertEquals("Number of unprocessed timeouts should be 0", 0, timerProcessed.stop().size()); + + final Timer timerUnprocessed = new HashedWheelTimer(); + for (int i = 0; i < 5; i ++) { + timerUnprocessed.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + } + }, 5, TimeUnit.SECONDS); + } + Thread.sleep(1000L); // sleep for a second + assertFalse("Number of unprocessed timeouts should be greater than 0", timerUnprocessed.stop().isEmpty()); + } + + @Test(expected = IllegalStateException.class) + public void testTimerShouldThrowExceptionAfterShutdownForNewTimeouts() throws InterruptedException { + final Timer timer = new HashedWheelTimer(); + for (int i = 0; i < 3; i ++) { + timer.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + } + }, 1, TimeUnit.MILLISECONDS); + } + + timer.stop(); + Thread.sleep(1000L); // sleep for a second + + timer.newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + fail("This should not run"); + } + }, 1, TimeUnit.SECONDS); + } +}