@Override public CheckAndSetResult<ValueAndBound<T>> tryTransformCurrentValue(Function<ValueAndBound<T>, T> transform) { CheckAndSetResult<ValueAndBound<T>> transformResult = store.transformAgreedValue(transform); ValueAndBound<T> existingValue = Iterables.getOnlyElement(transformResult.existingValues()); accumulateCachedValue(Optional.of(existingValue)); return transformResult; }
@Test public void delegatesTransformationToStore() { when(coordinationStore.transformAgreedValue(any())).thenReturn( CheckAndSetResult.of(true, ImmutableList.of(STRING_AND_ONE_HUNDRED))); CheckAndSetResult<ValueAndBound<String>> casResult = stringCoordinationService.tryTransformCurrentValue(unused -> STRING); assertThat(casResult.successful()).isTrue(); assertThat(Iterables.getOnlyElement(casResult.existingValues())) .isEqualTo(STRING_AND_ONE_HUNDRED); }
@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 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 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); } }
@Override public CheckAndSetResult<ValueAndBound<T>> tryTransformCurrentValue(Function<ValueAndBound<T>, T> transform) { CheckAndSetResult<ValueAndBound<T>> transformResult = store.transformAgreedValue(transform); ValueAndBound<T> existingValue = Iterables.getOnlyElement(transformResult.existingValues()); accumulateCachedValue(Optional.of(existingValue)); return transformResult; }