@Test public void timeoutCancelledTest() { final long now = System.currentTimeMillis(); final AsyncCallback cb = new AsyncCallback() { @Override public void onCallback() { /*nop*/ } }; Timeout t1 = new Timeout(now + 2000, cb); assertTrue(t1.getCallback() == cb); t1.cancel(); assertTrue(t1.getCallback() != cb); assertTrue(t1.isCancelled()); }
@Test public void simpleTimeoutConstructorTest() { final AsyncCallback cb = new AsyncCallback() { @Override public void onCallback() {} }; Timeout t = new Timeout(1492, cb); assertEquals(1492, t.getTimeout()); assertEquals(cb, t.getCallback()); }
private long executeTimeouts() { // makes a defensive copy to avoid (1) CME (new timeouts are added this iteration) and (2) IO // starvation. TreeSet<Timeout> defensive = new TreeSet<Timeout>(timeouts); /*Sets.newTreeSet(timeouts);*/ Iterator<Timeout> iter = defensive.iterator(); final long now = System.currentTimeMillis(); while (iter.hasNext()) { Timeout candidate = iter.next(); if (candidate.getTimeout() > now) { break; } candidate.getCallback().onCallback(); iter.remove(); timeouts.remove(candidate); logger.debug("Timeout triggered: {}", candidate); } return timeouts.isEmpty() ? Long.MAX_VALUE : Math.max(1, timeouts.iterator().next().getTimeout() - now); }