/* * JBoss, Home of Professional Open Source * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.test.util.test; import org.jboss.test.JBossTestCase; import org.jboss.util.timeout.TimeoutFactory; import org.jboss.util.timeout.Timeout; import org.jboss.util.timeout.TimeoutTarget; import EDU.oswego.cs.dl.util.concurrent.WaitableInt; /** * Unit tests for TimeoutFactory class. * * @author Elias Ross * @version $Revision: 1.4 $ */ public class TimeoutFactoryTestCase extends JBossTestCase { public TimeoutFactoryTestCase(String name) { super(name); } WaitableInt count = new WaitableInt(0); int sleep = 100; class TT implements TimeoutTarget, Runnable { public void timedOut(Timeout timeout) { assertTrue(timeout != null); run(); } public void run() { try { Thread.sleep(sleep); } catch (InterruptedException e) { } count.increment(); } } public void testBlocking() throws Exception { final int times = 5000; TT tt = new TT(); for (int i = 0; i < times; i++) TimeoutFactory.createTimeout(0, tt); count.whenEqual(times, null); assertEquals(times, count.get()); } public void testDefaultCtr() throws Exception { final int times = 5000; TT tt = new TT(); TimeoutFactory tf = new TimeoutFactory(); for (int i = 0; i < times; i++) { tf.schedule(0, (Runnable)tt); } count.whenEqual(times, null); assertEquals(times, count.get()); } public void testCancel() throws Exception { final int times = 100; TT tt = new TT(); TimeoutFactory tf = new TimeoutFactory(); long at = System.currentTimeMillis() + 500; for (int i = 0; i < times; i++) { Timeout t = tf.schedule(at, (TimeoutTarget)tt); t.cancel(); } Thread.sleep(1000); assertEquals(0, count.get()); } }