com.google.common.testing.FakeTicker Java Examples

The following examples show how to use com.google.common.testing.FakeTicker. 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: GitMirrorTest.java    From copybara with Apache License 2.0 6 votes vote down vote up
@Test
public void testMirror() throws Exception {
  RecordingListener recordingCallback = new RecordingListener();
  Profiler profiler = new Profiler(new FakeTicker());
  profiler.init(ImmutableList.of(recordingCallback));
  options.general.withProfiler(profiler);

  Migration mirror = createMirrorObj();
  mirror.run(workdir, ImmutableList.of());
  String orig = originRepo.git(originRepo.getGitDir(), "show-ref").getStdout();
  String dest = destRepo.git(destRepo.getGitDir(), "show-ref").getStdout();
  assertThat(dest).isEqualTo(orig);

  recordingCallback
      .assertMatchesNext(EventType.START, "//copybara")
      .assertMatchesNext(EventType.START, "//copybara/run/default")
      .assertMatchesNext(EventType.START, "//copybara/run/default/fetch")
      .assertMatchesNext(EventType.END, "//copybara/run/default/fetch")
      .assertMatchesNext(EventType.START, "//copybara/run/default/push")
      .assertMatchesNext(EventType.END, "//copybara/run/default/push")
      .assertMatchesNext(EventType.END, "//copybara/run/default");
}
 
Example #2
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
private void checkExpiration(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("Creator should not have been called @#" + i, loader.wasCalled());
  }

  CacheTesting.expireEntries(cache, EXPIRING_TIME, ticker);

  assertEquals("Map must be empty by now", 0, cache.size());
  assertEquals("Eviction notifications must be received", 10,
      removalListener.getCount());

  CacheTesting.expireEntries(cache, EXPIRING_TIME, ticker);
  // ensure that no new notifications are sent
  assertEquals("Eviction notifications must be received", 10,
      removalListener.getCount());
}
 
Example #3
Source File: DnsNameResolverTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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 #4
Source File: DnsNameResolverTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
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 #5
Source File: DnsNameResolverTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@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 #6
Source File: DnsNameResolverTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@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 #7
Source File: DnsNameResolverTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@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 #8
Source File: AbstractJCacheTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@BeforeMethod(alwaysRun = true)
public void before() {
  ticker = new FakeTicker().advance(START_TIME_MS, TimeUnit.MILLISECONDS);
  jcache = (CacheProxy<Integer, Integer>) cacheManager.createCache("jcache", getConfiguration());
  jcacheLoading = (LoadingCacheProxy<Integer, Integer>) cacheManager.createCache(
      "jcacheLoading", getLoadingConfiguration());
}
 
Example #9
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testRemovalScheduler_expireAfterBoth() {
  FakeTicker ticker = new FakeTicker();
  CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
  WatchedCreatorLoader loader = new WatchedCreatorLoader();
  LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterAccess(EXPIRING_TIME, MILLISECONDS)
      .expireAfterWrite(EXPIRING_TIME, MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener)
      .ticker(ticker::read),
      loader);
  runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME);
}
 
Example #10
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testRemovalScheduler_expireAfterAccess() {
  FakeTicker ticker = new FakeTicker();
  CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
  WatchedCreatorLoader loader = new WatchedCreatorLoader();
  LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterAccess(EXPIRING_TIME, MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener)
      .ticker(ticker::read),
      loader);
  runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME);
}
 
Example #11
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testRemovalScheduler_expireAfterWrite() {
  FakeTicker ticker = new FakeTicker();
  CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
  WatchedCreatorLoader loader = new WatchedCreatorLoader();
  LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(EXPIRING_TIME, MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener)
      .ticker(ticker::read),
      loader);
  runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME);
}
 
Example #12
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testExpiringGet_expireAfterAccess() {
  FakeTicker ticker = new FakeTicker();
  CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
  WatchedCreatorLoader loader = new WatchedCreatorLoader();
  LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterAccess(EXPIRING_TIME, MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener)
      .ticker(ticker::read),
      loader);
  runExpirationTest(cache, loader, ticker, removalListener);
}
 
Example #13
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testExpiringGet_expireAfterWrite() {
  FakeTicker ticker = new FakeTicker();
  CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
  WatchedCreatorLoader loader = new WatchedCreatorLoader();
  LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(EXPIRING_TIME, MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener)
      .ticker(ticker::read),
      loader);
  runExpirationTest(cache, loader, ticker, removalListener);
}
 
Example #14
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testExpiration_expireAfterAccess() {
  FakeTicker ticker = new FakeTicker();
  CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
  WatchedCreatorLoader loader = new WatchedCreatorLoader();
  LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterAccess(EXPIRING_TIME, MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener)
      .ticker(ticker::read),
      loader);
  checkExpiration(cache, loader, ticker, removalListener);
}
 
Example #15
Source File: DnsNameResolverTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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 #16
Source File: DnsNameResolverTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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 #17
Source File: ProfilerTest.java    From copybara with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  ticker = new FakeTicker().setAutoIncrementStep(1, TimeUnit.NANOSECONDS);
  profiler = new Profiler(ticker);
  recordingCallback = new RecordingListener();
  // We don't record anything  before start
  try (ProfilerTask ignore = profiler.start("bar")) {
    profiler.simpleTask("foo", 10, 20);
  }
  assertThat(recordingCallback.events).isEmpty();
  profiler.init(ImmutableList.of(recordingCallback));

}
 
Example #18
Source File: DnsNameResolverTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
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 #19
Source File: TestBookKeeper.java    From rubix with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #20
Source File: TestBookKeeper.java    From rubix with Apache License 2.0 5 votes vote down vote up
@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 #21
Source File: TestCoordinatorBookKeeper.java    From rubix with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #22
Source File: CaffeineAsyncLoadingTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@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 #23
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testExpiration_expireAfterWrite() {
  FakeTicker ticker = new FakeTicker();
  CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
  WatchedCreatorLoader loader = new WatchedCreatorLoader();
  LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(EXPIRING_TIME, MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener)
      .ticker(ticker::read),
      loader);
  checkExpiration(cache, loader, ticker, removalListener);
}
 
Example #24
Source File: CaffeineAsyncLoadingTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@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 #25
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 4 votes vote down vote up
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 #26
Source File: MultiwayPoolTest.java    From multiway-pool with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void beforeMethod() {
  ticker = new FakeTicker();
  lifecycle = new TestResourceLifecycle();
  multiway = makeMultiwayPool(MultiwayPoolBuilder.newBuilder());
}
 
Example #27
Source File: CacheContext.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public FakeTicker ticker() {
  return ticker;
}
 
Example #28
Source File: CacheTesting.java    From caffeine with Apache License 2.0 4 votes vote down vote up
static void expireEntries(Cache<?, ?> cache, long expiringTime, FakeTicker ticker) {
  checkNotNull(ticker);
  ticker.advance(2 * expiringTime, TimeUnit.MILLISECONDS);
  cache.cleanUp();
}
 
Example #29
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 4 votes vote down vote up
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 #30
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 4 votes vote down vote up
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);
}