DATAREDIS-469 - Polishing.
Return zero (0) in getAndSet to prevent NullPointerExceptions. Remove trailing whitespace. Add tests for existing methods. Simplify tests. Original pull request: #182.
This commit is contained in:
@@ -36,10 +36,11 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* Atomic double backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
|
||||
* operations.
|
||||
*
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class RedisAtomicDouble extends Number implements Serializable, BoundKeyOperations<String> {
|
||||
|
||||
@@ -51,7 +52,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisAtomicDouble</code> instance. Uses the value existing in Redis or 0 if none is found.
|
||||
*
|
||||
*
|
||||
* @param redisCounter redis counter
|
||||
* @param factory connection factory
|
||||
*/
|
||||
@@ -61,7 +62,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisAtomicDouble</code> instance.
|
||||
*
|
||||
*
|
||||
* @param redisCounter
|
||||
* @param factory
|
||||
* @param initialValue
|
||||
@@ -96,7 +97,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisAtomicDouble</code> instance. Uses the value existing in Redis or 0 if none is found.
|
||||
*
|
||||
*
|
||||
* @param redisCounter the redis counter
|
||||
* @param template the template
|
||||
* @see #RedisAtomicDouble(String, RedisConnectionFactory, double)
|
||||
@@ -110,7 +111,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
* with appropriate {@link RedisSerializer} for the key and value. As an alternative one could use the
|
||||
* {@link #RedisAtomicDouble(String, RedisConnectionFactory, Double)} constructor which uses appropriate default
|
||||
* serializers.
|
||||
*
|
||||
*
|
||||
* @param redisCounter the redis counter
|
||||
* @param template the template
|
||||
* @param initialValue the initial value
|
||||
@@ -141,7 +142,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Gets the current value.
|
||||
*
|
||||
*
|
||||
* @return the current value
|
||||
*/
|
||||
public double get() {
|
||||
@@ -156,7 +157,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Sets to the given value.
|
||||
*
|
||||
*
|
||||
* @param newValue the new value
|
||||
*/
|
||||
public void set(double newValue) {
|
||||
@@ -165,17 +166,23 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Atomically sets to the given value and returns the old value.
|
||||
*
|
||||
*
|
||||
* @param newValue the new value
|
||||
* @return the previous value
|
||||
*/
|
||||
public double getAndSet(double newValue) {
|
||||
return operations.getAndSet(key, newValue);
|
||||
|
||||
Double value = operations.getAndSet(key, newValue);
|
||||
if (value != null) {
|
||||
return value.doubleValue();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically sets 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
|
||||
* @return true if successful. False return indicates that the actual value was not equal to the expected value.
|
||||
@@ -204,7 +211,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Atomically increments by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the previous value
|
||||
*/
|
||||
public double getAndIncrement() {
|
||||
@@ -213,7 +220,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Atomically decrements by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the previous value
|
||||
*/
|
||||
public double getAndDecrement() {
|
||||
@@ -222,7 +229,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Atomically adds the given value to the current value.
|
||||
*
|
||||
*
|
||||
* @param delta the value to add
|
||||
* @return the previous value
|
||||
*/
|
||||
@@ -232,7 +239,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Atomically increments by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the updated value
|
||||
*/
|
||||
public double incrementAndGet() {
|
||||
@@ -241,7 +248,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Atomically decrements by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the updated value
|
||||
*/
|
||||
public double decrementAndGet() {
|
||||
@@ -250,7 +257,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Atomically adds the given value to the current value.
|
||||
*
|
||||
*
|
||||
* @param delta the value to add
|
||||
* @return the updated value
|
||||
*/
|
||||
@@ -260,7 +267,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
|
||||
/**
|
||||
* Returns the String representation of the current value.
|
||||
*
|
||||
*
|
||||
* @return the String representation of the current value.
|
||||
*/
|
||||
public String toString() {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* Copyright 2011-2016 the original author or authors.
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@@ -36,11 +36,12 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* Atomic integer backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
|
||||
* operations.
|
||||
*
|
||||
*
|
||||
* @see java.util.concurrent.atomic.AtomicInteger
|
||||
* @author Costin Leau
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class RedisAtomicInteger extends Number implements Serializable, BoundKeyOperations<String> {
|
||||
|
||||
@@ -52,7 +53,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisAtomicInteger</code> instance. Uses the value existing in Redis or 0 if none is found.
|
||||
*
|
||||
*
|
||||
* @param redisCounter redis counter
|
||||
* @param factory connection factory
|
||||
*/
|
||||
@@ -62,7 +63,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisAtomicInteger</code> instance.
|
||||
*
|
||||
*
|
||||
* @param redisCounter the redis counter
|
||||
* @param factory the factory
|
||||
* @param initialValue the initial value
|
||||
@@ -73,7 +74,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisAtomicInteger</code> instance. Uses the value existing in Redis or 0 if none is found.
|
||||
*
|
||||
*
|
||||
* @param redisCounter the redis counter
|
||||
* @param template the template
|
||||
* @see #RedisAtomicInteger(String, RedisConnectionFactory, int)
|
||||
@@ -87,7 +88,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
* with appropriate {@link RedisSerializer} for the key and value. As an alternative one could use the
|
||||
* {@link #RedisAtomicInteger(String, RedisConnectionFactory, Integer)} constructor which uses appropriate default
|
||||
* serializers.
|
||||
*
|
||||
*
|
||||
* @param redisCounter the redis counter
|
||||
* @param template the template
|
||||
* @param initialValue the initial value
|
||||
@@ -139,7 +140,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Get the current value.
|
||||
*
|
||||
*
|
||||
* @return the current value
|
||||
*/
|
||||
public int get() {
|
||||
@@ -154,7 +155,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Set to the given value.
|
||||
*
|
||||
*
|
||||
* @param newValue the new value
|
||||
*/
|
||||
public void set(int newValue) {
|
||||
@@ -163,17 +164,23 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Set to the give value and return the old value.
|
||||
*
|
||||
*
|
||||
* @param newValue the new value
|
||||
* @return the previous value
|
||||
*/
|
||||
public int getAndSet(int newValue) {
|
||||
return operations.getAndSet(key, newValue);
|
||||
|
||||
Integer value = operations.getAndSet(key, newValue);
|
||||
if (value != null) {
|
||||
return value.intValue();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically set the value to the given updated value if the current value <tt>==</tt> the expected value.
|
||||
*
|
||||
*
|
||||
* @param expect the expected value
|
||||
* @param update the new value
|
||||
* @return true if successful. False return indicates that the actual value was not equal to the expected value.
|
||||
@@ -202,7 +209,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Atomically increment by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the previous value
|
||||
*/
|
||||
public int getAndIncrement() {
|
||||
@@ -211,7 +218,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Atomically decrement by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the previous value
|
||||
*/
|
||||
public int getAndDecrement() {
|
||||
@@ -220,7 +227,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Atomically add the given value to current value.
|
||||
*
|
||||
*
|
||||
* @param delta the value to add
|
||||
* @return the previous value
|
||||
*/
|
||||
@@ -230,7 +237,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Atomically increment by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the updated value
|
||||
*/
|
||||
public int incrementAndGet() {
|
||||
@@ -239,7 +246,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Atomically decrement by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the updated value
|
||||
*/
|
||||
public int decrementAndGet() {
|
||||
@@ -248,7 +255,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Atomically add the given value to current value.
|
||||
*
|
||||
*
|
||||
* @param delta the value to add
|
||||
* @return the updated value
|
||||
*/
|
||||
@@ -258,7 +265,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
|
||||
/**
|
||||
* Returns the String representation of the current value.
|
||||
*
|
||||
*
|
||||
* @return the String representation of the current value.
|
||||
*/
|
||||
public String toString() {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* Copyright 2011-2016 the original author or authors.
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@@ -36,11 +36,12 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* Atomic long backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
|
||||
* operations.
|
||||
*
|
||||
*
|
||||
* @see java.util.concurrent.atomic.AtomicLong
|
||||
* @author Costin Leau
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class RedisAtomicLong extends Number implements Serializable, BoundKeyOperations<String> {
|
||||
|
||||
@@ -52,7 +53,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisAtomicLong</code> instance. Uses the value existing in Redis or 0 if none is found.
|
||||
*
|
||||
*
|
||||
* @param redisCounter redis counter
|
||||
* @param factory connection factory
|
||||
*/
|
||||
@@ -62,7 +63,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisAtomicLong</code> instance.
|
||||
*
|
||||
*
|
||||
* @param redisCounter
|
||||
* @param factory
|
||||
* @param initialValue
|
||||
@@ -97,7 +98,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Constructs a new <code>RedisAtomicLong</code> instance. Uses the value existing in Redis or 0 if none is found.
|
||||
*
|
||||
*
|
||||
* @param redisCounter the redis counter
|
||||
* @param template the template
|
||||
* @see #RedisAtomicLong(String, RedisConnectionFactory, long)
|
||||
@@ -116,7 +117,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
* As an alternative one could use the {@link #RedisAtomicLong(String, RedisConnectionFactory, Long)} constructor
|
||||
* which uses appropriate default serializers, in this case {@link StringRedisSerializer} for the key and
|
||||
* {@link GenericToStringSerializer} for the value.
|
||||
*
|
||||
*
|
||||
* @param redisCounter the redis counter
|
||||
* @param template the template
|
||||
* @param initialValue the initial value
|
||||
@@ -147,7 +148,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Gets the current value.
|
||||
*
|
||||
*
|
||||
* @return the current value
|
||||
*/
|
||||
public long get() {
|
||||
@@ -162,7 +163,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Sets to the given value.
|
||||
*
|
||||
*
|
||||
* @param newValue the new value
|
||||
*/
|
||||
public void set(long newValue) {
|
||||
@@ -171,17 +172,23 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Atomically sets to the given value and returns the old value.
|
||||
*
|
||||
*
|
||||
* @param newValue the new value
|
||||
* @return the previous value
|
||||
*/
|
||||
public long getAndSet(long newValue) {
|
||||
return operations.getAndSet(key, newValue);
|
||||
|
||||
Long value = operations.getAndSet(key, newValue);
|
||||
if (value != null) {
|
||||
return value.longValue();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically sets 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
|
||||
* @return true if successful. False return indicates that the actual value was not equal to the expected value.
|
||||
@@ -210,7 +217,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Atomically increments by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the previous value
|
||||
*/
|
||||
public long getAndIncrement() {
|
||||
@@ -219,7 +226,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Atomically decrements by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the previous value
|
||||
*/
|
||||
public long getAndDecrement() {
|
||||
@@ -228,7 +235,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Atomically adds the given value to the current value.
|
||||
*
|
||||
*
|
||||
* @param delta the value to add
|
||||
* @return the previous value
|
||||
*/
|
||||
@@ -238,7 +245,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Atomically increments by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the updated value
|
||||
*/
|
||||
public long incrementAndGet() {
|
||||
@@ -247,7 +254,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Atomically decrements by one the current value.
|
||||
*
|
||||
*
|
||||
* @return the updated value
|
||||
*/
|
||||
public long decrementAndGet() {
|
||||
@@ -256,7 +263,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Atomically adds the given value to the current value.
|
||||
*
|
||||
*
|
||||
* @param delta the value to add
|
||||
* @return the updated value
|
||||
*/
|
||||
@@ -266,7 +273,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
|
||||
/**
|
||||
* Returns the String representation of the current value.
|
||||
*
|
||||
*
|
||||
* @return the String representation of the current value.
|
||||
*/
|
||||
public String toString() {
|
||||
|
||||
@@ -42,21 +42,30 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Integration test of {@link RedisAtomicDouble}
|
||||
*
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
|
||||
|
||||
private RedisAtomicDouble doubleCounter;
|
||||
|
||||
private RedisConnectionFactory factory;
|
||||
private RedisTemplate<String, Double> template;
|
||||
|
||||
public RedisAtomicDoubleTests(RedisConnectionFactory factory) {
|
||||
doubleCounter = new RedisAtomicDouble(getClass().getSimpleName() + ":double", factory);
|
||||
|
||||
this.doubleCounter = new RedisAtomicDouble(getClass().getSimpleName() + ":double", factory);
|
||||
this.factory = factory;
|
||||
|
||||
this.template = new RedisTemplate<String, Double>();
|
||||
this.template.setConnectionFactory(factory);
|
||||
this.template.setKeySerializer(new StringRedisSerializer());
|
||||
this.template.setValueSerializer(new GenericToStringSerializer<Double>(Double.class));
|
||||
this.template.afterPropertiesSet();
|
||||
|
||||
ConnectionFactoryTracker.add(factory);
|
||||
}
|
||||
|
||||
@@ -201,12 +210,6 @@ public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
|
||||
@Test
|
||||
public void testShouldBeAbleToUseRedisAtomicDoubleWithProperlyConfiguredRedisTemplate() {
|
||||
|
||||
RedisTemplate<String, Double> template = new RedisTemplate<String, Double>();
|
||||
template.setConnectionFactory(factory);
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new GenericToStringSerializer<Double>(Double.class));
|
||||
template.afterPropertiesSet();
|
||||
|
||||
RedisAtomicDouble ral = new RedisAtomicDouble("DATAREDIS-317.atomicDouble", template);
|
||||
ral.set(32.23);
|
||||
|
||||
@@ -222,18 +225,27 @@ public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
|
||||
expectedException.expect(DataRetrievalFailureException.class);
|
||||
expectedException.expectMessage("'test' seems to no longer exist");
|
||||
|
||||
// setup long
|
||||
// setup double
|
||||
RedisAtomicDouble test = new RedisAtomicDouble("test", factory, 1);
|
||||
assertThat(test.get(), equalTo(1D)); // this passes
|
||||
|
||||
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
|
||||
template.setConnectionFactory(factory);
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
|
||||
template.afterPropertiesSet();
|
||||
|
||||
template.delete("test");
|
||||
|
||||
test.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void getAndSetReturnsZeroWhenKeyHasBeenRemoved() {
|
||||
|
||||
// setup double
|
||||
RedisAtomicDouble test = new RedisAtomicDouble("test", factory, 1);
|
||||
assertThat(test.get(), equalTo(1D)); // this passes
|
||||
|
||||
template.delete("test");
|
||||
|
||||
assertThat(test.getAndSet(2), is(0D));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,21 +41,31 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Integration test of {@link RedisAtomicInteger}
|
||||
*
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
|
||||
|
||||
private RedisAtomicInteger intCounter;
|
||||
private RedisConnectionFactory factory;
|
||||
private RedisTemplate<String, Integer> template;
|
||||
|
||||
public RedisAtomicIntegerTests(RedisConnectionFactory factory) {
|
||||
intCounter = new RedisAtomicInteger(getClass().getSimpleName() + ":int", factory);
|
||||
|
||||
this.intCounter = new RedisAtomicInteger(getClass().getSimpleName() + ":int", factory);
|
||||
this.factory = factory;
|
||||
|
||||
this.template = new RedisTemplate<String, Integer>();
|
||||
this.template.setConnectionFactory(factory);
|
||||
this.template.setKeySerializer(new StringRedisSerializer());
|
||||
this.template.setValueSerializer(new GenericToStringSerializer<Integer>(Integer.class));
|
||||
this.template.afterPropertiesSet();
|
||||
|
||||
ConnectionFactoryTracker.add(factory);
|
||||
}
|
||||
|
||||
@@ -77,7 +87,7 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckAndSet() throws Exception {
|
||||
public void testCheckAndSet() {
|
||||
// Txs not supported in Jredis
|
||||
assumeTrue(!ConnectionUtils.isJredis(factory));
|
||||
intCounter.set(0);
|
||||
@@ -87,7 +97,7 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncrementAndGet() throws Exception {
|
||||
public void testIncrementAndGet() {
|
||||
intCounter.set(0);
|
||||
assertEquals(1, intCounter.incrementAndGet());
|
||||
}
|
||||
@@ -100,11 +110,55 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecrementAndGet() throws Exception {
|
||||
public void testDecrementAndGet() {
|
||||
intCounter.set(1);
|
||||
assertEquals(0, intCounter.decrementAndGet());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void testGetAndIncrement() {
|
||||
|
||||
intCounter.set(1);
|
||||
assertEquals(1, intCounter.getAndIncrement());
|
||||
assertEquals(2, intCounter.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void testGetAndAdd() {
|
||||
|
||||
intCounter.set(1);
|
||||
assertEquals(1, intCounter.getAndAdd(5));
|
||||
assertEquals(6, intCounter.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void testGetAndDecrement() {
|
||||
|
||||
intCounter.set(1);
|
||||
assertEquals(1, intCounter.getAndDecrement());
|
||||
assertEquals(0, intCounter.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void testGetAndSet() {
|
||||
|
||||
intCounter.set(1);
|
||||
assertEquals(1, intCounter.getAndSet(5));
|
||||
assertEquals(5, intCounter.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("DATAREDIS-108 Test is intermittently failing")
|
||||
public void testCompareSet() throws Exception {
|
||||
@@ -171,12 +225,6 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
|
||||
@Test
|
||||
public void testShouldBeAbleToUseRedisAtomicIntegerWithProperlyConfiguredRedisTemplate() {
|
||||
|
||||
RedisTemplate<String, Integer> template = new RedisTemplate<String, Integer>();
|
||||
template.setConnectionFactory(factory);
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new GenericToStringSerializer<Integer>(Integer.class));
|
||||
template.afterPropertiesSet();
|
||||
|
||||
RedisAtomicInteger ral = new RedisAtomicInteger("DATAREDIS-317.atomicInteger", template);
|
||||
ral.set(32);
|
||||
|
||||
@@ -192,18 +240,27 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
|
||||
expectedException.expect(DataRetrievalFailureException.class);
|
||||
expectedException.expectMessage("'test' seems to no longer exist");
|
||||
|
||||
// setup long
|
||||
// setup integer
|
||||
RedisAtomicInteger test = new RedisAtomicInteger("test", factory, 1);
|
||||
assertThat(test.get(), equalTo(1)); // this passes
|
||||
|
||||
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
|
||||
template.setConnectionFactory(factory);
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
|
||||
template.afterPropertiesSet();
|
||||
|
||||
template.delete("test");
|
||||
|
||||
test.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void getAndSetReturnsZeroWhenKeyHasBeenRemoved() {
|
||||
|
||||
// setup integer
|
||||
RedisAtomicInteger test = new RedisAtomicInteger("test", factory, 1);
|
||||
assertThat(test.get(), equalTo(1)); // this passes
|
||||
|
||||
template.delete("test");
|
||||
|
||||
assertThat(test.getAndSet(2), is(0));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Integration test of {@link RedisAtomicLong}
|
||||
*
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
@@ -49,10 +49,19 @@ public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
|
||||
|
||||
private RedisAtomicLong longCounter;
|
||||
private RedisConnectionFactory factory;
|
||||
private RedisTemplate<String, Long> template;
|
||||
|
||||
public RedisAtomicLongTests(RedisConnectionFactory factory) {
|
||||
longCounter = new RedisAtomicLong(getClass().getSimpleName() + ":long", factory);
|
||||
|
||||
this.longCounter = new RedisAtomicLong(getClass().getSimpleName() + ":long", factory);
|
||||
this.factory = factory;
|
||||
|
||||
this.template = new RedisTemplate<String, Long>();
|
||||
this.template.setConnectionFactory(factory);
|
||||
this.template.setKeySerializer(new StringRedisSerializer());
|
||||
this.template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
|
||||
this.template.afterPropertiesSet();
|
||||
|
||||
ConnectionFactoryTracker.add(factory);
|
||||
}
|
||||
|
||||
@@ -102,8 +111,53 @@ public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
|
||||
assertEquals(0, longCounter.decrementAndGet());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void testGetAndIncrement() {
|
||||
|
||||
longCounter.set(1);
|
||||
assertEquals(1, longCounter.getAndIncrement());
|
||||
assertEquals(2, longCounter.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void testGetAndAdd() {
|
||||
|
||||
longCounter.set(1);
|
||||
assertEquals(1, longCounter.getAndAdd(5));
|
||||
assertEquals(6, longCounter.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void testGetAndDecrement() {
|
||||
|
||||
longCounter.set(1);
|
||||
assertEquals(1, longCounter.getAndDecrement());
|
||||
assertEquals(0, longCounter.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void testGetAndSet() {
|
||||
|
||||
longCounter.set(1);
|
||||
assertEquals(1, longCounter.getAndSet(5));
|
||||
assertEquals(5, longCounter.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetExistingValue() throws Exception {
|
||||
|
||||
longCounter.set(5);
|
||||
RedisAtomicLong keyCopy = new RedisAtomicLong(longCounter.getKey(), factory);
|
||||
assertEquals(longCounter.get(), keyCopy.get());
|
||||
@@ -167,14 +221,23 @@ public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
|
||||
RedisAtomicLong test = new RedisAtomicLong("test", factory, 1);
|
||||
assertThat(test.get(), equalTo(1L)); // this passes
|
||||
|
||||
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
|
||||
template.setConnectionFactory(factory);
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
|
||||
template.afterPropertiesSet();
|
||||
|
||||
template.delete("test");
|
||||
|
||||
test.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void getAndSetReturnsZeroWhenKeyHasBeenRemoved() {
|
||||
|
||||
// setup long
|
||||
RedisAtomicLong test = new RedisAtomicLong("test", factory, 1);
|
||||
assertThat(test.get(), equalTo(1L)); // this passes
|
||||
|
||||
template.delete("test");
|
||||
|
||||
assertThat(test.getAndSet(2), is(0L));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user