@Test public void throwsIfAttemptingToPutTwice() { coordinationStore.putUnlessValueExists(SEQUENCE_NUMBER_1, VALUE_1); assertThatThrownBy(() -> coordinationStore.putUnlessValueExists(SEQUENCE_NUMBER_1, VALUE_2)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("The coordination store failed a putUnlessExists. This is unexpected" + " as it implies timestamps may have been reused, or a writer to the store behaved badly."); }
@Test public void throwsIfAttemptingToPutAtNegativeSequenceNumber() { assertThatThrownBy(() -> coordinationStore.putUnlessValueExists(-1, VALUE_1)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Only positive sequence numbers are supported"); }
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") // Passed from other operations returning Optional private SequenceAndBound determineNewSequenceAndBound( Optional<SequenceAndBound> coordinationValue, ValueAndBound<T> extantValueAndBound, T targetValue) { long sequenceNumber; long newBound; if (shouldReuseExtantValue(coordinationValue, extantValueAndBound.value(), targetValue)) { // Safe as we're only on this branch if the value is present sequenceNumber = coordinationValue.get().sequence(); newBound = getNewBound(sequenceNumberSupplier.getAsLong()); } else { sequenceNumber = sequenceNumberSupplier.getAsLong(); putUnlessValueExists(sequenceNumber, targetValue); newBound = getNewBound(sequenceNumber); } return SequenceAndBound.of(sequenceNumber, newBound); }
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") // Passed from other operations returning Optional private SequenceAndBound determineNewSequenceAndBound( Optional<SequenceAndBound> coordinationValue, ValueAndBound<T> extantValueAndBound, T targetValue) { long sequenceNumber; long newBound; if (shouldReuseExtantValue(coordinationValue, extantValueAndBound.value(), targetValue)) { // Safe as we're only on this branch if the value is present sequenceNumber = coordinationValue.get().sequence(); newBound = getNewBound(sequenceNumberSupplier.getAsLong()); } else { sequenceNumber = sequenceNumberSupplier.getAsLong(); putUnlessValueExists(sequenceNumber, targetValue); newBound = getNewBound(sequenceNumber); } return SequenceAndBound.of(sequenceNumber, newBound); }