1 29 package jegg.timer; 30 31 import jegg.impl.TestBase; 32 33 public class TimerTest extends TestBase 34 { 35 static 36 { 37 setTestClass(TimerTest.class); 38 } 39 40 private TestTimeoutListener tl = null; 41 42 class TestTimeoutListener implements TimeoutListener 43 { 44 public int timeouts = 0; 45 public long last_timeout = 0; 46 public void timeout(Timer t) 47 { 48 timeouts++; 49 last_timeout = System.currentTimeMillis(); 50 } 51 } 52 53 56 public TimerTest(String n) 57 { 58 super(n); 59 } 60 61 64 protected void setup() 65 { 66 tl = new TestTimeoutListener(); 67 } 68 69 72 protected void teardown() 73 { 74 } 76 77 public void test1() 78 { 79 assertEquals("test timer not cleared", 0, tl.timeouts); 80 long before = System.currentTimeMillis(); 81 Timer t = Timer.createSingleShotTimer(tl,1000); 82 assertNotNull("Failed to create timer",t); 83 try{Thread.sleep(2000);}catch(Throwable e){} 84 long interval = tl.last_timeout - before; 85 assertEquals("test timer not called-back", 1, tl.timeouts); 86 assertTrue("Timer fired too early: "+interval+" msec", 950 < interval); 87 } 88 } 89 | Popular Tags |