DATAREDIS-469 - Throw DataRetrievalFailureException for atomic numbers when key is removed.
We now throw DataRetrievalFailureException in case of get() when the underlying key storing the value of an AtomicInteger, AtomicLong or AtomicDouble is removed from Redis. Original pull request: #182.
This commit is contained in:
committed by
Mark Paluch
parent
d097a64bc6
commit
20cfd3e76c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-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.
|
||||
@@ -20,6 +20,7 @@ import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.data.redis.connection.DataType;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.BoundKeyOperations;
|
||||
@@ -38,6 +39,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class RedisAtomicDouble extends Number implements Serializable, BoundKeyOperations<String> {
|
||||
|
||||
@@ -143,7 +145,13 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
|
||||
* @return the current value
|
||||
*/
|
||||
public double get() {
|
||||
return operations.get(key);
|
||||
|
||||
Double value = operations.get(key);
|
||||
if (value != null) {
|
||||
return value.doubleValue();
|
||||
}
|
||||
|
||||
throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
* 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.
|
||||
@@ -20,6 +20,7 @@ import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.data.redis.connection.DataType;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.BoundKeyOperations;
|
||||
@@ -39,6 +40,7 @@ import org.springframework.util.Assert;
|
||||
* @see java.util.concurrent.atomic.AtomicInteger
|
||||
* @author Costin Leau
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class RedisAtomicInteger extends Number implements Serializable, BoundKeyOperations<String> {
|
||||
|
||||
@@ -141,7 +143,13 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
* @return the current value
|
||||
*/
|
||||
public int get() {
|
||||
return Integer.valueOf(operations.get(key));
|
||||
|
||||
Integer value = operations.get(key);
|
||||
if (value != null) {
|
||||
return value.intValue();
|
||||
}
|
||||
|
||||
throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
* 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.
|
||||
@@ -20,6 +20,7 @@ import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.data.redis.connection.DataType;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.BoundKeyOperations;
|
||||
@@ -39,6 +40,7 @@ import org.springframework.util.Assert;
|
||||
* @see java.util.concurrent.atomic.AtomicLong
|
||||
* @author Costin Leau
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class RedisAtomicLong extends Number implements Serializable, BoundKeyOperations<String> {
|
||||
|
||||
@@ -149,7 +151,13 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
* @return the current value
|
||||
*/
|
||||
public long get() {
|
||||
return operations.get(key);
|
||||
|
||||
Long value = operations.get(key);
|
||||
if (value != null) {
|
||||
return value.longValue();
|
||||
}
|
||||
|
||||
throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-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.
|
||||
@@ -30,6 +30,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.RedisTestProfileValueSource;
|
||||
import org.springframework.data.redis.connection.ConnectionUtils;
|
||||
@@ -44,6 +45,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
|
||||
@@ -210,4 +212,28 @@ public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
|
||||
|
||||
assertThat(ral.get(), is(32.23));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void getThrowsExceptionWhenKeyHasBeenRemoved() {
|
||||
|
||||
expectedException.expect(DataRetrievalFailureException.class);
|
||||
expectedException.expectMessage("'test' seems to no longer exist");
|
||||
|
||||
// setup long
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-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.
|
||||
@@ -30,6 +30,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.connection.ConnectionUtils;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
@@ -44,6 +45,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
|
||||
@@ -180,4 +182,28 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
|
||||
|
||||
assertThat(ral.get(), is(32));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void getThrowsExceptionWhenKeyHasBeenRemoved() {
|
||||
|
||||
expectedException.expect(DataRetrievalFailureException.class);
|
||||
expectedException.expectMessage("'test' seems to no longer exist");
|
||||
|
||||
// setup long
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.connection.ConnectionUtils;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
@@ -41,6 +42,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
|
||||
@@ -151,4 +153,28 @@ public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
|
||||
|
||||
assertThat(ral.get(), is(32L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-469
|
||||
*/
|
||||
@Test
|
||||
public void getThrowsExceptionWhenKeyHasBeenRemoved() {
|
||||
|
||||
expectedException.expect(DataRetrievalFailureException.class);
|
||||
expectedException.expectMessage("'test' seems to no longer exist");
|
||||
|
||||
// setup long
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user