InMemoryMessageIdProvider implements MessageIdProvider { @Override public int getNextMessageId(final InetSocketAddress destination) { MessageIdTracker tracker = getTracker(destination); if (tracker == null) { return Message.NONE; } else { return tracker.getNextMessageId(); } } InMemoryMessageIdProvider(final NetworkConfig config); @Override int getNextMessageId(final InetSocketAddress destination);  }
@Test public void testNullTrackerGetNextMessageIdReturnsMid() { config.set(NetworkConfig.Keys.MID_TRACKER, "NULL"); InMemoryMessageIdProvider provider = new InMemoryMessageIdProvider(config); InetSocketAddress peerAddress = getPeerAddress(1); int mid1 = provider.getNextMessageId(peerAddress); int mid2 = provider.getNextMessageId(peerAddress); assertThat(mid1, is(not(Message.NONE))); assertThat(mid2, is(not(Message.NONE))); assertThat(mid1, is(not(mid2))); for (int index = 0; index < TOTAL_NO_OF_MIDS * 2; ++index) { int mid = provider.getNextMessageId(peerAddress); assertThat(mid, is(not(Message.NONE))); } }
@Test public void testGetNextMessageIdReturnsMid() { InMemoryMessageIdProvider provider = new InMemoryMessageIdProvider(config); InetSocketAddress peerAddress = getPeerAddress(1); int mid1 = provider.getNextMessageId(peerAddress); int mid2 = provider.getNextMessageId(peerAddress); assertThat(mid1, is(not(-1))); assertThat(mid2, is(not(-1))); assertThat(mid1, is(not(mid2))); }
@Test public void testGetNextMessageIdFailsIfMaxPeersIsReached() { int MAX_PEERS = 2; config.setLong(NetworkConfig.Keys.MAX_ACTIVE_PEERS, MAX_PEERS); InMemoryMessageIdProvider provider = new InMemoryMessageIdProvider(config); addPeers(provider, MAX_PEERS); assertThat( "Should not have been able to add more peers", provider.getNextMessageId(getPeerAddress(MAX_PEERS + 1)), is(-1)); }
@Test public void testGetNextMessageIdIfMaxPeersIsReachedWithStaleEntry() throws InterruptedException { int MAX_PEERS = 2; int MAX_PEER_INACTIVITY_PERIOD = 1; config.setLong(NetworkConfig.Keys.MAX_ACTIVE_PEERS, MAX_PEERS); config.setLong(NetworkConfig.Keys.MAX_PEER_INACTIVITY_PERIOD, MAX_PEER_INACTIVITY_PERIOD); InMemoryMessageIdProvider provider = new InMemoryMessageIdProvider(config); addPeers(provider, MAX_PEERS); Thread.sleep(MAX_PEER_INACTIVITY_PERIOD * 1200); assertThat(provider.getNextMessageId(getPeerAddress(MAX_PEERS + 1)), is(not(-1))); }