public ErrorTranslatorImpl(String... errorFileNames) { log.info("Start initializing {}", getClass().getSimpleName()); messageSources = asList(errorFileNames); standardLocale = Locale.getDefault(); standardMessages = retrieveByLocale(standardLocale); messagesByLocale = new ReapedMap<>(ONE_HOUR, true); log.info("Finished initializing {}", getClass().getSimpleName()); }
@SuppressWarnings("unchecked") private void setUpAccessBaseAgingExpectations() { ReferenceQueue<Integer> queue = mock(ReferenceQueue.class); map = new ReapedMap<>(1000, true, queue); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private void setUpGCExpectations(final int gcAfter) { ReferenceQueue<Integer> queue = mock(ReferenceQueue.class); map = new ReapedMap<>(10000, false, queue); final IdAwareReference ref = mock(IdAwareReference.class); when(ref.getKey()).thenReturn("three").thenReturn(null); // the gcAfter queue poll simulates a GC event and triggers deletion // on the reapable map when(queue.poll()).thenAnswer(new Answer<Reference<Integer>>() { private int times = 0; @Override public Reference<Integer> answer(InvocationOnMock invocation) { return times++ == gcAfter ? ref : null; } }); }
@Test public void testReapingWithoutGC() throws Exception { map = new ReapedMap<>(1000); populate(1, 2, 3); assertSizes(3, 0); assertExpected(1, 2, 3); map.reapable("one"); map.reapable("three"); assertSizes(1, 2); map.reapable("one"); assertSizes(1, 2); assertExpected(1, 2, 3); assertEquals(Integer.valueOf(3), map.remove("three")); assertSizes(1, 1); assertExpected(1, 2); assertNull(map.get("three")); Thread.sleep(1100); assertExpected(1); assertNull(map.get("one")); assertExpected(2); assertSizes(1, 0); populate(4, 5); map.clear(); }