private Optional<ValueAndBound<T>> readLatestValueFromStore() { Optional<ValueAndBound<T>> storedValueAndBound = store.getAgreedValue(); accumulateCachedValue(storedValueAndBound); return storedValueAndBound; }
@Test public void retrieveLocalValueReturnsEmptyIfNothingCached() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(OTHER_STRING_AND_ONE_THOUSAND)); assertThat(stringCoordinationService.getLastKnownLocalValue()).isEmpty(); verify(coordinationStore, never()).getAgreedValue(); } }
@Test public void retrieveLocalValueRetrievesCachedValue() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(OTHER_STRING_AND_ONE_THOUSAND)); assertThat(stringCoordinationService.getLastKnownLocalValue()).contains(STRING_AND_ONE_HUNDRED); verify(coordinationStore, times(1)).getAgreedValue(); }
@Test public void getValueWithNoValuesStoredReturnsEmpty() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.empty()); assertThat(stringCoordinationService.getValueForTimestamp(42)).isEmpty(); }
@Test public void getValueForWhichNoRecentValueExistsReturnsEmpty() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(142)).isEmpty(); }
@Test public void canRetrieveValues() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); }
@Test public void canLookUpNewValueIfCacheOutOfDate() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); assertThat(stringCoordinationService.getValueForTimestamp(742)).isEmpty(); when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(OTHER_STRING_AND_ONE_THOUSAND)); assertThat(stringCoordinationService.getValueForTimestamp(742)).contains(OTHER_STRING_AND_ONE_THOUSAND); }
@Test public void canCacheValues() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); verify(coordinationStore, times(1)).getAgreedValue(); }
@Test public void successfulUpdateUpdatesCache() { when(coordinationStore.transformAgreedValue(any())).thenReturn( CheckAndSetResult.of(true, ImmutableList.of(STRING_AND_ONE_HUNDRED))); stringCoordinationService.tryTransformCurrentValue(unused -> STRING); stringCoordinationService.getValueForTimestamp(56); stringCoordinationService.getValueForTimestamp(88); verify(coordinationStore, never()).getAgreedValue(); }
@Test public void failedUpdateUpdatesCache() { when(coordinationStore.transformAgreedValue(any())).thenReturn( CheckAndSetResult.of(false, ImmutableList.of(STRING_AND_ONE_HUNDRED))); stringCoordinationService.tryTransformCurrentValue(unused -> STRING); stringCoordinationService.getValueForTimestamp(56); stringCoordinationService.getValueForTimestamp(88); verify(coordinationStore, never()).getAgreedValue(); }
@Test public void multipleStoresCanCoexist() { byte[] otherCoordinationKey = PtBytes.toBytes("bbbbb"); CoordinationStore<String> otherCoordinationStore = KeyValueServiceCoordinationStore.create( ObjectMappers.newServerObjectMapper(), keyValueService, otherCoordinationKey, timestampSequence::incrementAndGet, String.class, false); coordinationStore.transformAgreedValue(unused -> VALUE_1); otherCoordinationStore.transformAgreedValue(unused -> VALUE_2); assertThat(coordinationStore.getAgreedValue().get().value()).contains(VALUE_1); assertThat(otherCoordinationStore.getAgreedValue().get().value()).contains(VALUE_2); } }
private Optional<ValueAndBound<T>> readLatestValueFromStore() { Optional<ValueAndBound<T>> storedValueAndBound = store.getAgreedValue(); accumulateCachedValue(storedValueAndBound); return storedValueAndBound; }