@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 testShouldBlockAtLeast100Ms() throws InterruptedException {
   BlockingEnvelopeMap map = new MockBlockingEnvelopeMap();
   map.register(SSP, "0");
   long now = System.currentTimeMillis();
   map.poll(FETCH, 100);
   assertTrue(System.currentTimeMillis() - now >= 100);
 }
  @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());
  }
 @Test
 public void testEmptyMapReturnsEmptyList() throws InterruptedException {
   BlockingEnvelopeMap map = new MockBlockingEnvelopeMap();
   map.register(SSP, "0");
   map.poll(FETCH, 0);
   map.poll(FETCH, 30);
   map.setIsAtHead(SSP, true);
   map.poll(FETCH, -1);
 }
 @Test
 public void testShouldGetSomeMessages() throws InterruptedException {
   BlockingEnvelopeMap map = new MockBlockingEnvelopeMap();
   map.register(SSP, "0");
   map.put(SSP, envelope);
   List<IncomingMessageEnvelope> envelopes = map.poll(FETCH, 0);
   assertEquals(1, envelopes.size());
   map.put(SSP, envelope);
   map.put(SSP, envelope);
   envelopes = map.poll(FETCH, 0);
   assertEquals(2, envelopes.size());
 }
  @Test
  public void testShouldNotReturnMoreEnvelopesThanAllowed() throws InterruptedException {
    BlockingEnvelopeMap map = new MockBlockingEnvelopeMap();
    int maxMessages = FETCH.get(SSP);

    map.register(SSP, "0");

    for (int i = 0; i < 3 * maxMessages; ++i) {
      map.put(SSP, envelope);
    }

    assertEquals(3 * maxMessages, map.getNumMessagesInQueue(SSP));
    assertEquals(maxMessages, map.poll(FETCH, 0).size());
    assertEquals(2 * maxMessages, map.getNumMessagesInQueue(SSP));
    assertEquals(maxMessages, map.poll(FETCH, 30).size());
    assertEquals(maxMessages, map.getNumMessagesInQueue(SSP));
    assertEquals(maxMessages, map.poll(FETCH, 0).size());
    assertEquals(0, map.getNumMessagesInQueue(SSP));
    assertEquals(0, map.poll(FETCH, 0).size());
  }