diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java index 4253959ea..3188c3716 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java @@ -18,8 +18,8 @@ package org.springframework.data.redis.support.atomic; import java.io.Serializable; import java.util.Date; import java.util.concurrent.TimeUnit; -import java.util.function.DoubleUnaryOperator; import java.util.function.DoubleBinaryOperator; +import java.util.function.DoubleUnaryOperator; import org.springframework.dao.DataRetrievalFailureException; import org.springframework.data.redis.connection.DataType; @@ -145,7 +145,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO } /** - * Gets the current value. + * Get the current value. * * @return the current value. */ @@ -160,7 +160,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO } /** - * Sets to the given value. + * Set to the given value. * * @param newValue the new value. */ @@ -169,7 +169,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO } /** - * Atomically sets to the given value and returns the old value. + * Set to the given value and return the old value. * * @param newValue the new value. * @return the previous value. @@ -182,7 +182,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO } /** - * Atomically sets the value to the given updated value if the current value {@code ==} the expected value. + * Atomically set the value to the given updated value if the current value {@code ==} the expected value. * * @param expect the expected value. * @param update the new value. @@ -194,134 +194,154 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO } /** - * Atomically increments by one the current value. + * Atomically increment by one the current value. * - * @return the previous value + * @return the previous value. */ public double getAndIncrement() { return incrementAndGet() - 1.0; } /** - * Atomically decrements by one the current value. + * Atomically decrement by one the current value. * - * @return the previous value + * @return the previous value. */ public double getAndDecrement() { return decrementAndGet() + 1.0; } /** - * Atomically adds the given value to the current value. + * Atomically add the given value to current value. * - * @param delta the value to add - * @return the previous value + * @param delta the value to add. + * @return the previous value. */ public double getAndAdd(double delta) { return addAndGet(delta) - delta; } /** - * Atomically update the current value using the given update function. + * Atomically update the current value using the given {@link DoubleUnaryOperator update function}. * * @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects), - * because it will be applied several times if update attempts fail due to concurrent calls. + * because it will be applied several times if update attempts fail due to concurrent calls. Must not be + * {@literal null}. * @return the previous value. + * @since 2.2 */ public double getAndUpdate(DoubleUnaryOperator updateFunction) { + Assert.notNull(updateFunction, "Update function must not be null!"); + double previousValue, newValue; + do { previousValue = get(); newValue = updateFunction.applyAsDouble(previousValue); } while (!compareAndSet(previousValue, newValue)); + return previousValue; } /** - * Atomically update the current value using the given accumulator function. - * The new value is calculated by applying the accumulator function to the current value and the - * given `updateValue`. + * Atomically update the current value using the given {@link DoubleBinaryOperator accumulator function}. The new + * value is calculated by applying the accumulator function to the current value and the given {@code updateValue}. * * @param updateValue the value which will be passed into the accumulator function. * @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side - * effects), because it will be applied several times if update attempts fail due to - * concurrent calls. + * effects), because it will be applied several times if update attempts fail due to concurrent calls. Must + * not be {@literal null}. * @return the previous value. + * @since 2.2 */ public double getAndAccumulate(double updateValue, DoubleBinaryOperator accumulatorFunction) { + Assert.notNull(accumulatorFunction, "Accumulator function must not be null!"); + double previousValue, newValue; + do { previousValue = get(); newValue = accumulatorFunction.applyAsDouble(previousValue, updateValue); } while (!compareAndSet(previousValue, newValue)); + return previousValue; } /** - * Atomically increments by one the current value. + * Atomically increment by one the current value. * - * @return the updated value + * @return the updated value. */ public double incrementAndGet() { return operations.increment(key, 1.0); } /** - * Atomically decrements by one the current value. + * Atomically decrement by one the current value. * - * @return the updated value + * @return the updated value. */ public double decrementAndGet() { return operations.increment(key, -1.0); } /** - * Atomically adds the given value to the current value. + * Atomically add the given value to current value. * - * @param delta the value to add - * @return the updated value + * @param delta the value to add. + * @return the updated value. */ public double addAndGet(double delta) { return operations.increment(key, delta); } /** - * Atomically update the current value using the given update function. + * Atomically update the current value using the given {@link DoubleUnaryOperator update function}. * * @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects), - * because it will be applied several times if update attempts fail due to concurrent calls. + * because it will be applied several times if update attempts fail due to concurrent calls. Must not be + * {@literal null}. * @return the updated value. + * @since 2.2 */ public double updateAndGet(DoubleUnaryOperator updateFunction) { + Assert.notNull(updateFunction, "Update function must not be null!"); + double previousValue, newValue; + do { previousValue = get(); newValue = updateFunction.applyAsDouble(previousValue); } while (!compareAndSet(previousValue, newValue)); + return newValue; } /** - * Atomically update the current value using the given accumulator function. - * The new value is calculated by applying the accumulator function to the current value and the - * given `updateValue`. + * Atomically update the current value using the given {@link DoubleBinaryOperator accumulator function}. The new + * value is calculated by applying the accumulator function to the current value and the given {@code updateValue}. * * @param updateValue the value which will be passed into the accumulator function. * @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side - * effects), because it will be applied several times if update attempts fail due to - * concurrent calls. + * effects), because it will be applied several times if update attempts fail due to concurrent calls. Must + * not be {@literal null}. * @return the updated value. + * @since 2.2 */ public double accumulateAndGet(double updateValue, DoubleBinaryOperator accumulatorFunction) { + Assert.notNull(accumulatorFunction, "Accumulator function must not be null!"); + double previousValue, newValue; + do { previousValue = get(); newValue = accumulatorFunction.applyAsDouble(previousValue, updateValue); } while (!compareAndSet(previousValue, newValue)); + return newValue; } diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java index c6c37badf..ced05f04a 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java @@ -167,7 +167,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey } /** - * Set to the give value and return the old value. + * Set to the given value and return the old value. * * @param newValue the new value. * @return the previous value. @@ -180,7 +180,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey } /** - * Atomically set the value to the given updated value if the current value == the expected value. + * Atomically set the value to the given updated value if the current value {@code ==} the expected value. * * @param expect the expected value. * @param update the new value. @@ -220,40 +220,50 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey } /** - * Atomically update the current value using the given update function. + * Atomically update the current value using the given {@link IntUnaryOperator update function}. * * @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects), - * because it will be applied several times if update attempts fail due to concurrent calls. + * because it will be applied several times if update attempts fail due to concurrent calls. Must not be + * {@literal null}. * @return the previous value. + * @since 2.2 */ public int getAndUpdate(IntUnaryOperator updateFunction) { + Assert.notNull(updateFunction, "Update function must not be null!"); + int previousValue, newValue; + do { previousValue = get(); newValue = updateFunction.applyAsInt(previousValue); } while (!compareAndSet(previousValue, newValue)); + return previousValue; } /** - * Atomically update the current value using the given accumulator function. - * The new value is calculated by applying the accumulator function to the current value and the - * given `updateValue`. + * Atomically update the current value using the given {@link IntBinaryOperator accumulator function}. The new value + * is calculated by applying the accumulator function to the current value and the given {@code updateValue}. * * @param updateValue the value which will be passed into the accumulator function. * @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side - * effects), because it will be applied several times if update attempts fail due to - * concurrent calls. + * effects), because it will be applied several times if update attempts fail due to concurrent calls. Must + * not be {@literal null}. * @return the previous value. + * @since 2.2 */ public int getAndAccumulate(int updateValue, IntBinaryOperator accumulatorFunction) { + Assert.notNull(accumulatorFunction, "Accumulator function must not be null!"); + int previousValue, newValue; + do { previousValue = get(); newValue = accumulatorFunction.applyAsInt(previousValue, updateValue); } while (!compareAndSet(previousValue, newValue)); + return previousValue; } @@ -286,40 +296,50 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey } /** - * Atomically update the current value using the given update function. + * Atomically update the current value using the given {@link IntUnaryOperator update function}. * * @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects), - * because it will be applied several times if update attempts fail due to concurrent calls. + * because it will be applied several times if update attempts fail due to concurrent calls. Must not be + * {@literal null}. * @return the updated value. + * @since 2.2 */ public int updateAndGet(IntUnaryOperator updateFunction) { + Assert.notNull(updateFunction, "Update function must not be null!"); + int previousValue, newValue; + do { previousValue = get(); newValue = updateFunction.applyAsInt(previousValue); } while (!compareAndSet(previousValue, newValue)); + return newValue; } /** - * Atomically update the current value using the given accumulator function. - * The new value is calculated by applying the accumulator function to the current value and the - * given `updateValue`. + * Atomically update the current value using the given {@link IntBinaryOperator accumulator function}. The new value + * is calculated by applying the accumulator function to the current value and the given {@code updateValue}. * * @param updateValue the value which will be passed into the accumulator function. * @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side - * effects), because it will be applied several times if update attempts fail due to - * concurrent calls. + * effects), because it will be applied several times if update attempts fail due to concurrent calls. Must + * not be {@literal null}. * @return the updated value. + * @since 2.2 */ public int accumulateAndGet(int updateValue, IntBinaryOperator accumulatorFunction) { + Assert.notNull(accumulatorFunction, "Accumulator function must not be null!"); + int previousValue, newValue; + do { previousValue = get(); newValue = accumulatorFunction.applyAsInt(previousValue, updateValue); } while (!compareAndSet(previousValue, newValue)); + return newValue; } diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java index 04e0bc5b9..7af0cf100 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java @@ -150,7 +150,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Gets the current value. + * Get the current value. * * @return the current value. */ @@ -165,16 +165,16 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Sets to the given value. + * Set to the given value. * - * @param newValue the new value + * @param newValue the new value. */ public void set(long newValue) { operations.set(key, newValue); } /** - * Atomically sets to the given value and returns the old value. + * Set to the given value and return the old value. * * @param newValue the new value. * @return the previous value. @@ -187,7 +187,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Atomically sets the value to the given updated value if the current value {@code ==} the expected value. + * Atomically set the value to the given updated value if the current value {@code ==} the expected value. * * @param expect the expected value. * @param update the new value. @@ -199,7 +199,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Atomically increments by one the current value. + * Atomically increment by one the current value. * * @return the previous value. */ @@ -208,7 +208,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Atomically decrements by one the current value. + * Atomically decrement by one the current value. * * @return the previous value. */ @@ -217,7 +217,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Atomically adds the given value to the current value. + * Atomically add the given value to current value. * * @param delta the value to add. * @return the previous value. @@ -227,45 +227,53 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Atomically update the current value using the given update function. + * Atomically update the current value using the given {@link LongUnaryOperator update function}. * * @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects), - * because it will be applied several times if update attempts fail due to concurrent calls. + * because it will be applied several times if update attempts fail due to concurrent calls. * @return the previous value. + * @since 2.2 */ public long getAndUpdate(LongUnaryOperator updateFunction) { + Assert.notNull(updateFunction, "Update function must not be null!"); + long previousValue, newValue; + do { previousValue = get(); newValue = updateFunction.applyAsLong(previousValue); } while (!compareAndSet(previousValue, newValue)); + return previousValue; } /** - * Atomically update the current value using the given accumulator function. - * The new value is calculated by applying the accumulator function to the current value and the - * given `updateValue`. + * Atomically update the current value using the given {@link LongBinaryOperator accumulator function}. The new value + * is calculated by applying the accumulator function to the current value and the given {@code updateValue}. * * @param updateValue the value which will be passed into the accumulator function. * @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side - * effects), because it will be applied several times if update attempts fail due to - * concurrent calls. + * effects), because it will be applied several times if update attempts fail due to concurrent calls. * @return the previous value. + * @since 2.2 */ public long getAndAccumulate(long updateValue, LongBinaryOperator accumulatorFunction) { + Assert.notNull(accumulatorFunction, "Accumulator function must not be null!"); + long previousValue, newValue; + do { previousValue = get(); newValue = accumulatorFunction.applyAsLong(previousValue, updateValue); } while (!compareAndSet(previousValue, newValue)); + return previousValue; } /** - * Atomically increments by one the current value. + * Atomically increment by one the current value. * * @return the updated value. */ @@ -274,7 +282,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Atomically decrements by one the current value. + * Atomically decrement by one the current value. * * @return the updated value. */ @@ -283,7 +291,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Atomically adds the given value to the current value. + * Atomically add the given value to current value. * * @param delta the value to add. * @return the updated value. @@ -293,40 +301,48 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe } /** - * Atomically update the current value using the given update function. + * Atomically update the current value using the given {@link LongUnaryOperator update function}. * * @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects), - * because it will be applied several times if update attempts fail due to concurrent calls. + * because it will be applied several times if update attempts fail due to concurrent calls. * @return the updated value. + * @since 2.2 */ public long updateAndGet(LongUnaryOperator updateFunction) { + Assert.notNull(updateFunction, "Update function must not be null!"); + long previousValue, newValue; + do { previousValue = get(); newValue = updateFunction.applyAsLong(previousValue); } while (!compareAndSet(previousValue, newValue)); + return newValue; } /** - * Atomically update the current value using the given accumulator function. - * The new value is calculated by applying the accumulator function to the current value and the - * given `updateValue`. + * Atomically update the current value using the given {@link LongBinaryOperator accumulator function}. The new value + * is calculated by applying the accumulator function to the current value and the given {@code updateValue}. * * @param updateValue the value which will be passed into the accumulator function. * @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side - * effects), because it will be applied several times if update attempts fail due to - * concurrent calls. + * effects), because it will be applied several times if update attempts fail due to concurrent calls. * @return the updated value. + * @since 2.2 */ public long accumulateAndGet(long updateValue, LongBinaryOperator accumulatorFunction) { + Assert.notNull(accumulatorFunction, "Accumulator function must not be null!"); + long previousValue, newValue; + do { previousValue = get(); newValue = accumulatorFunction.applyAsLong(previousValue, updateValue); } while (!compareAndSet(previousValue, newValue)); + return newValue; } diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleTests.java index e1cea1e3a..c93932bd8 100644 --- a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleTests.java +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleTests.java @@ -21,8 +21,8 @@ import java.util.Collection; import java.util.Date; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.DoubleUnaryOperator; import java.util.function.DoubleBinaryOperator; +import java.util.function.DoubleUnaryOperator; import org.assertj.core.data.Offset; import org.junit.After; @@ -233,92 +233,174 @@ public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests { @Test // DATAREDIS-874 public void updateAndGetAppliesGivenUpdateFunctionAndReturnsUpdatedValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); double initialValue = 5.3; double expectedNewValue = 10.6; doubleCounter.set(initialValue); DoubleUnaryOperator updateFunction = input -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act double result = doubleCounter.updateAndGet(updateFunction); - // Assert assertThat(result).isEqualTo(expectedNewValue); - assertThat(doubleCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(doubleCounter.get()).isCloseTo(expectedNewValue, Offset.offset(0.001)); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void updateAndGetUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + double initialValue = 5.3; + doubleCounter.set(initialValue); + + DoubleUnaryOperator updateFunction = input -> { + + operatorHasBeenApplied.set(true); + + assertThat(input).isCloseTo(initialValue, Offset.offset(0.001)); + + return -1; + }; + + doubleCounter.updateAndGet(updateFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } @Test // DATAREDIS-874 public void getAndUpdateAppliesGivenUpdateFunctionAndReturnsOriginalValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); double initialValue = 5.3; double expectedNewValue = 10.6; doubleCounter.set(initialValue); DoubleUnaryOperator updateFunction = input -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act double result = doubleCounter.getAndUpdate(updateFunction); - // Assert assertThat(result).isEqualTo(initialValue); - assertThat(doubleCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(doubleCounter.get()).isCloseTo(expectedNewValue, Offset.offset(0.001)); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void getAndUpdateUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + double initialValue = 5.3; + doubleCounter.set(initialValue); + + DoubleUnaryOperator updateFunction = input -> { + + operatorHasBeenApplied.set(true); + + assertThat(input).isCloseTo(initialValue, Offset.offset(0.001)); + + return -1; + }; + + doubleCounter.getAndUpdate(updateFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } @Test // DATAREDIS-874 public void accumulateAndGetAppliesGivenAccumulatorFunctionAndReturnsUpdatedValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); double initialValue = 5.3; double expectedNewValue = 10.6; doubleCounter.set(initialValue); DoubleBinaryOperator accumulatorFunction = (x, y) -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act double result = doubleCounter.accumulateAndGet(15.9, accumulatorFunction); - // Assert assertThat(result).isEqualTo(expectedNewValue); - assertThat(doubleCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(doubleCounter.get()).isCloseTo(expectedNewValue, Offset.offset(0.001)); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void accumulateAndGetUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + double initialValue = 5.3; + doubleCounter.set(initialValue); + + DoubleBinaryOperator accumulatorFunction = (x, y) -> { + + operatorHasBeenApplied.set(true); + + assertThat(x).isCloseTo(initialValue, Offset.offset(0.001)); + assertThat(y).isCloseTo(15, Offset.offset(0.001)); + + return -1; + }; + + doubleCounter.accumulateAndGet(15, accumulatorFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } @Test // DATAREDIS-874 public void getAndAccumulateAppliesGivenAccumulatorFunctionAndReturnsOriginalValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); double initialValue = 5.3; double expectedNewValue = 10.6; doubleCounter.set(initialValue); DoubleBinaryOperator accumulatorFunction = (x, y) -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act double result = doubleCounter.getAndAccumulate(15.9, accumulatorFunction); - // Assert assertThat(result).isEqualTo(initialValue); - assertThat(doubleCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(doubleCounter.get()).isCloseTo(expectedNewValue, Offset.offset(0.001)); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void getAndAccumulateUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + double initialValue = 5.3; + doubleCounter.set(initialValue); + + DoubleBinaryOperator accumulatorFunction = (x, y) -> { + + operatorHasBeenApplied.set(true); + + assertThat(x).isCloseTo(initialValue, Offset.offset(0.001)); + assertThat(y).isCloseTo(15, Offset.offset(0.001)); + + return -1; + }; + + doubleCounter.getAndAccumulate(15, accumulatorFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } } diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerTests.java index 8cc1ff8e6..2982d0fa2 100644 --- a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerTests.java +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerTests.java @@ -240,92 +240,174 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests { @Test // DATAREDIS-874 public void updateAndGetAppliesGivenUpdateFunctionAndReturnsUpdatedValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); int initialValue = 5; int expectedNewValue = 10; intCounter.set(initialValue); IntUnaryOperator updateFunction = input -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act int result = intCounter.updateAndGet(updateFunction); - // Assert assertThat(result).isEqualTo(expectedNewValue); assertThat(intCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void updateAndGetUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + int initialValue = 5; + intCounter.set(initialValue); + + IntUnaryOperator updateFunction = input -> { + + operatorHasBeenApplied.set(true); + + assertThat(input).isEqualTo(initialValue); + + return -1; + }; + + intCounter.updateAndGet(updateFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } @Test // DATAREDIS-874 public void getAndUpdateAppliesGivenUpdateFunctionAndReturnsOriginalValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); int initialValue = 5; int expectedNewValue = 10; intCounter.set(initialValue); IntUnaryOperator updateFunction = input -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act int result = intCounter.getAndUpdate(updateFunction); - // Assert assertThat(result).isEqualTo(initialValue); assertThat(intCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void getAndUpdateUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + int initialValue = 5; + intCounter.set(initialValue); + + IntUnaryOperator updateFunction = input -> { + + operatorHasBeenApplied.set(true); + + assertThat(input).isEqualTo(initialValue); + + return -1; + }; + + intCounter.getAndUpdate(updateFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } @Test // DATAREDIS-874 public void accumulateAndGetAppliesGivenAccumulatorFunctionAndReturnsUpdatedValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); int initialValue = 5; int expectedNewValue = 10; intCounter.set(initialValue); IntBinaryOperator accumulatorFunction = (x, y) -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act int result = intCounter.accumulateAndGet(15, accumulatorFunction); - // Assert assertThat(result).isEqualTo(expectedNewValue); assertThat(intCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void accumulateAndGetUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + int initialValue = 5; + intCounter.set(initialValue); + + IntBinaryOperator accumulatorFunction = (x, y) -> { + + operatorHasBeenApplied.set(true); + + assertThat(x).isEqualTo(initialValue); + assertThat(y).isEqualTo(15); + + return -1; + }; + + intCounter.accumulateAndGet(15, accumulatorFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } @Test // DATAREDIS-874 public void getAndAccumulateAppliesGivenAccumulatorFunctionAndReturnsOriginalValue() { - // Arrange AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); int initialValue = 5; int expectedNewValue = 10; intCounter.set(initialValue); IntBinaryOperator accumulatorFunction = (x, y) -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act int result = intCounter.getAndAccumulate(15, accumulatorFunction); - // Assert assertThat(result).isEqualTo(initialValue); assertThat(intCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void getAndAccumulateUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + int initialValue = 5; + intCounter.set(initialValue); + + IntBinaryOperator accumulatorFunction = (x, y) -> { + + operatorHasBeenApplied.set(true); + + assertThat(x).isEqualTo(initialValue); + assertThat(y).isEqualTo(15); + + return -1; + }; + + intCounter.getAndAccumulate(15, accumulatorFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } } diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongTests.java index 8d14ff1c9..0e8ecdbf7 100644 --- a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongTests.java +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongTests.java @@ -222,92 +222,174 @@ public class RedisAtomicLongTests extends AbstractRedisAtomicsTests { @Test // DATAREDIS-874 public void updateAndGetAppliesGivenUpdateFunctionAndReturnsUpdatedValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); long initialValue = 5; long expectedNewValue = 10; longCounter.set(initialValue); LongUnaryOperator updateFunction = input -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act long result = longCounter.updateAndGet(updateFunction); - // Assert assertThat(result).isEqualTo(expectedNewValue); assertThat(longCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void updateAndGetUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + long initialValue = 5; + longCounter.set(initialValue); + + LongUnaryOperator updateFunction = input -> { + + operatorHasBeenApplied.set(true); + + assertThat(input).isEqualTo(initialValue); + + return -1; + }; + + longCounter.updateAndGet(updateFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } @Test // DATAREDIS-874 public void getAndUpdateAppliesGivenUpdateFunctionAndReturnsOriginalValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); long initialValue = 5; long expectedNewValue = 10; longCounter.set(initialValue); LongUnaryOperator updateFunction = input -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act long result = longCounter.getAndUpdate(updateFunction); - // Assert assertThat(result).isEqualTo(initialValue); assertThat(longCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void getAndUpdateUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + long initialValue = 5; + longCounter.set(initialValue); + + LongUnaryOperator updateFunction = input -> { + + operatorHasBeenApplied.set(true); + + assertThat(input).isEqualTo(initialValue); + + return -1; + }; + + longCounter.getAndUpdate(updateFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } @Test // DATAREDIS-874 public void accumulateAndGetAppliesGivenAccumulatorFunctionAndReturnsUpdatedValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); long initialValue = 5; long expectedNewValue = 10; longCounter.set(initialValue); LongBinaryOperator accumulatorFunction = (x, y) -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act long result = longCounter.accumulateAndGet(15L, accumulatorFunction); - // Assert assertThat(result).isEqualTo(expectedNewValue); assertThat(longCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void accumulateAndGetUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + long initialValue = 5; + longCounter.set(initialValue); + + LongBinaryOperator accumulatorFunction = (x, y) -> { + + operatorHasBeenApplied.set(true); + + assertThat(x).isEqualTo(initialValue); + assertThat(y).isEqualTo(15); + + return -1; + }; + + longCounter.accumulateAndGet(15L, accumulatorFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } @Test // DATAREDIS-874 public void getAndAccumulateAppliesGivenAccumulatorFunctionAndReturnsOriginalValue() { - // Arrange - AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(false); + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); long initialValue = 5; long expectedNewValue = 10; longCounter.set(initialValue); LongBinaryOperator accumulatorFunction = (x, y) -> { + operatorHasBeenApplied.set(true); + return expectedNewValue; }; - // Act long result = longCounter.getAndAccumulate(15L, accumulatorFunction); - // Assert assertThat(result).isEqualTo(initialValue); assertThat(longCounter.get()).isEqualTo(expectedNewValue); - assertThat(operatorHasBeenApplied.get()).isTrue(); + assertThat(operatorHasBeenApplied).isTrue(); + } + + @Test // DATAREDIS-874 + public void getAndAccumulateUsesCorrectArguments() { + + AtomicBoolean operatorHasBeenApplied = new AtomicBoolean(); + long initialValue = 5; + longCounter.set(initialValue); + + LongBinaryOperator accumulatorFunction = (x, y) -> { + + operatorHasBeenApplied.set(true); + + assertThat(x).isEqualTo(initialValue); + assertThat(y).isEqualTo(15); + + return -1; + }; + + longCounter.getAndAccumulate(15L, accumulatorFunction); + + assertThat(operatorHasBeenApplied).isTrue(); } }