use of io.pravega.common.util.SimpleCache in project pravega by pravega.
the class AutoScaleProcessorTest method testSteadyStateExpiry.
@Test
public void testSteadyStateExpiry() {
HashMap<String, Pair<Long, Long>> map = new HashMap<>();
HashMap<String, Long> lastAccessedTime = new HashMap<>();
List<String> evicted = new ArrayList<>();
@SuppressWarnings("unchecked") SimpleCache<String, Pair<Long, Long>> simpleCache = mock(SimpleCache.class);
AtomicLong clock = new AtomicLong(0L);
Function<Void, Void> cleanup = m -> {
for (Map.Entry<String, Long> e : lastAccessedTime.entrySet()) {
if (e.getValue() < clock.get()) {
lastAccessedTime.remove(e.getKey());
map.remove(e.getKey());
evicted.add(e.getKey());
}
}
// remove all that should have expired.
return null;
};
doAnswer(x -> {
cleanup.apply(null);
return map.get(x.getArgument(0));
}).when(simpleCache).get(anyString());
doAnswer(x -> {
cleanup.apply(null);
map.put(x.getArgument(0), x.getArgument(1));
return map.get(x.getArgument(0));
}).when(simpleCache).put(anyString(), any());
doAnswer(x -> cleanup.apply(null)).when(simpleCache).cleanUp();
AutoScalerConfig config = AutoScalerConfig.builder().with(AutoScalerConfig.CONTROLLER_URI, "tcp://localhost:9090").with(AutoScalerConfig.TLS_ENABLED, false).build();
ClientConfig objectUnderTest = AutoScaleProcessor.prepareClientConfig(config);
@Cleanup EventStreamClientFactory eventStreamClientFactory = EventStreamClientFactory.withScope(SCOPE, objectUnderTest);
@Cleanup TestAutoScaleProcessor monitor = new TestAutoScaleProcessor(AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).with(AutoScalerConfig.AUTH_ENABLED, authEnabled).with(AutoScalerConfig.CACHE_CLEANUP_IN_SECONDS, 150).with(AutoScalerConfig.CACHE_EXPIRY_IN_SECONDS, 60).build(), eventStreamClientFactory, executorService(), simpleCache);
String streamSegmentName1 = NameUtils.getQualifiedStreamSegmentName(SCOPE, STREAM1, 0L);
monitor.setTimeMillis(0L);
clock.set(0L);
monitor.notifyCreated(streamSegmentName1);
monitor.put(streamSegmentName1, new ImmutablePair<>(5L, 5L));
monitor.setTimeMillis(30 * 1000L);
clock.set(30L);
monitor.report(streamSegmentName1, 10L, 0L, 10D, 10D, 10D, 10D);
monitor.setTimeMillis(80 * 1000L);
clock.set(80L);
simpleCache.cleanUp();
assertNotNull(monitor.get(streamSegmentName1));
assertNotNull(simpleCache.get(streamSegmentName1));
assertTrue(evicted.isEmpty());
AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(null, config, executorService()), e -> e instanceof NullPointerException);
AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(null, eventStreamClientFactory, executorService()), e -> e instanceof NullPointerException);
AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).with(AutoScalerConfig.AUTH_ENABLED, authEnabled).with(AutoScalerConfig.CACHE_CLEANUP_IN_SECONDS, 150).with(AutoScalerConfig.CACHE_EXPIRY_IN_SECONDS, 60).build(), eventStreamClientFactory, null), e -> e instanceof NullPointerException);
AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).with(AutoScalerConfig.AUTH_ENABLED, authEnabled).with(AutoScalerConfig.CACHE_CLEANUP_IN_SECONDS, 150).with(AutoScalerConfig.CACHE_EXPIRY_IN_SECONDS, 60).build(), eventStreamClientFactory, null, simpleCache), e -> e instanceof NullPointerException);
AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(null, eventStreamClientFactory, executorService(), simpleCache), e -> e instanceof NullPointerException);
monitor.notifySealed(streamSegmentName1);
}
Aggregations