@Test public void testShouldBlockWhenNotAtHead() throws InterruptedException { MockQueue q = new MockQueue(); final BlockingEnvelopeMap map = new MockBlockingEnvelopeMap(q); map.register(SSP, "0"); Thread t = new Thread( new Runnable() { @Override public void run() { try { // Should trigger a take() call. map.poll(FETCH, -1); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); t.setDaemon(true); t.start(); q.awaitPollTimeout(); t.join(60000); // 1000 = blocking timeout constant assertEquals(1000, q.timeout); assertFalse(t.isAlive()); }
@Test public void testShouldPollWithATimeout() throws InterruptedException { MockQueue q = new MockQueue(); // Always use the same time in this test so that we can be sure we get a // 100ms poll, rather than a 99ms poll (for example). Have to do this // because BlockingEnvelopeMap calls clock.currentTimeMillis twice, and // uses the second call to determine the actual poll time. final BlockingEnvelopeMap map = new MockBlockingEnvelopeMap( q, new Clock() { private final long NOW = System.currentTimeMillis(); public long currentTimeMillis() { return NOW; } }); map.register(SSP, "0"); Thread t = new Thread( new Runnable() { @Override public void run() { try { // Should trigger a poll(100, TimeUnit.MILLISECONDS) call. map.poll(FETCH, 100); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); t.setDaemon(true); t.start(); q.awaitPollTimeout(); t.join(60000); assertEquals(100, q.timeout); assertFalse(t.isAlive()); }