CachingPartitionAllocator implements NumericPartitionAllocator<T> { @Override public List<Integer> getRelevantModuli(T object) { return ImmutableList.of(loadingCache.get(object)); } @VisibleForTesting  CachingPartitionAllocator(
            DistributingModulusGenerator modulusGenerator,
            Executor executor,
            Ticker ticker,
            Duration timeoutAfterAccess); static CachingPartitionAllocator<T> createDefault(int numModuli); @Override List<Integer> getRelevantModuli(T object);  }
@Test public void cachesResults() { List<Integer> firstResponse = allocator.getRelevantModuli(KEY); List<Integer> secondResponse = allocator.getRelevantModuli(KEY); List<Integer> thirdResponse = allocator.getRelevantModuli(KEY); verify(generator, times(1)).getAndMarkResidue(); verifyNoMoreInteractions(generator); assertThat(firstResponse).isEqualTo(secondResponse).isEqualTo(thirdResponse); }
@Test public void unmarksResidueIfClientGoesAway() { List<Integer> firstResponse = allocator.getRelevantModuli(KEY); time.addAndGet(5_000_000L); allocator.getRelevantModuli(KEY); scheduler.runUntilIdle(); verify(generator, times(2)).getAndMarkResidue(); verify(generator, times(1)).unmarkResidue(Iterables.getOnlyElement(firstResponse)); verifyNoMoreInteractions(generator); }