DATAREDIS-188 - Infinite loop renaming a non-existent Collection when using Lettuce.
We run into an infinite loop in org.springframework.data.redis.support.collections.CollectionUtils.rename(K, K, RedisOperations<K, ?>) if no value was associated with the key which leads to operations.hasKey(...) always returning false which prevents the actual renaming. RedisCollections will only send rename command if values are present as empty collections cannot be stored and therefor cannot be renamed. Original pull request: #28
This commit is contained in:
@@ -19,21 +19,23 @@ import static org.junit.Assert.*;
|
||||
import static org.junit.Assume.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.ObjectFactory;
|
||||
import org.springframework.data.redis.connection.ConnectionUtils;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.core.BoundKeyOperations;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
|
||||
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
|
||||
@@ -42,13 +44,20 @@ import org.springframework.data.redis.support.atomic.RedisAtomicLong;
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class BoundKeyOperationsTest {
|
||||
|
||||
@SuppressWarnings("rawtypes")//
|
||||
private BoundKeyOperations keyOps;
|
||||
|
||||
private ObjectFactory<Object> objFactory;
|
||||
|
||||
@SuppressWarnings("rawtypes")//
|
||||
private RedisTemplate template;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public BoundKeyOperationsTest(BoundKeyOperations<Object> keyOps, ObjectFactory<Object> objFactory,
|
||||
RedisTemplate template) {
|
||||
this.objFactory = objFactory;
|
||||
@@ -57,8 +66,23 @@ public class BoundKeyOperationsTest {
|
||||
ConnectionFactoryTracker.add(template.getConnectionFactory());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
populateBoundKey();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@After
|
||||
public void stop() {}
|
||||
public void tearDown() {
|
||||
template.execute(new RedisCallback<Object>() {
|
||||
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
connection.flushDb();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
@@ -70,23 +94,16 @@ public class BoundKeyOperationsTest {
|
||||
return BoundKeyParams.testParams();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testRename() throws Exception {
|
||||
// DATAREDIS-188 Infinite loop renaming a non-existent Collection when using Lettuce
|
||||
assumeTrue(!ConnectionUtils.isLettuce(template.getConnectionFactory()));
|
||||
|
||||
Object key = keyOps.getKey();
|
||||
assertNotNull(key);
|
||||
// RedisAtomicInteger/Long need to be reset, as they may be created
|
||||
// at start of test run and underlying key wiped out by other tests
|
||||
try {
|
||||
keyOps.getClass().getMethod("set", int.class).invoke(keyOps, 0);
|
||||
} catch (NoSuchMethodException e) {}
|
||||
try {
|
||||
keyOps.getClass().getMethod("set", long.class).invoke(keyOps, 0l);
|
||||
} catch (NoSuchMethodException e) {}
|
||||
Object newName = objFactory.instance();
|
||||
|
||||
keyOps.rename(newName);
|
||||
assertEquals(newName, keyOps.getKey());
|
||||
|
||||
keyOps.rename(key);
|
||||
assertEquals(key, keyOps.getKey());
|
||||
}
|
||||
@@ -97,9 +114,8 @@ public class BoundKeyOperationsTest {
|
||||
@Test
|
||||
public void testExpire() throws Exception {
|
||||
|
||||
populateBoundKey();
|
||||
|
||||
assertEquals(keyOps.getClass().getName() + " -> " + keyOps.getKey(), Long.valueOf(-1), keyOps.getExpire());
|
||||
|
||||
if (keyOps.expire(10, TimeUnit.SECONDS)) {
|
||||
long expire = keyOps.getExpire().longValue();
|
||||
assertTrue(expire <= 10 && expire > 5);
|
||||
@@ -113,22 +129,20 @@ public class BoundKeyOperationsTest {
|
||||
public void testPersist() throws Exception {
|
||||
assumeTrue(!ConnectionUtils.isJredis(template.getConnectionFactory()));
|
||||
|
||||
populateBoundKey();
|
||||
|
||||
keyOps.persist();
|
||||
|
||||
assertEquals(keyOps.getClass().getName() + " -> " + keyOps.getKey(), Long.valueOf(-1), keyOps.getExpire());
|
||||
if (keyOps.expire(10, TimeUnit.SECONDS)) {
|
||||
assertTrue(keyOps.getExpire().longValue() > 0);
|
||||
}
|
||||
|
||||
keyOps.persist();
|
||||
assertEquals(keyOps.getClass().getName() + " -> " + keyOps.getKey(), -1, keyOps.getExpire().longValue());
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void populateBoundKey() {
|
||||
|
||||
if (keyOps instanceof List || keyOps instanceof Set) {
|
||||
if (keyOps instanceof Collection) {
|
||||
((Collection) keyOps).add("dummy");
|
||||
} else if (keyOps instanceof Map) {
|
||||
((Map) keyOps).put("dummy", "dummy");
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright 2014 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.support.collections;
|
||||
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.redis.connection.DataType;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AbstractRedisCollectionUnitTests {
|
||||
|
||||
private AbstractRedisCollection<String> collection;
|
||||
|
||||
@SuppressWarnings("rawtypes")//
|
||||
private RedisTemplate redisTemplateSpy;
|
||||
private @Mock RedisConnectionFactory connectionFactoryMock;
|
||||
private @Mock RedisConnection connectionMock;
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
redisTemplateSpy = spy(new RedisTemplate() {
|
||||
|
||||
public Boolean hasKey(Object key) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
redisTemplateSpy.setConnectionFactory(connectionFactoryMock);
|
||||
redisTemplateSpy.afterPropertiesSet();
|
||||
|
||||
collection = new AbstractRedisCollection<String>("key", redisTemplateSpy) {
|
||||
|
||||
private List<String> delegate = new ArrayList<String>();
|
||||
|
||||
@Override
|
||||
public boolean add(String value) {
|
||||
return this.delegate.add(value);
|
||||
};
|
||||
|
||||
@Override
|
||||
public DataType getType() {
|
||||
return DataType.LIST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<String> iterator() {
|
||||
return this.delegate.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return this.delegate.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return this.delegate.isEmpty();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
when(connectionFactoryMock.getConnection()).thenReturn(connectionMock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-188
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testRenameOfEmptyCollectionShouldNotTriggerRedisOperation() {
|
||||
|
||||
collection.rename("new-key");
|
||||
verify(redisTemplateSpy, never()).rename(eq("key"), eq("new-key"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-188
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testRenameCollectionShouldTriggerRedisOperation() {
|
||||
|
||||
when(redisTemplateSpy.hasKey(anyObject())).thenReturn(Boolean.TRUE);
|
||||
|
||||
collection.add("spring-data-redis");
|
||||
collection.rename("new-key");
|
||||
verify(redisTemplateSpy, times(1)).rename(eq("key"), eq("new-key"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user