Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
dzharikhin committed Aug 20, 2021
1 parent 8ccb900 commit aa385b2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
45 changes: 38 additions & 7 deletions src/test/java/com/orbitz/consul/cache/ConsulCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Value> consulCache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig, eventHandler, new CacheDescriptor(""));
final ConsulCache<String, Value> consulCache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig, eventHandler,
new CacheDescriptor(""), null
);
final ConsulResponse<List<Value>> consulResponse = new ConsulResponse<>(response, 0, false, BigInteger.ONE, null, null);
final ImmutableMap<String, Value> map = consulCache.convertToMap(consulResponse);
assertNotNull(map);
Expand Down Expand Up @@ -143,11 +145,11 @@ public void testListenerIsCalled() {
.flags(0)
.build();
final List<Value> result = Collections.singletonList(value);
final StubCallbackConsumer callbackConsumer = new StubCallbackConsumer(
final CountingCallsCallbackConsumer callbackConsumer = new CountingCallsCallbackConsumer(
result);

final ConsulCache<String, Value> cache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig,
eventHandler, new CacheDescriptor(""));
eventHandler, new CacheDescriptor(""), null);
try {
final StubListener listener = new StubListener();

Expand All @@ -167,6 +169,35 @@ public void testListenerIsCalled() {
}
}

@Test
public void testCacheStartsWithInitialIndex() {
final Function<Value, String> 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<Value> result = Collections.singletonList(value);
final IndexAwareStubCallbackConsumer callbackConsumer = new IndexAwareStubCallbackConsumer(result);

BigInteger expectedIndex = BigInteger.valueOf(23);
final ConsulCache<String, Value> 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<Value, String> keyExtractor = Value::getKey;
Expand All @@ -186,7 +217,7 @@ public void testListenerThrowingExceptionIsIsolated() throws InterruptedExceptio
final List<Value> result = Collections.singletonList(value);
try (final AsyncCallbackConsumer callbackConsumer = new AsyncCallbackConsumer(result)) {
try (final ConsulCache<String, Value> cache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig,
eventHandler, new CacheDescriptor(""))) {
eventHandler, new CacheDescriptor(""), null)) {

final StubListener goodListener = new StubListener();
final AlwaysThrowsListener badListener1 = new AlwaysThrowsListener();
Expand Down Expand Up @@ -232,11 +263,11 @@ public void testExceptionReceivedFromListenerWhenAlreadyStarted() {
.flags(0)
.build();
final List<Value> result = Collections.singletonList(value);
final StubCallbackConsumer callbackConsumer = new StubCallbackConsumer(
final CountingCallsCallbackConsumer callbackConsumer = new CountingCallsCallbackConsumer(
result);

try (final ConsulCache<String, Value> cache = new ConsulCache<>(keyExtractor, callbackConsumer, cacheConfig,
eventHandler, new CacheDescriptor(""))) {
eventHandler, new CacheDescriptor(""), null)) {

final AlwaysThrowsListener badListener = new AlwaysThrowsListener();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
/**
*
*/
public class StubCallbackConsumer implements ConsulCache.CallbackConsumer<Value> {
public class CountingCallsCallbackConsumer implements ConsulCache.CallbackConsumer<Value> {

private final List<Value> result;
private int callCount;

public StubCallbackConsumer(List<Value> result) {
public CountingCallsCallbackConsumer(List<Value> result) {
this.result = Collections.unmodifiableList(result);
}

Expand Down

0 comments on commit aa385b2

Please sign in to comment.