NullMessageIdTracker implements MessageIdTracker { public int getNextMessageId() { int mid = currentMID.getAndIncrement(); int result = mid % range; if (result == (range - 1)) { currentMID.addAndGet(-range); } return min + mid; } NullMessageIdTracker(int initialMid, int minMid, int maxMid); int getNextMessageId();  }
@Test public void testGetNextMessage() { NullMessageIdTracker tracker = new NullMessageIdTracker(INITIAL_MID, 0, TOTAL_NO_OF_MIDS); for (int count = 0; count < TOTAL_NO_OF_MIDS * 16; ++count) { int mid = tracker.getNextMessageId(); assertThat(mid, is(count % TOTAL_NO_OF_MIDS)); } }
@Test public void testGetNextMessageInRange() { final int minMid = 1024; final int maxMid = 2048; final int rangeMid = maxMid - minMid; NullMessageIdTracker tracker = new NullMessageIdTracker(INITIAL_MID + minMid, minMid, maxMid); for (int count = 0; count < rangeMid * 16; ++count) { int mid = tracker.getNextMessageId(); assertThat(mid, is(inRange(minMid, maxMid))); } }
@Test public void testGetNextMessageOverflow() { NullMessageIdTracker tracker = new NullMessageIdTracker(TOTAL_NO_OF_MIDS - 1, 0, TOTAL_NO_OF_MIDS); int mid = tracker.getNextMessageId(); assertThat(mid, is(TOTAL_NO_OF_MIDS - 1)); mid = tracker.getNextMessageId(); assertThat(mid, is(0)); mid = tracker.getNextMessageId(); assertThat(mid, is(1)); }