当前位置: 首页>>代码示例>>Java>>正文


Java FakeTicker.advance方法代码示例

本文整理汇总了Java中com.google.common.testing.FakeTicker.advance方法的典型用法代码示例。如果您正苦于以下问题:Java FakeTicker.advance方法的具体用法?Java FakeTicker.advance怎么用?Java FakeTicker.advance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.common.testing.FakeTicker的用法示例。


在下文中一共展示了FakeTicker.advance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setup

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
@Before
public void setup() {
    MockitoAnnotations.initMocks(this);

    doNothing().when(mockCallback).accept(any(MockFailure.class));

    ticker = new FakeTicker();
    ticker.advance(ThreadLocalRandom.current().nextLong());
    doReturn(ticker).when(mockContext).ticker();

    final ClientActorConfig mockConfig = AccessClientUtil.newMockClientActorConfig();
    doReturn(mockConfig).when(mockContext).config();

    doReturn(mock(MessageSlicer.class)).when(mockContext).messageSlicer();

    mockActor = TestProbe.apply(actorSystem);
    mockBackendInfo = new BackendInfo(mockActor.ref(), 0, ABIVersion.current(), 5);
    mockRequest = new MockRequest(mockIdentifier, mockReplyTo);
    mockRequest2 = new MockRequest(mockIdentifier, mockReplyTo);
    mockResponse = mockRequest.toRequestFailure(mockCause);
    mockResponseEnvelope = new FailureEnvelope(mockResponse, 0, 0, 0);
    mockCookie = ThreadLocalRandom.current().nextLong();

    queue = new ConnectingClientConnection<>(mockContext, mockCookie);
}
 
开发者ID:hashsdn,项目名称:hashsdn-controller,代码行数:26,代码来源:ConnectingClientConnectionTest.java

示例2: testExpiration_invalidateAll

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testExpiration_invalidateAll() {
  FakeTicker ticker = new FakeTicker();
  QueuingRemovalListener<Integer, Integer> listener =
      TestingRemovalListeners.queuingRemovalListener();
  Cache<Integer, Integer> cache =
      CacheBuilder.newBuilder()
          .expireAfterAccess(1, TimeUnit.MINUTES)
          .removalListener(listener)
          .ticker(ticker)
          .build();
  cache.put(1, 1);
  ticker.advance(10, TimeUnit.MINUTES);
  cache.invalidateAll();

  assertThat(listener.poll().getCause()).isEqualTo(RemovalCause.EXPIRED);
}
 
开发者ID:google,项目名称:guava,代码行数:17,代码来源:CacheExpirationTest.java

示例3: testRemovalListener_expired

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testRemovalListener_expired() {
  FakeTicker ticker = new FakeTicker();
  QueuingRemovalListener<Object, Object> listener = queuingRemovalListener();
  LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()
      .concurrencyLevel(1)
      .expireAfterWrite(3, TimeUnit.NANOSECONDS)
      .ticker(ticker)
      .removalListener(listener));
  assertTrue(listener.isEmpty());

  Object one = new Object();
  Object two = new Object();
  Object three = new Object();
  Object four = new Object();
  Object five = new Object();

  map.put(one, two);
  ticker.advance(1);
  map.put(two, three);
  ticker.advance(1);
  map.put(three, four);
  assertTrue(listener.isEmpty());
  ticker.advance(1);
  map.put(four, five);
  assertNotified(listener, one, two, RemovalCause.EXPIRED);

  assertTrue(listener.isEmpty());
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:29,代码来源:LocalCacheTest.java

示例4: testExpireAfterWrite

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testExpireAfterWrite() {
  FakeTicker ticker = new FakeTicker();
  LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()
      .concurrencyLevel(1)
      .ticker(ticker)
      .expireAfterWrite(2, TimeUnit.NANOSECONDS));
  Segment<Object, Object> segment = map.segments[0];

  Object key = new Object();
  Object value = new Object();
  map.put(key, value);
  ReferenceEntry<Object, Object> entry = map.getEntry(key);
  assertTrue(map.isLive(entry, ticker.read()));

  segment.writeQueue.add(entry);
  assertSame(value, map.get(key));
  assertSame(entry, segment.writeQueue.peek());
  assertEquals(1, segment.writeQueue.size());

  segment.recordRead(entry, ticker.read());
  segment.expireEntries(ticker.read());
  assertSame(value, map.get(key));
  assertSame(entry, segment.writeQueue.peek());
  assertEquals(1, segment.writeQueue.size());

  ticker.advance(1);
  segment.recordRead(entry, ticker.read());
  segment.expireEntries(ticker.read());
  assertSame(value, map.get(key));
  assertSame(entry, segment.writeQueue.peek());
  assertEquals(1, segment.writeQueue.size());

  ticker.advance(1);
  assertNull(map.get(key));
  segment.expireEntries(ticker.read());
  assertNull(map.get(key));
  assertTrue(segment.writeQueue.isEmpty());
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:39,代码来源:LocalCacheTest.java

示例5: testExpiration_invalidateAll

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testExpiration_invalidateAll() {
  FakeTicker ticker = new FakeTicker();
  QueuingRemovalListener<Integer, Integer> listener =
      TestingRemovalListeners.queuingRemovalListener();
  Cache<Integer, Integer> cache = CacheBuilder.newBuilder()
      .expireAfterAccess(1, TimeUnit.MINUTES)
      .removalListener(listener)
      .ticker(ticker)
      .build();
  cache.put(1, 1);
  ticker.advance(10, TimeUnit.MINUTES);
  cache.invalidateAll();

  assertThat(listener.poll().getCause()).isEqualTo(RemovalCause.EXPIRED);
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:16,代码来源:CacheExpirationTest.java

示例6: testRemovalListener_expireAfterWrite

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testRemovalListener_expireAfterWrite() {
  FakeTicker ticker = new FakeTicker();
  final AtomicInteger evictionCount = new AtomicInteger();
  final AtomicInteger applyCount = new AtomicInteger();
  final AtomicInteger totalSum = new AtomicInteger();

  RemovalListener<Integer, AtomicInteger> removalListener =
      new RemovalListener<Integer, AtomicInteger>() {
        @Override
        public void onRemoval(RemovalNotification<Integer, AtomicInteger> notification) {
          if (notification.wasEvicted()) {
            evictionCount.incrementAndGet();
            totalSum.addAndGet(notification.getValue().get());
          }
        }
      };

  CacheLoader<Integer, AtomicInteger> loader = new CacheLoader<Integer, AtomicInteger>() {
    @Override public AtomicInteger load(Integer key) {
      applyCount.incrementAndGet();
      return new AtomicInteger();
    }
  };

  LoadingCache<Integer, AtomicInteger> cache = CacheBuilder.newBuilder()
      .removalListener(removalListener)
      .expireAfterWrite(10, MILLISECONDS)
      .ticker(ticker)
      .build(loader);

  // Increment 100 times
  for (int i = 0; i < 100; ++i) {
    cache.getUnchecked(10).incrementAndGet();
    ticker.advance(1, MILLISECONDS);
  }

  assertEquals(evictionCount.get() + 1, applyCount.get());
  int remaining = cache.getUnchecked(10).get();
  assertEquals(100, totalSum.get() + remaining);
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:41,代码来源:CacheExpirationTest.java

示例7: testRemovalListener_expired

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testRemovalListener_expired() {
  FakeTicker ticker = new FakeTicker();
  QueuingRemovalListener<Object, Object> listener = queuingRemovalListener();
  LocalCache<Object, Object> map =
      makeLocalCache(
          createCacheBuilder()
              .concurrencyLevel(1)
              .expireAfterWrite(3, TimeUnit.NANOSECONDS)
              .ticker(ticker)
              .removalListener(listener));
  assertTrue(listener.isEmpty());

  Object one = new Object();
  Object two = new Object();
  Object three = new Object();
  Object four = new Object();
  Object five = new Object();

  map.put(one, two);
  ticker.advance(1);
  map.put(two, three);
  ticker.advance(1);
  map.put(three, four);
  assertTrue(listener.isEmpty());
  ticker.advance(1);
  map.put(four, five);
  assertNotified(listener, one, two, RemovalCause.EXPIRED);

  assertTrue(listener.isEmpty());
}
 
开发者ID:google,项目名称:guava,代码行数:31,代码来源:LocalCacheTest.java

示例8: testExpireAfterWrite

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testExpireAfterWrite() {
  FakeTicker ticker = new FakeTicker();
  LocalCache<Object, Object> map =
      makeLocalCache(
          createCacheBuilder()
              .concurrencyLevel(1)
              .ticker(ticker)
              .expireAfterWrite(2, TimeUnit.NANOSECONDS));
  Segment<Object, Object> segment = map.segments[0];

  Object key = new Object();
  Object value = new Object();
  map.put(key, value);
  ReferenceEntry<Object, Object> entry = map.getEntry(key);
  assertTrue(map.isLive(entry, ticker.read()));

  segment.writeQueue.add(entry);
  assertSame(value, map.get(key));
  assertSame(entry, segment.writeQueue.peek());
  assertEquals(1, segment.writeQueue.size());

  segment.recordRead(entry, ticker.read());
  segment.expireEntries(ticker.read());
  assertSame(value, map.get(key));
  assertSame(entry, segment.writeQueue.peek());
  assertEquals(1, segment.writeQueue.size());

  ticker.advance(1);
  segment.recordRead(entry, ticker.read());
  segment.expireEntries(ticker.read());
  assertSame(value, map.get(key));
  assertSame(entry, segment.writeQueue.peek());
  assertEquals(1, segment.writeQueue.size());

  ticker.advance(1);
  assertNull(map.get(key));
  segment.expireEntries(ticker.read());
  assertNull(map.get(key));
  assertTrue(segment.writeQueue.isEmpty());
}
 
开发者ID:google,项目名称:guava,代码行数:41,代码来源:LocalCacheTest.java

示例9: testAutoRefresh

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testAutoRefresh() {
  FakeTicker ticker = new FakeTicker();
  IncrementingLoader loader = incrementingLoader();
  LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder()
      .refreshAfterWrite(3, MILLISECONDS)
      .expireAfterWrite(6, MILLISECONDS)
      .lenientParsing()
      .ticker(ticker)
      .build(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);
  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);
  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);
  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());
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:70,代码来源:CacheRefreshTest.java

示例10: runExpirationTest

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
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((LoadingCache<?, ?>) 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((LoadingCache<?, ?>) cache, EXPIRING_TIME, ticker);
  assertEquals("Eviction notifications must be received", 21,
      removalListener.getCount());

  CacheTesting.expireEntries((LoadingCache<?, ?>) cache, EXPIRING_TIME, ticker);
  // ensure that no new notifications are sent
  assertEquals("Eviction notifications must be received", 21,
      removalListener.getCount());
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:49,代码来源:CacheExpirationTest.java

示例11: testExpirationOrder_access

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testExpirationOrder_access() {
  // test lru within a single segment
  FakeTicker ticker = new FakeTicker();
  IdentityLoader<Integer> loader = identityLoader();
  LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder()
      .concurrencyLevel(1)
      .expireAfterAccess(11, MILLISECONDS)
      .ticker(ticker)
      .build(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);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:52,代码来源:CacheExpirationTest.java

示例12: testExpirationOrder_write

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testExpirationOrder_write() throws ExecutionException {
  // test lru within a single segment
  FakeTicker ticker = new FakeTicker();
  IdentityLoader<Integer> loader = identityLoader();
  LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder()
      .concurrencyLevel(1)
      .expireAfterWrite(11, MILLISECONDS)
      .ticker(ticker)
      .build(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);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:47,代码来源:CacheExpirationTest.java

示例13: testExpirationOrder_writeAccess

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testExpirationOrder_writeAccess() throws ExecutionException {
  // test lru within a single segment
  FakeTicker ticker = new FakeTicker();
  IdentityLoader<Integer> loader = identityLoader();
  LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder()
      .concurrencyLevel(1)
      .expireAfterWrite(5, MILLISECONDS)
      .expireAfterAccess(3, MILLISECONDS)
      .ticker(ticker)
      .build(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);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:49,代码来源:CacheExpirationTest.java

示例14: runRemovalScheduler

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
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());
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:46,代码来源:CacheExpirationTest.java

示例15: testExpireAfterAccess

import com.google.common.testing.FakeTicker; //导入方法依赖的package包/类
public void testExpireAfterAccess() {
  FakeTicker ticker = new FakeTicker();
  LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()
      .concurrencyLevel(1)
      .ticker(ticker)
      .expireAfterAccess(2, TimeUnit.NANOSECONDS));
  Segment<Object, Object> segment = map.segments[0];

  Object key = new Object();
  Object value = new Object();
  map.put(key, value);
  ReferenceEntry<Object, Object> entry = map.getEntry(key);
  assertTrue(map.isLive(entry, ticker.read()));

  segment.accessQueue.add(entry);
  assertSame(value, map.get(key));
  assertSame(entry, segment.accessQueue.peek());
  assertEquals(1, segment.accessQueue.size());

  segment.recordRead(entry, ticker.read());
  segment.expireEntries(ticker.read());
  assertTrue(map.containsKey(key));
  assertSame(entry, segment.accessQueue.peek());
  assertEquals(1, segment.accessQueue.size());

  ticker.advance(1);
  segment.recordRead(entry, ticker.read());
  segment.expireEntries(ticker.read());
  assertTrue(map.containsKey(key));
  assertSame(entry, segment.accessQueue.peek());
  assertEquals(1, segment.accessQueue.size());

  ticker.advance(1);
  segment.recordRead(entry, ticker.read());
  segment.expireEntries(ticker.read());
  assertTrue(map.containsKey(key));
  assertSame(entry, segment.accessQueue.peek());
  assertEquals(1, segment.accessQueue.size());

  ticker.advance(1);
  segment.expireEntries(ticker.read());
  assertTrue(map.containsKey(key));
  assertSame(entry, segment.accessQueue.peek());
  assertEquals(1, segment.accessQueue.size());

  ticker.advance(1);
  assertFalse(map.containsKey(key));
  assertNull(map.get(key));
  segment.expireEntries(ticker.read());
  assertFalse(map.containsKey(key));
  assertNull(map.get(key));
  assertTrue(segment.accessQueue.isEmpty());
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:54,代码来源:LocalCacheTest.java


注:本文中的com.google.common.testing.FakeTicker.advance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。