diff --git a/src/test/java/com/orbitz/consul/cache/ConsulCacheTest.java b/src/test/java/com/orbitz/consul/cache/ConsulCacheTest.java index 21a0dbd7..dd64550d 100644 --- a/src/test/java/com/orbitz/consul/cache/ConsulCacheTest.java +++ b/src/test/java/com/orbitz/consul/cache/ConsulCacheTest.java @@ -49,9 +49,11 @@ public void testDuplicateServicesDontCauseFailure() { CacheConfig cacheConfig = mock(CacheConfig.class); ClientEventHandler eventHandler = mock(ClientEventHandler.class); - final StubCallbackConsumer callbackConsumer = new StubCallbackConsumer(Collections.emptyList()); + final CountingCallsCallbackConsumer callbackConsumer = new CountingCallsCallbackConsumer(Collections.emptyList()); - final ConsulCache consulCache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig, eventHandler, new CacheDescriptor("")); + final ConsulCache consulCache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig, eventHandler, + new CacheDescriptor(""), null + ); final ConsulResponse> consulResponse = new ConsulResponse<>(response, 0, false, BigInteger.ONE, null, null); final ImmutableMap map = consulCache.convertToMap(consulResponse); assertNotNull(map); @@ -143,11 +145,11 @@ public void testListenerIsCalled() { .flags(0) .build(); final List result = Collections.singletonList(value); - final StubCallbackConsumer callbackConsumer = new StubCallbackConsumer( + final CountingCallsCallbackConsumer callbackConsumer = new CountingCallsCallbackConsumer( result); final ConsulCache cache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig, - eventHandler, new CacheDescriptor("")); + eventHandler, new CacheDescriptor(""), null); try { final StubListener listener = new StubListener(); @@ -167,6 +169,35 @@ public void testListenerIsCalled() { } } + @Test + public void testCacheStartsWithInitialIndex() { + final Function keyExtractor = Value::getKey; + final CacheConfig cacheConfig = CacheConfig.builder().build(); + ClientEventHandler eventHandler = mock(ClientEventHandler.class); + + final String key = "foo"; + final ImmutableValue value = ImmutableValue.builder() + .createIndex(1) + .modifyIndex(2) + .lockIndex(2) + .key(key) + .flags(0) + .build(); + final List result = Collections.singletonList(value); + final IndexAwareStubCallbackConsumer callbackConsumer = new IndexAwareStubCallbackConsumer(result); + + BigInteger expectedIndex = BigInteger.valueOf(23); + final ConsulCache cache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig, + eventHandler, new CacheDescriptor(""), expectedIndex + ); + try { + cache.start(); + assertEquals(expectedIndex, callbackConsumer.getIndex()); + } finally { + cache.stop(); + } + } + @Test public void testListenerThrowingExceptionIsIsolated() throws InterruptedException { final Function keyExtractor = Value::getKey; @@ -186,7 +217,7 @@ public void testListenerThrowingExceptionIsIsolated() throws InterruptedExceptio final List result = Collections.singletonList(value); try (final AsyncCallbackConsumer callbackConsumer = new AsyncCallbackConsumer(result)) { try (final ConsulCache cache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig, - eventHandler, new CacheDescriptor(""))) { + eventHandler, new CacheDescriptor(""), null)) { final StubListener goodListener = new StubListener(); final AlwaysThrowsListener badListener1 = new AlwaysThrowsListener(); @@ -232,11 +263,11 @@ public void testExceptionReceivedFromListenerWhenAlreadyStarted() { .flags(0) .build(); final List result = Collections.singletonList(value); - final StubCallbackConsumer callbackConsumer = new StubCallbackConsumer( + final CountingCallsCallbackConsumer callbackConsumer = new CountingCallsCallbackConsumer( result); try (final ConsulCache cache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig, - eventHandler, new CacheDescriptor(""))) { + eventHandler, new CacheDescriptor(""), null)) { final AlwaysThrowsListener badListener = new AlwaysThrowsListener(); diff --git a/src/test/java/com/orbitz/consul/cache/StubCallbackConsumer.java b/src/test/java/com/orbitz/consul/cache/CountingCallsCallbackConsumer.java similarity index 81% rename from src/test/java/com/orbitz/consul/cache/StubCallbackConsumer.java rename to src/test/java/com/orbitz/consul/cache/CountingCallsCallbackConsumer.java index 0e32e9ee..b5e14896 100644 --- a/src/test/java/com/orbitz/consul/cache/StubCallbackConsumer.java +++ b/src/test/java/com/orbitz/consul/cache/CountingCallsCallbackConsumer.java @@ -11,12 +11,12 @@ /** * */ -public class StubCallbackConsumer implements ConsulCache.CallbackConsumer { +public class CountingCallsCallbackConsumer implements ConsulCache.CallbackConsumer { private final List result; private int callCount; - public StubCallbackConsumer(List result) { + public CountingCallsCallbackConsumer(List result) { this.result = Collections.unmodifiableList(result); }