Java Code Examples for com.google.common.testing.FakeTicker#advance()
The following examples show how to use
com.google.common.testing.FakeTicker#advance() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DnsNameResolverTest.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
@Test public void resolve_usingCache() throws Exception { long ttl = 60; System.setProperty(DnsNameResolver.NETWORKADDRESS_CACHE_TTL_PROPERTY, Long.toString(ttl)); final List<InetAddress> answer = createAddressList(2); String name = "foo.googleapis.com"; FakeTicker fakeTicker = new FakeTicker(); DnsNameResolver resolver = newResolver(name, 81, GrpcUtil.NOOP_PROXY_DETECTOR, Stopwatch.createUnstarted(fakeTicker)); AddressResolver mockResolver = mock(AddressResolver.class); when(mockResolver.resolveAddress(Matchers.anyString())) .thenReturn(answer) .thenThrow(new AssertionError("should not reach here.")); resolver.setAddressResolver(mockResolver); resolver.start(mockListener); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener).onAddresses(resultCaptor.capture(), any(Attributes.class)); assertAnswerMatches(answer, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); // this refresh should return cached result fakeTicker.advance(ttl - 1, TimeUnit.SECONDS); resolver.refresh(); assertEquals(1, fakeExecutor.runDueTasks()); verifyNoMoreInteractions(mockListener); assertAnswerMatches(answer, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); resolver.shutdown(); verify(mockResolver).resolveAddress(Matchers.anyString()); }
Example 2
Source File: DnsNameResolverTest.java From grpc-java with Apache License 2.0 | 5 votes |
private void resolveDefaultValue() throws Exception { final List<InetAddress> answer1 = createAddressList(2); final List<InetAddress> answer2 = createAddressList(1); String name = "foo.googleapis.com"; FakeTicker fakeTicker = new FakeTicker(); DnsNameResolver resolver = newResolver(name, 81, GrpcUtil.NOOP_PROXY_DETECTOR, Stopwatch.createUnstarted(fakeTicker)); AddressResolver mockResolver = mock(AddressResolver.class); when(mockResolver.resolveAddress(anyString())).thenReturn(answer1).thenReturn(answer2); resolver.setAddressResolver(mockResolver); resolver.start(mockListener); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener).onResult(resultCaptor.capture()); assertAnswerMatches(answer1, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); fakeTicker.advance(DnsNameResolver.DEFAULT_NETWORK_CACHE_TTL_SECONDS, TimeUnit.SECONDS); resolver.refresh(); assertEquals(0, fakeExecutor.runDueTasks()); assertEquals(0, fakeClock.numPendingTasks()); verifyNoMoreInteractions(mockListener); fakeTicker.advance(1, TimeUnit.SECONDS); resolver.refresh(); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener, times(2)).onResult(resultCaptor.capture()); assertAnswerMatches(answer2, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); resolver.shutdown(); verify(mockResolver, times(2)).resolveAddress(anyString()); }
Example 3
Source File: DnsNameResolverTest.java From grpc-java with Apache License 2.0 | 5 votes |
@Test public void resolve_cacheExpired() throws Exception { long ttl = 60; System.setProperty(DnsNameResolver.NETWORKADDRESS_CACHE_TTL_PROPERTY, Long.toString(ttl)); final List<InetAddress> answer1 = createAddressList(2); final List<InetAddress> answer2 = createAddressList(1); String name = "foo.googleapis.com"; FakeTicker fakeTicker = new FakeTicker(); DnsNameResolver resolver = newResolver(name, 81, GrpcUtil.NOOP_PROXY_DETECTOR, Stopwatch.createUnstarted(fakeTicker)); AddressResolver mockResolver = mock(AddressResolver.class); when(mockResolver.resolveAddress(anyString())).thenReturn(answer1) .thenReturn(answer2); resolver.setAddressResolver(mockResolver); resolver.start(mockListener); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener).onResult(resultCaptor.capture()); assertAnswerMatches(answer1, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); fakeTicker.advance(ttl + 1, TimeUnit.SECONDS); resolver.refresh(); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener, times(2)).onResult(resultCaptor.capture()); assertAnswerMatches(answer2, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); resolver.shutdown(); verify(mockResolver, times(2)).resolveAddress(anyString()); }
Example 4
Source File: DnsNameResolverTest.java From grpc-java with Apache License 2.0 | 5 votes |
@Test public void resolve_usingCache() throws Exception { long ttl = 60; System.setProperty(DnsNameResolver.NETWORKADDRESS_CACHE_TTL_PROPERTY, Long.toString(ttl)); final List<InetAddress> answer = createAddressList(2); String name = "foo.googleapis.com"; FakeTicker fakeTicker = new FakeTicker(); DnsNameResolver resolver = newResolver(name, 81, GrpcUtil.NOOP_PROXY_DETECTOR, Stopwatch.createUnstarted(fakeTicker)); AddressResolver mockResolver = mock(AddressResolver.class); when(mockResolver.resolveAddress(anyString())) .thenReturn(answer) .thenThrow(new AssertionError("should not reach here.")); resolver.setAddressResolver(mockResolver); resolver.start(mockListener); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener).onResult(resultCaptor.capture()); assertAnswerMatches(answer, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); // this refresh should return cached result fakeTicker.advance(ttl - 1, TimeUnit.SECONDS); resolver.refresh(); assertEquals(0, fakeExecutor.runDueTasks()); assertEquals(0, fakeClock.numPendingTasks()); verifyNoMoreInteractions(mockListener); resolver.shutdown(); verify(mockResolver).resolveAddress(anyString()); }
Example 5
Source File: DnsNameResolverTest.java From grpc-java with Apache License 2.0 | 5 votes |
@Test public void resolve_cacheForever() throws Exception { System.setProperty(DnsNameResolver.NETWORKADDRESS_CACHE_TTL_PROPERTY, "-1"); final List<InetAddress> answer1 = createAddressList(2); String name = "foo.googleapis.com"; FakeTicker fakeTicker = new FakeTicker(); DnsNameResolver resolver = newResolver(name, 81, GrpcUtil.NOOP_PROXY_DETECTOR, Stopwatch.createUnstarted(fakeTicker)); AddressResolver mockResolver = mock(AddressResolver.class); when(mockResolver.resolveAddress(anyString())) .thenReturn(answer1) .thenThrow(new AssertionError("should not called twice")); resolver.setAddressResolver(mockResolver); resolver.start(mockListener); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener).onResult(resultCaptor.capture()); assertAnswerMatches(answer1, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); fakeTicker.advance(1, TimeUnit.DAYS); resolver.refresh(); assertEquals(0, fakeExecutor.runDueTasks()); assertEquals(0, fakeClock.numPendingTasks()); verifyNoMoreInteractions(mockListener); resolver.shutdown(); verify(mockResolver).resolveAddress(anyString()); }
Example 6
Source File: TestBookKeeper.java From rubix with Apache License 2.0 | 5 votes |
/** * Verify that the metric representing total cache evictions is correctly registered & incremented. * * @throws TException when file metadata cannot be fetched or refreshed. * @throws FileNotFoundException when cache directories cannot be created. */ @Test public void verifyCacheExpiryMetricIsReported() throws TException, IOException { final FakeTicker ticker = new FakeTicker(); CacheConfig.setCacheDataExpirationAfterWrite(conf, 1000); metrics = new MetricRegistry(); // Close metrics created in setUp(); we want a new one with the above configuration. bookKeeperMetrics.close(); try (BookKeeperMetrics bookKeeperMetrics = new BookKeeperMetrics(conf, metrics)) { bookKeeper = new CoordinatorBookKeeper(conf, bookKeeperMetrics, ticker); assertEquals(metrics.getCounters().get(BookKeeperMetrics.CacheMetric.CACHE_EXPIRY_COUNT.getMetricName()).getCount(), 0); CacheStatusRequest request = new CacheStatusRequest(TEST_REMOTE_PATH, TEST_FILE_LENGTH, TEST_LAST_MODIFIED, TEST_START_BLOCK, TEST_END_BLOCK) .setClusterType(ClusterType.TEST_CLUSTER_MANAGER.ordinal()) .setIncrMetrics(true); bookKeeper.getCacheStatus(request); ticker.advance(30000, TimeUnit.MILLISECONDS); bookKeeper.fileMetadataCache.cleanUp(); assertEquals(metrics.getCounters().get(BookKeeperMetrics.CacheMetric.CACHE_EXPIRY_COUNT.getMetricName()).getCount(), 1); } }
Example 7
Source File: TestBookKeeper.java From rubix with Apache License 2.0 | 5 votes |
@Test public void testGetFileInfoWithInvalidationDisabledWithCacheExpired() throws Exception { Path backendFilePath = new Path(TestUtil.getDefaultTestDirectoryPath(conf), BACKEND_FILE_NAME); DataGen.populateFile(backendFilePath.toString()); int expectedFileSize = DataGen.generateContent(1).length(); CacheConfig.setFileStalenessCheck(conf, false); CacheConfig.setStaleFileInfoExpiryPeriod(conf, 5); FakeTicker ticker = new FakeTicker(); // Close metrics created in setUp(); we want a new one with the above configuration. bookKeeperMetrics.close(); try (BookKeeperMetrics bookKeeperMetrics = new BookKeeperMetrics(conf, new MetricRegistry())) { bookKeeper = new CoordinatorBookKeeper(conf, bookKeeperMetrics, ticker); FileInfo info = bookKeeper.getFileInfo(backendFilePath.toString()); assertTrue(info.getFileSize() == expectedFileSize, "FileSize was not equal to the expected value." + " Got FileSize: " + info.getFileSize() + " Expected Value : " + expectedFileSize); //Rewrite the file with half the data DataGen.populateFile(backendFilePath.toString(), 2); info = bookKeeper.getFileInfo(backendFilePath.toString()); assertTrue(info.getFileSize() == expectedFileSize, "FileSize was not equal to the expected value." + " Got FileSize: " + info.getFileSize() + " Expected Value : " + expectedFileSize); // Advance the ticker to 5 sec ticker.advance(5, TimeUnit.SECONDS); expectedFileSize = DataGen.generateContent(2).length(); info = bookKeeper.getFileInfo(backendFilePath.toString()); assertTrue(info.getFileSize() == expectedFileSize, "FileSize was not equal to the expected value." + " Got FileSize: " + info.getFileSize() + " Expected Value : " + expectedFileSize); } }
Example 8
Source File: TestCoordinatorBookKeeper.java From rubix with Apache License 2.0 | 5 votes |
/** * Verify that the worker health status properly expires. */ @Test public void testWorkerHealthMetrics_healthStatusExpired() throws IOException { final FakeTicker ticker = new FakeTicker(); final int healthStatusExpiry = 1000; // ms CacheConfig.setValidationEnabled(conf, true); CacheConfig.setHealthStatusExpiry(conf, healthStatusExpiry); try (BookKeeperMetrics bookKeeperMetrics = new BookKeeperMetrics(conf, metrics)) { final CoordinatorBookKeeper coordinatorBookKeeper = new CoordinatorBookKeeper(conf, bookKeeperMetrics, ticker); final Gauge liveWorkerGauge = metrics.getGauges().get(BookKeeperMetrics.HealthMetric.LIVE_WORKER_GAUGE.getMetricName()); final Gauge cachingValidatedWorkerGauge = metrics.getGauges().get(BookKeeperMetrics.HealthMetric.CACHING_VALIDATED_WORKER_GAUGE.getMetricName()); final Gauge fileValidatedWorkerGauge = metrics.getGauges().get(BookKeeperMetrics.HealthMetric.FILE_VALIDATED_WORKER_GAUGE.getMetricName()); coordinatorBookKeeper.handleHeartbeat(TEST_HOSTNAME_WORKER1, TEST_STATUS_ALL_VALIDATED); coordinatorBookKeeper.handleHeartbeat(TEST_HOSTNAME_WORKER2, TEST_STATUS_ALL_VALIDATED); long workerCount = (long) liveWorkerGauge.getValue(); long cachingValidationCount = (long) cachingValidatedWorkerGauge.getValue(); long fileValidationCount = (long) fileValidatedWorkerGauge.getValue(); assertEquals(workerCount, 2, "Incorrect number of workers reporting heartbeat"); assertEquals(cachingValidationCount, 2, "Incorrect number of workers have been validated"); assertEquals(fileValidationCount, 2, "Incorrect number of workers have been validated"); ticker.advance(healthStatusExpiry, TimeUnit.MILLISECONDS); coordinatorBookKeeper.handleHeartbeat(TEST_HOSTNAME_WORKER1, TEST_STATUS_ALL_VALIDATED); workerCount = (long) liveWorkerGauge.getValue(); cachingValidationCount = (long) cachingValidatedWorkerGauge.getValue(); fileValidationCount = (long) fileValidatedWorkerGauge.getValue(); assertEquals(workerCount, 1, "Incorrect number of workers reporting heartbeat"); assertEquals(cachingValidationCount, 1, "Incorrect number of workers have been validated"); assertEquals(fileValidationCount, 1, "Incorrect number of workers have been validated"); } }
Example 9
Source File: CaffeineAsyncLoadingTest.java From promregator with Apache License 2.0 | 5 votes |
@Test public void testFailureOnAsynchronous() { FakeTicker ticker = new FakeTicker(); AsyncLoadingCache<String, Integer> subject = Caffeine.newBuilder() .expireAfterAccess(240, TimeUnit.SECONDS) .refreshAfterWrite(120, TimeUnit.SECONDS) .ticker(ticker::read) .recordStats() .buildAsync(new AsyncCacheLoaderFailureImplementation()); Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block()); ticker.advance(Duration.ofSeconds(10)); Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block()); ticker.advance(Duration.ofSeconds(250)); Mono<Integer> errorMono = Mono.fromFuture(subject.get("a")); boolean thrown = false; try { errorMono.block(); thrown = false; } catch (Throwable t) { thrown = true; } Assert.assertTrue(thrown); }
Example 10
Source File: CaffeineAsyncLoadingTest.java From promregator with Apache License 2.0 | 5 votes |
@Test public void testRefreshIsAsynchronous() throws InterruptedException { FakeTicker ticker = new FakeTicker(); AsyncLoadingCache<String, Integer> subject = Caffeine.newBuilder() .expireAfterAccess(240, TimeUnit.SECONDS) .refreshAfterWrite(120, TimeUnit.SECONDS) .ticker(ticker::read) .recordStats() .buildAsync(new AsyncCacheLoaderTimingImplementation()); log.info("Starting first request"); Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block()); log.info("Stats on cache: "+subject.synchronous().stats().toString()); ticker.advance(Duration.ofSeconds(10)); log.info("Sending second request"); Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block()); log.info("Stats on cache: "+subject.synchronous().stats().toString()); ticker.advance(Duration.ofSeconds(120)); log.info("Sending third request"); Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block()); // That's the interesting case here! Note the zero above: This means that we get old cache data (which is what we want!) log.info("Stats on cache: "+subject.synchronous().stats().toString()); ticker.advance(Duration.ofSeconds(10)); Thread.sleep(250); // wait until async loading took place log.info("Sending fourth request"); Assert.assertEquals(new Integer(1), Mono.fromFuture(subject.get("a")).block()); log.info("Stats on cache: "+subject.synchronous().stats().toString()); }
Example 11
Source File: DnsNameResolverTest.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
private void resolveDefaultValue() throws Exception { final List<InetAddress> answer1 = createAddressList(2); final List<InetAddress> answer2 = createAddressList(1); String name = "foo.googleapis.com"; FakeTicker fakeTicker = new FakeTicker(); DnsNameResolver resolver = newResolver(name, 81, GrpcUtil.NOOP_PROXY_DETECTOR, Stopwatch.createUnstarted(fakeTicker)); AddressResolver mockResolver = mock(AddressResolver.class); when(mockResolver.resolveAddress(Matchers.anyString())).thenReturn(answer1).thenReturn(answer2); resolver.setAddressResolver(mockResolver); resolver.start(mockListener); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener).onAddresses(resultCaptor.capture(), any(Attributes.class)); assertAnswerMatches(answer1, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); fakeTicker.advance(DnsNameResolver.DEFAULT_NETWORK_CACHE_TTL_SECONDS, TimeUnit.SECONDS); resolver.refresh(); assertEquals(1, fakeExecutor.runDueTasks()); verifyNoMoreInteractions(mockListener); assertAnswerMatches(answer1, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); fakeTicker.advance(1, TimeUnit.SECONDS); resolver.refresh(); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener, times(2)).onAddresses(resultCaptor.capture(), any(Attributes.class)); assertAnswerMatches(answer2, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); resolver.shutdown(); verify(mockResolver, times(2)).resolveAddress(Matchers.anyString()); }
Example 12
Source File: DnsNameResolverTest.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
@Test public void resolve_cacheExpired() throws Exception { long ttl = 60; System.setProperty(DnsNameResolver.NETWORKADDRESS_CACHE_TTL_PROPERTY, Long.toString(ttl)); final List<InetAddress> answer1 = createAddressList(2); final List<InetAddress> answer2 = createAddressList(1); String name = "foo.googleapis.com"; FakeTicker fakeTicker = new FakeTicker(); DnsNameResolver resolver = newResolver(name, 81, GrpcUtil.NOOP_PROXY_DETECTOR, Stopwatch.createUnstarted(fakeTicker)); AddressResolver mockResolver = mock(AddressResolver.class); when(mockResolver.resolveAddress(Matchers.anyString())).thenReturn(answer1).thenReturn(answer2); resolver.setAddressResolver(mockResolver); resolver.start(mockListener); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener).onAddresses(resultCaptor.capture(), any(Attributes.class)); assertAnswerMatches(answer1, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); fakeTicker.advance(ttl + 1, TimeUnit.SECONDS); resolver.refresh(); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener, times(2)).onAddresses(resultCaptor.capture(), any(Attributes.class)); assertAnswerMatches(answer2, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); resolver.shutdown(); verify(mockResolver, times(2)).resolveAddress(Matchers.anyString()); }
Example 13
Source File: DnsNameResolverTest.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
@Test public void resolve_cacheForever() throws Exception { System.setProperty(DnsNameResolver.NETWORKADDRESS_CACHE_TTL_PROPERTY, "-1"); final List<InetAddress> answer1 = createAddressList(2); String name = "foo.googleapis.com"; FakeTicker fakeTicker = new FakeTicker(); DnsNameResolver resolver = newResolver(name, 81, GrpcUtil.NOOP_PROXY_DETECTOR, Stopwatch.createUnstarted(fakeTicker)); AddressResolver mockResolver = mock(AddressResolver.class); when(mockResolver.resolveAddress(Matchers.anyString())) .thenReturn(answer1) .thenThrow(new AssertionError("should not called twice")); resolver.setAddressResolver(mockResolver); resolver.start(mockListener); assertEquals(1, fakeExecutor.runDueTasks()); verify(mockListener).onAddresses(resultCaptor.capture(), any(Attributes.class)); assertAnswerMatches(answer1, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); fakeTicker.advance(1, TimeUnit.DAYS); resolver.refresh(); assertEquals(1, fakeExecutor.runDueTasks()); verifyNoMoreInteractions(mockListener); assertAnswerMatches(answer1, 81, resultCaptor.getValue()); assertEquals(0, fakeClock.numPendingTasks()); resolver.shutdown(); verify(mockResolver).resolveAddress(Matchers.anyString()); }
Example 14
Source File: CacheRefreshTest.java From caffeine with Apache License 2.0 | 4 votes |
public void testAutoRefresh() { FakeTicker ticker = new FakeTicker(); IncrementingLoader loader = incrementingLoader(); LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder() .refreshAfterWrite(3, MILLISECONDS) .expireAfterWrite(6, MILLISECONDS) .executor(MoreExecutors.directExecutor()) .ticker(ticker::read), loader); int expectedLoads = 0; int expectedReloads = 0; for (int i = 0; i < 3; i++) { assertEquals(Integer.valueOf(i), cache.getUnchecked(i)); expectedLoads++; assertEquals(expectedLoads, loader.getLoadCount()); assertEquals(expectedReloads, loader.getReloadCount()); ticker.advance(1, MILLISECONDS); } assertEquals(Integer.valueOf(0), cache.getUnchecked(0)); assertEquals(Integer.valueOf(1), cache.getUnchecked(1)); assertEquals(Integer.valueOf(2), cache.getUnchecked(2)); assertEquals(expectedLoads, loader.getLoadCount()); assertEquals(expectedReloads, loader.getReloadCount()); // refresh 0 ticker.advance(1, MILLISECONDS); cache.getUnchecked(0); // Allow refresh to return old value while refreshing assertEquals(Integer.valueOf(1), cache.getUnchecked(0)); expectedReloads++; assertEquals(Integer.valueOf(1), cache.getUnchecked(1)); assertEquals(Integer.valueOf(2), cache.getUnchecked(2)); assertEquals(expectedLoads, loader.getLoadCount()); assertEquals(expectedReloads, loader.getReloadCount()); // write to 1 to delay its refresh cache.asMap().put(1, -1); ticker.advance(1, MILLISECONDS); assertEquals(Integer.valueOf(1), cache.getUnchecked(0)); assertEquals(Integer.valueOf(-1), cache.getUnchecked(1)); assertEquals(Integer.valueOf(2), cache.getUnchecked(2)); assertEquals(expectedLoads, loader.getLoadCount()); assertEquals(expectedReloads, loader.getReloadCount()); // refresh 2 ticker.advance(1, MILLISECONDS); cache.getUnchecked(2); // Allow refresh to return old value while refreshing assertEquals(Integer.valueOf(1), cache.getUnchecked(0)); assertEquals(Integer.valueOf(-1), cache.getUnchecked(1)); assertEquals(Integer.valueOf(3), cache.getUnchecked(2)); expectedReloads++; assertEquals(expectedLoads, loader.getLoadCount()); assertEquals(expectedReloads, loader.getReloadCount()); ticker.advance(1, MILLISECONDS); assertEquals(Integer.valueOf(1), cache.getUnchecked(0)); assertEquals(Integer.valueOf(-1), cache.getUnchecked(1)); assertEquals(Integer.valueOf(3), cache.getUnchecked(2)); assertEquals(expectedLoads, loader.getLoadCount()); assertEquals(expectedReloads, loader.getReloadCount()); // refresh 0 and 1 ticker.advance(1, MILLISECONDS); cache.getUnchecked(0); // Allow refresh to return old value while refreshing cache.getUnchecked(1); // Allow refresh to return old value while refreshing assertEquals(Integer.valueOf(2), cache.getUnchecked(0)); expectedReloads++; assertEquals(Integer.valueOf(0), cache.getUnchecked(1)); expectedReloads++; assertEquals(Integer.valueOf(3), cache.getUnchecked(2)); assertEquals(expectedLoads, loader.getLoadCount()); assertEquals(expectedReloads, loader.getReloadCount()); }
Example 15
Source File: CacheExpirationTest.java From caffeine with Apache License 2.0 | 4 votes |
private void runExpirationTest(LoadingCache<String, Integer> cache, WatchedCreatorLoader loader, FakeTicker ticker, CountingRemovalListener<String, Integer> removalListener) { for (int i = 0; i < 10; i++) { assertEquals(Integer.valueOf(VALUE_PREFIX + i), cache.getUnchecked(KEY_PREFIX + i)); } for (int i = 0; i < 10; i++) { loader.reset(); assertEquals(Integer.valueOf(VALUE_PREFIX + i), cache.getUnchecked(KEY_PREFIX + i)); assertFalse("Loader should NOT have been called @#" + i, loader.wasCalled()); } // wait for entries to expire, but don't call expireEntries ticker.advance(EXPIRING_TIME * 10, MILLISECONDS); // add a single unexpired entry cache.getUnchecked(KEY_PREFIX + 11); // collections views shouldn't expose expired entries assertEquals(1, Iterators.size(cache.asMap().entrySet().iterator())); assertEquals(1, Iterators.size(cache.asMap().keySet().iterator())); assertEquals(1, Iterators.size(cache.asMap().values().iterator())); CacheTesting.expireEntries(cache, EXPIRING_TIME, ticker); for (int i = 0; i < 11; i++) { assertFalse(cache.asMap().containsKey(KEY_PREFIX + i)); } assertEquals(11, removalListener.getCount()); for (int i = 0; i < 10; i++) { assertFalse(cache.asMap().containsKey(KEY_PREFIX + i)); loader.reset(); assertEquals(Integer.valueOf(VALUE_PREFIX + i), cache.getUnchecked(KEY_PREFIX + i)); assertTrue("Creator should have been called @#" + i, loader.wasCalled()); } // expire new values we just created CacheTesting.expireEntries(cache, EXPIRING_TIME, ticker); assertEquals("Eviction notifications must be received", 21, removalListener.getCount()); CacheTesting.expireEntries(cache, EXPIRING_TIME, ticker); // ensure that no new notifications are sent assertEquals("Eviction notifications must be received", 21, removalListener.getCount()); }
Example 16
Source File: CacheExpirationTest.java From caffeine with Apache License 2.0 | 4 votes |
public void testExpirationOrder_access() { // test lru within a single segment FakeTicker ticker = new FakeTicker(); IdentityLoader<Integer> loader = identityLoader(); LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder() .expireAfterAccess(11, MILLISECONDS) .ticker(ticker::read), loader); for (int i = 0; i < 10; i++) { cache.getUnchecked(i); ticker.advance(1, MILLISECONDS); } Set<Integer> keySet = cache.asMap().keySet(); assertThat(keySet).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // 0 expires ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9); // reorder getAll(cache, asList(0, 1, 2)); CacheTesting.drainRecencyQueues(cache); ticker.advance(2, MILLISECONDS); assertThat(keySet).containsExactly(3, 4, 5, 6, 7, 8, 9, 0, 1, 2); // 3 expires ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(4, 5, 6, 7, 8, 9, 0, 1, 2); // reorder getAll(cache, asList(5, 7, 9)); CacheTesting.drainRecencyQueues(cache); assertThat(keySet).containsExactly(4, 6, 8, 0, 1, 2, 5, 7, 9); // 4 expires ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(6, 8, 0, 1, 2, 5, 7, 9); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(6, 8, 0, 1, 2, 5, 7, 9); // 6 expires ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(8, 0, 1, 2, 5, 7, 9); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(8, 0, 1, 2, 5, 7, 9); // 8 expires ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(0, 1, 2, 5, 7, 9); }
Example 17
Source File: CacheExpirationTest.java From caffeine with Apache License 2.0 | 4 votes |
public void testExpirationOrder_write() throws ExecutionException { // test lru within a single segment FakeTicker ticker = new FakeTicker(); IdentityLoader<Integer> loader = identityLoader(); LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder() .expireAfterWrite(11, MILLISECONDS) .ticker(ticker::read), loader); for (int i = 0; i < 10; i++) { cache.getUnchecked(i); ticker.advance(1, MILLISECONDS); } Set<Integer> keySet = cache.asMap().keySet(); assertThat(keySet).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // 0 expires ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9); // get doesn't stop 1 from expiring getAll(cache, asList(0, 1, 2)); CacheTesting.drainRecencyQueues(cache); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(2, 3, 4, 5, 6, 7, 8, 9, 0); // get(K, Callable) doesn't stop 2 from expiring cache.get(2, Callables.returning(-2)); CacheTesting.drainRecencyQueues(cache); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(3, 4, 5, 6, 7, 8, 9, 0); // asMap.put saves 3 cache.asMap().put(3, -3); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(4, 5, 6, 7, 8, 9, 0, 3); // asMap.replace saves 4 cache.asMap().replace(4, -4); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(5, 6, 7, 8, 9, 0, 3, 4); // 5 expires ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(6, 7, 8, 9, 0, 3, 4); }
Example 18
Source File: CacheExpirationTest.java From caffeine with Apache License 2.0 | 4 votes |
public void testExpirationOrder_writeAccess() throws ExecutionException { // test lru within a single segment FakeTicker ticker = new FakeTicker(); IdentityLoader<Integer> loader = identityLoader(); LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder() .expireAfterWrite(5, MILLISECONDS) .expireAfterAccess(3, MILLISECONDS) .ticker(ticker::read), loader); for (int i = 0; i < 5; i++) { cache.getUnchecked(i); } ticker.advance(1, MILLISECONDS); for (int i = 5; i < 10; i++) { cache.getUnchecked(i); } ticker.advance(1, MILLISECONDS); Set<Integer> keySet = cache.asMap().keySet(); assertThat(keySet).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // get saves 1, 3; 0, 2, 4 expire getAll(cache, asList(1, 3)); CacheTesting.drainRecencyQueues(cache); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(5, 6, 7, 8, 9, 1, 3); // get saves 6, 8; 5, 7, 9 expire getAll(cache, asList(6, 8)); CacheTesting.drainRecencyQueues(cache); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(1, 3, 6, 8); // get fails to save 1, put saves 3 cache.asMap().put(3, -3); getAll(cache, asList(1)); CacheTesting.drainRecencyQueues(cache); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(6, 8, 3); // get(K, Callable) fails to save 8, replace saves 6 cache.asMap().replace(6, -6); cache.get(8, Callables.returning(-8)); CacheTesting.drainRecencyQueues(cache); ticker.advance(1, MILLISECONDS); assertThat(keySet).containsExactly(3, 6); }
Example 19
Source File: CacheExpirationTest.java From caffeine with Apache License 2.0 | 4 votes |
private void runRemovalScheduler(LoadingCache<String, Integer> cache, CountingRemovalListener<String, Integer> removalListener, WatchedCreatorLoader loader, FakeTicker ticker, String keyPrefix, long ttl) { int shift1 = 10 + VALUE_PREFIX; loader.setValuePrefix(shift1); // fill with initial data for (int i = 0; i < 10; i++) { assertEquals(Integer.valueOf(i + shift1), cache.getUnchecked(keyPrefix + i)); } //assertEquals(10, CacheTesting.expirationQueueSize(cache)); assertEquals(0, removalListener.getCount()); // wait, so that entries have just 10 ms to live ticker.advance(ttl * 2 / 3, MILLISECONDS); //assertEquals(10, CacheTesting.expirationQueueSize(cache)); assertEquals(0, removalListener.getCount()); int shift2 = shift1 + 10; loader.setValuePrefix(shift2); // fill with new data - has to live for 20 ms more for (int i = 0; i < 10; i++) { cache.invalidate(keyPrefix + i); assertEquals("key: " + keyPrefix + i, Integer.valueOf(i + shift2), cache.getUnchecked(keyPrefix + i)); } //assertEquals(10, CacheTesting.expirationQueueSize(cache)); assertEquals(10, removalListener.getCount()); // these are the invalidated ones // old timeouts must expire after this wait ticker.advance(ttl * 2 / 3, MILLISECONDS); //assertEquals(10, CacheTesting.expirationQueueSize(cache)); assertEquals(10, removalListener.getCount()); // check that new values are still there - they still have 10 ms to live for (int i = 0; i < 10; i++) { loader.reset(); assertEquals(Integer.valueOf(i + shift2), cache.getUnchecked(keyPrefix + i)); assertFalse("Creator should NOT have been called @#" + i, loader.wasCalled()); } assertEquals(10, removalListener.getCount()); }
Example 20
Source File: CacheTesting.java From caffeine with Apache License 2.0 | 4 votes |
static void expireEntries(Cache<?, ?> cache, long expiringTime, FakeTicker ticker) { checkNotNull(ticker); ticker.advance(2 * expiringTime, TimeUnit.MILLISECONDS); cache.cleanUp(); }