tests refactoring

pull/1285/head
Nikita 7 years ago
parent 8345e24c0b
commit 3152a46628

@ -2,19 +2,845 @@ package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.redisson.api.RLocalCachedMap;
import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.redisson.api.map.MapLoader;
import org.redisson.api.map.MapWriter;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
public abstract class BaseMapTest extends BaseTest {
public static class SimpleKey implements Serializable {
private String key;
public SimpleKey() {
}
public SimpleKey(String field) {
this.key = field;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Override
public String toString() {
return "key: " + key;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SimpleKey other = (SimpleKey) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return true;
}
}
public static class SimpleValue implements Serializable {
private String value;
public SimpleValue() {
}
public SimpleValue(String field) {
this.value = field;
}
public void setValue(String field) {
this.value = field;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "value: " + value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SimpleValue other = (SimpleValue) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}
@Test
public void testGetAllWithStringKeys() {
RMap<String, Integer> map = getMap("getAllStrings");
map.put("A", 100);
map.put("B", 200);
map.put("C", 300);
map.put("D", 400);
Map<String, Integer> filtered = map.getAll(new HashSet<String>(Arrays.asList("B", "C", "E")));
Map<String, Integer> expectedMap = new HashMap<String, Integer>();
expectedMap.put("B", 200);
expectedMap.put("C", 300);
assertThat(filtered).isEqualTo(expectedMap);
}
@Test
public void testStringCodec() {
Config config = createConfig();
config.setCodec(StringCodec.INSTANCE);
RedissonClient redisson = Redisson.create(config);
RMap<String, String> rmap = redisson.getMap("TestRMap01");
rmap.put("A", "1");
rmap.put("B", "2");
Iterator<Map.Entry<String, String>> iterator = rmap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
assertThat(next).isIn(new AbstractMap.SimpleEntry("A", "1"), new AbstractMap.SimpleEntry("B", "2"));
}
redisson.shutdown();
}
@Test
public void testInteger() {
Map<Integer, Integer> map = getMap("test_int");
map.put(1, 2);
map.put(3, 4);
assertThat(map.size()).isEqualTo(2);
Integer val = map.get(1);
assertThat(val).isEqualTo(2);
Integer val2 = map.get(3);
assertThat(val2).isEqualTo(4);
}
@Test
public void testLong() {
Map<Long, Long> map = getMap("test_long");
map.put(1L, 2L);
map.put(3L, 4L);
assertThat(map.size()).isEqualTo(2);
Long val = map.get(1L);
assertThat(val).isEqualTo(2);
Long val2 = map.get(3L);
assertThat(val2).isEqualTo(4);
}
@Test
public void testIterator() {
RMap<Integer, Integer> rMap = getMap("123");
int size = 1000;
for (int i = 0; i < size; i++) {
rMap.put(i, i);
}
assertThat(rMap.size()).isEqualTo(1000);
int counter = 0;
for (Integer key : rMap.keySet()) {
counter++;
}
assertThat(counter).isEqualTo(size);
counter = 0;
for (Integer value : rMap.values()) {
counter++;
}
assertThat(counter).isEqualTo(size);
counter = 0;
for (Entry<Integer, Integer> entry : rMap.entrySet()) {
counter++;
}
assertThat(counter).isEqualTo(size);
}
@Test
public void testOrdering() {
Map<String, String> map = new LinkedHashMap<String, String>();
// General player data
map.put("name", "123");
map.put("ip", "4124");
map.put("rank", "none");
map.put("tokens", "0");
map.put("coins", "0");
// Arsenal player statistics
map.put("ar_score", "0");
map.put("ar_gameswon", "0");
map.put("ar_gameslost", "0");
map.put("ar_kills", "0");
map.put("ar_deaths", "0");
RMap<String, String> rmap = getMap("123");
Assume.assumeTrue(!(rmap instanceof RLocalCachedMap));
rmap.putAll(map);
assertThat(rmap.keySet()).containsExactlyElementsOf(map.keySet());
assertThat(rmap.readAllKeySet()).containsExactlyElementsOf(map.keySet());
assertThat(rmap.values()).containsExactlyElementsOf(map.values());
assertThat(rmap.readAllValues()).containsExactlyElementsOf(map.values());
assertThat(rmap.entrySet()).containsExactlyElementsOf(map.entrySet());
assertThat(rmap.readAllEntrySet()).containsExactlyElementsOf(map.entrySet());
}
@Test(expected = NullPointerException.class)
public void testNullValue() {
Map<Integer, String> map = getMap("simple12");
map.put(1, null);
}
@Test(expected = NullPointerException.class)
public void testNullKey() {
Map<Integer, String> map = getMap("simple12");
map.put(null, "1");
}
@Test
public void testSize() {
Map<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertEquals(3, map.size());
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
Assert.assertEquals(3, map.size());
map.put(new SimpleKey("1"), new SimpleValue("21"));
map.put(new SimpleKey("3"), new SimpleValue("41"));
Assert.assertEquals(3, map.size());
map.put(new SimpleKey("51"), new SimpleValue("6"));
Assert.assertEquals(4, map.size());
map.remove(new SimpleKey("3"));
Assert.assertEquals(3, map.size());
}
@Test
public void testEmptyRemove() {
RMap<Integer, Integer> map = getMap("simple");
Assert.assertFalse(map.remove(1, 3));
map.put(4, 5);
Assert.assertTrue(map.remove(4, 5));
}
public void testFastPutIfAbsent() throws Exception {
RMap<SimpleKey, SimpleValue> map = getMap("simple");
SimpleKey key = new SimpleKey("1");
SimpleValue value = new SimpleValue("2");
map.put(key, value);
assertThat(map.fastPutIfAbsent(key, new SimpleValue("3"))).isFalse();
assertThat(map.get(key)).isEqualTo(value);
SimpleKey key1 = new SimpleKey("2");
SimpleValue value1 = new SimpleValue("4");
assertThat(map.fastPutIfAbsent(key1, value1)).isTrue();
assertThat(map.get(key1)).isEqualTo(value1);
}
@Test
public void testPutAll() {
Map<Integer, String> map = getMap("simple");
map.put(1, "1");
map.put(2, "2");
map.put(3, "3");
Map<Integer, String> joinMap = new HashMap<Integer, String>();
joinMap.put(4, "4");
joinMap.put(5, "5");
joinMap.put(6, "6");
map.putAll(joinMap);
assertThat(map.keySet()).containsOnly(1, 2, 3, 4, 5, 6);
}
@Test
public void testPutAllBig() {
Map<Integer, String> joinMap = new HashMap<Integer, String>();
for (int i = 0; i < 100000; i++) {
joinMap.put(i, "" + i);
}
Map<Integer, String> map = getMap("simple");
map.putAll(joinMap);
assertThat(map.size()).isEqualTo(joinMap.size());
}
@Test
public void testPutGet() {
Map<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
SimpleValue val1 = map.get(new SimpleKey("33"));
Assert.assertEquals("44", val1.getValue());
SimpleValue val2 = map.get(new SimpleKey("5"));
Assert.assertEquals("6", val2.getValue());
}
@Test
public void testPutIfAbsent() throws Exception {
ConcurrentMap<SimpleKey, SimpleValue> map = getMap("simple");
SimpleKey key = new SimpleKey("1");
SimpleValue value = new SimpleValue("2");
map.put(key, value);
Assert.assertEquals(value, map.putIfAbsent(key, new SimpleValue("3")));
Assert.assertEquals(value, map.get(key));
SimpleKey key1 = new SimpleKey("2");
SimpleValue value1 = new SimpleValue("4");
Assert.assertNull(map.putIfAbsent(key1, value1));
Assert.assertEquals(value1, map.get(key1));
}
@Test(timeout = 5000)
public void testDeserializationErrorReturnsErrorImmediately() throws Exception {
redisson.getConfig().setCodec(new JsonJacksonCodec());
RMap<String, SimpleObjectWithoutDefaultConstructor> map = getMap("deserializationFailure");
Assume.assumeTrue(!(map instanceof RLocalCachedMap));
SimpleObjectWithoutDefaultConstructor object = new SimpleObjectWithoutDefaultConstructor("test-val");
Assert.assertEquals("test-val", object.getTestField());
map.put("test-key", object);
try {
map.get("test-key");
Assert.fail("Expected exception from map.get() call");
} catch (Exception e) {
e.printStackTrace();
}
}
public static class SimpleObjectWithoutDefaultConstructor {
private String testField;
SimpleObjectWithoutDefaultConstructor(String testField) {
this.testField = testField;
}
public String getTestField() {
return testField;
}
public void setTestField(String testField) {
this.testField = testField;
}
}
@Test
public void testReplaceOldValueFail() {
ConcurrentMap<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("43"), new SimpleValue("31"));
Assert.assertFalse(res);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("2", val1.getValue());
}
@Test
public void testReplaceOldValueSuccess() {
ConcurrentMap<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertTrue(res);
boolean res1 = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertFalse(res1);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
}
@Test
public void testReplaceValue() {
ConcurrentMap<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3"));
Assert.assertEquals("2", res.getValue());
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
}
@Test
public void testReplace() {
Map<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
SimpleValue val1 = map.get(new SimpleKey("33"));
Assert.assertEquals("44", val1.getValue());
map.put(new SimpleKey("33"), new SimpleValue("abc"));
SimpleValue val2 = map.get(new SimpleKey("33"));
Assert.assertEquals("abc", val2.getValue());
}
@Test
public void testContainsValue() {
Map<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertTrue(map.containsValue(new SimpleValue("2")));
Assert.assertFalse(map.containsValue(new SimpleValue("441")));
Assert.assertFalse(map.containsValue(new SimpleKey("5")));
}
@Test
public void testContainsKey() {
Map<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertTrue(map.containsKey(new SimpleKey("33")));
Assert.assertFalse(map.containsKey(new SimpleKey("34")));
}
@Test
public void testRemoveValueFail() {
ConcurrentMap<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.remove(new SimpleKey("2"), new SimpleValue("1"));
Assert.assertFalse(res);
boolean res1 = map.remove(new SimpleKey("1"), new SimpleValue("3"));
Assert.assertFalse(res1);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("2", val1.getValue());
}
@Test
public void testRemoveValue() {
ConcurrentMap<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.remove(new SimpleKey("1"), new SimpleValue("2"));
Assert.assertTrue(res);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertNull(val1);
Assert.assertEquals(0, map.size());
}
@Test
public void testRemoveObject() {
Map<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
assertThat(map.remove(new SimpleKey("33"))).isEqualTo(new SimpleValue("44"));
assertThat(map.remove(new SimpleKey("5"))).isEqualTo(new SimpleValue("6"));
assertThat(map.remove(new SimpleKey("11"))).isNull();
assertThat(map.size()).isEqualTo(1);
}
@Test
public void testRemove() {
RMap<Integer, Integer> map = getMap("simple");
map.put(1, 3);
map.put(3, 5);
map.put(7, 8);
assertThat(map.remove(1)).isEqualTo(3);
assertThat(map.remove(3)).isEqualTo(5);
assertThat(map.remove(10)).isNull();
assertThat(map.remove(7)).isEqualTo(8);
}
@Test
public void testFastRemove() throws InterruptedException, ExecutionException {
RMap<Integer, Integer> map = getMap("simple");
map.put(1, 3);
map.put(3, 5);
map.put(4, 6);
map.put(7, 8);
assertThat(map.fastRemove(1, 3, 7)).isEqualTo(3);
Thread.sleep(1);
assertThat(map.size()).isEqualTo(1);
}
@Test
public void testValueIterator() {
RMap<Integer, Integer> map = getMap("simple");
map.put(1, 0);
map.put(3, 5);
map.put(4, 6);
map.put(7, 8);
Collection<Integer> values = map.values();
assertThat(values).containsOnly(0, 5, 6, 8);
for (Iterator<Integer> iterator = map.values().iterator(); iterator.hasNext();) {
Integer value = iterator.next();
if (!values.remove(value)) {
Assert.fail();
}
}
assertThat(values.size()).isEqualTo(0);
}
@Test
public void testFastPut() throws Exception {
RMap<Integer, Integer> map = getMap("simple");
Assert.assertTrue(map.fastPut(1, 2));
assertThat(map.get(1)).isEqualTo(2);
Assert.assertFalse(map.fastPut(1, 3));
assertThat(map.get(1)).isEqualTo(3);
Assert.assertEquals(1, map.size());
}
@Test
public void testEquals() {
RMap<String, String> map = getMap("simple");
map.put("1", "7");
map.put("2", "4");
map.put("3", "5");
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("1", "7");
testMap.put("2", "4");
testMap.put("3", "5");
assertThat(map).isEqualTo(testMap);
assertThat(testMap.hashCode()).isEqualTo(map.hashCode());
}
@Test
public void testFastRemoveEmpty() throws Exception {
RMap<Integer, Integer> map = getMap("simple");
map.put(1, 3);
assertThat(map.fastRemove()).isZero();
assertThat(map.size()).isEqualTo(1);
}
@Test
public void testKeySetByPattern() {
RMap<String, String> map = getMap("simple", StringCodec.INSTANCE);
map.put("10", "100");
map.put("20", "200");
map.put("30", "300");
assertThat(map.keySet("?0")).containsExactly("10", "20", "30");
assertThat(map.keySet("1")).isEmpty();
assertThat(map.keySet("10")).containsExactly("10");
}
@Test
public void testValuesByPattern() {
RMap<String, String> map = getMap("simple", StringCodec.INSTANCE);
map.put("10", "100");
map.put("20", "200");
map.put("30", "300");
assertThat(map.values("?0")).containsExactly("100", "200", "300");
assertThat(map.values("1")).isEmpty();
assertThat(map.values("10")).containsExactly("100");
}
@Test
public void testEntrySetByPattern() {
RMap<String, String> map = getMap("simple", StringCodec.INSTANCE);
map.put("10", "100");
map.put("20", "200");
map.put("30", "300");
assertThat(map.entrySet("?0")).containsExactly(new AbstractMap.SimpleEntry("10", "100"), new AbstractMap.SimpleEntry("20", "200"), new AbstractMap.SimpleEntry("30", "300"));
assertThat(map.entrySet("1")).isEmpty();
assertThat(map.entrySet("10")).containsExactly(new AbstractMap.SimpleEntry("10", "100"));
}
@Test
public void testReadAllKeySet() {
RMap<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
assertThat(map.readAllKeySet().size()).isEqualTo(3);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllKeySet()).containsOnlyElementsOf(testMap.keySet());
}
@Test
public void testEntrySetIteratorRemoveHighVolume() throws InterruptedException {
RMap<Integer, Integer> map = getMap("simpleMap");
for (int i = 0; i < 10000; i++) {
map.put(i, i*10);
}
int cnt = 0;
Iterator<Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Integer, Integer> entry = iterator.next();
iterator.remove();
cnt++;
}
Assert.assertEquals(10000, cnt);
assertThat(map).isEmpty();
Assert.assertEquals(0, map.size());
}
@Test
public void testEntrySetIteratorRandomRemoveHighVolume() throws InterruptedException {
RMap<Integer, Integer> map = getMap("simpleMap");
for (int i = 0; i < 10000; i++) {
map.put(i, i*10);
}
int cnt = 0;
int removed = 0;
Iterator<Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Integer, Integer> entry = iterator.next();
if (ThreadLocalRandom.current().nextBoolean()) {
iterator.remove();
removed++;
}
cnt++;
}
Assert.assertEquals(10000, cnt);
assertThat(map.size()).isEqualTo(cnt - removed);
}
@Test
public void testKeySetIteratorRemoveHighVolume() throws InterruptedException {
RMap<Integer, Integer> map = getMap("simpleMap");
for (int i = 0; i < 10000; i++) {
map.put(i, i*10);
}
int cnt = 0;
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Integer integer = iterator.next();
iterator.remove();
cnt++;
}
Assert.assertEquals(10000, cnt);
assertThat(map).isEmpty();
Assert.assertEquals(0, map.size());
}
@Test
public void testReadAllKeySetHighAmount() {
RMap<SimpleKey, SimpleValue> map = getMap("simple");
for (int i = 0; i < 1000; i++) {
map.put(new SimpleKey("" + i), new SimpleValue("" + i));
}
assertThat(map.readAllKeySet().size()).isEqualTo(1000);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllKeySet()).containsOnlyElementsOf(testMap.keySet());
}
@Test
public void testReadAllValues() {
RMap<SimpleKey, SimpleValue> map = getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
assertThat(map.readAllValues().size()).isEqualTo(3);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllValues()).containsOnlyElementsOf(testMap.values());
}
@Test
public void testGetAllBig() {
Map<Integer, String> joinMap = new HashMap<Integer, String>();
for (int i = 0; i < 10000; i++) {
joinMap.put(i, "" + i);
}
RMap<Integer, String> map = getMap("simple");
map.putAll(joinMap);
Map<Integer, String> s = map.getAll(joinMap.keySet());
assertThat(s).isEqualTo(joinMap);
assertThat(map.size()).isEqualTo(joinMap.size());
}
@Test
public void testGetAll() {
RMap<Integer, Integer> map = getMap("getAll");
map.put(1, 100);
map.put(2, 200);
map.put(3, 300);
map.put(4, 400);
Map<Integer, Integer> filtered = map.getAll(new HashSet<Integer>(Arrays.asList(2, 3, 5)));
Map<Integer, Integer> expectedMap = new HashMap<Integer, Integer>();
expectedMap.put(2, 200);
expectedMap.put(3, 300);
assertThat(filtered).isEqualTo(expectedMap);
}
@Test
public void testValueSize() {
Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("3.2.0") > 0);
RMap<String, String> map = getMap("getAll");
Assume.assumeTrue(!(map instanceof RMapCache));
map.put("1", "1234");
assertThat(map.valueSize("4")).isZero();
assertThat(map.valueSize("1")).isEqualTo(6);
}
@Test
public void testGetAllOrder() {
RMap<Integer, Integer> map = getMap("getAll");
map.put(1, 100);
map.put(2, 200);
map.put(3, 300);
map.put(4, 400);
map.put(5, 500);
map.put(6, 600);
map.put(7, 700);
map.put(8, 800);
Map<Integer, Integer> filtered = map.getAll(new HashSet<Integer>(Arrays.asList(2, 3, 5, 1, 7, 8)));
Map<Integer, Integer> expectedMap = new LinkedHashMap<Integer, Integer>();
expectedMap.put(1, 100);
expectedMap.put(2, 200);
expectedMap.put(3, 300);
expectedMap.put(5, 500);
expectedMap.put(7, 700);
expectedMap.put(8, 800);
assertThat(filtered.entrySet()).containsExactlyElementsOf(expectedMap.entrySet());
}
@Test
public void testAddAndGet() throws InterruptedException {
RMap<Integer, Integer> map = getMap("getAll");
map.put(1, 100);
Integer res = map.addAndGet(1, 12);
assertThat(res).isEqualTo(112);
res = map.get(1);
assertThat(res).isEqualTo(112);
RMap<Integer, Double> map2 = getMap("getAll2");
map2.put(1, new Double(100.2));
Double res2 = map2.addAndGet(1, new Double(12.1));
assertThat(res2).isEqualTo(112.3);
res2 = map2.get(1);
assertThat(res2).isEqualTo(112.3);
RMap<String, Integer> mapStr = getMap("mapStr");
assertThat(mapStr.put("1", 100)).isNull();
assertThat(mapStr.addAndGet("1", 12)).isEqualTo(112);
assertThat(mapStr.get("1")).isEqualTo(112);
}
protected abstract <K, V> RMap<K, V> getMap(String name);
protected abstract <K, V> RMap<K, V> getMap(String name, Codec codec);
protected abstract <K, V> RMap<K, V> getWriterTestMap(String name, Map<K, V> map);
protected abstract <K, V> RMap<K, V> getLoaderTestMap(String name, Map<K, V> map);

@ -13,8 +13,6 @@ import org.junit.Assert;
import org.junit.Test;
import org.redisson.RedissonLocalCachedMap.CacheKey;
import org.redisson.RedissonLocalCachedMap.CacheValue;
import org.redisson.RedissonMapTest.SimpleKey;
import org.redisson.RedissonMapTest.SimpleValue;
import org.redisson.api.LocalCachedMapOptions;
import org.redisson.api.LocalCachedMapOptions.EvictionPolicy;
import org.redisson.api.LocalCachedMapOptions.ReconnectionStrategy;
@ -22,6 +20,7 @@ import org.redisson.api.LocalCachedMapOptions.SyncStrategy;
import org.redisson.api.RLocalCachedMap;
import org.redisson.api.RMap;
import org.redisson.cache.Cache;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import mockit.Deencapsulation;
@ -114,16 +113,26 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
}
@Override
protected <K, V> RMap<K, V> getMap(String name) {
return redisson.getLocalCachedMap(name, LocalCachedMapOptions.<K, V>defaults());
}
@Override
protected <K, V> RMap<K, V> getMap(String name, Codec codec) {
return redisson.getLocalCachedMap(name, codec, LocalCachedMapOptions.<K, V>defaults());
}
@Override
protected <K, V> RMap<K, V> getWriterTestMap(String name, Map<K, V> map) {
LocalCachedMapOptions<K, V> options = LocalCachedMapOptions.<K, V>defaults().writer(createMapWriter(map));
return redisson.getLocalCachedMap("test", options);
return redisson.getLocalCachedMap(name, options);
}
@Override
protected <K, V> RMap<K, V> getLoaderTestMap(String name, Map<K, V> map) {
LocalCachedMapOptions<K, V> options = LocalCachedMapOptions.<K, V>defaults().loader(createMapLoader(map));
return redisson.getLocalCachedMap("test", options);
return redisson.getLocalCachedMap(name, options);
}
@Test
@ -375,7 +384,7 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
}
@Test
public void testSize() {
public void testSizeCache() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
Cache<CacheKey, CacheValue> cache = Deencapsulation.getField(map, "cache");
@ -404,7 +413,7 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
}
@Test
public void testPut() {
public void testPutGetCache() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
Cache<CacheKey, CacheValue> cache = Deencapsulation.getField(map, "cache");
@ -425,7 +434,7 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
}
@Test
public void testGetAll() {
public void testGetAllCache() {
RMap<String, Integer> map = redisson.getLocalCachedMap("getAll", LocalCachedMapOptions.defaults());
Cache<CacheKey, CacheValue> cache = Deencapsulation.getField(map, "cache");
map.put("1", 100);
@ -468,7 +477,7 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
@Test
public void testPutAll() throws InterruptedException {
public void testPutAllCache() throws InterruptedException {
Map<Integer, String> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
Map<Integer, String> map1 = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
Cache<CacheKey, CacheValue> cache = Deencapsulation.getField(map, "cache");

@ -1,19 +1,17 @@
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@ -23,7 +21,6 @@ import org.awaitility.Duration;
import org.junit.Assert;
import org.junit.Test;
import org.redisson.api.MapOptions;
import org.redisson.api.RFuture;
import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.map.event.EntryCreatedListener;
@ -31,116 +28,21 @@ import org.redisson.api.map.event.EntryEvent;
import org.redisson.api.map.event.EntryExpiredListener;
import org.redisson.api.map.event.EntryRemovedListener;
import org.redisson.api.map.event.EntryUpdatedListener;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.DoubleCodec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.codec.MsgPackJacksonCodec;
import static org.awaitility.Awaitility.*;
public class RedissonMapCacheTest extends BaseMapTest {
public static class SimpleKey implements Serializable {
private String key;
public SimpleKey() {
}
public SimpleKey(String field) {
this.key = field;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Override
public String toString() {
return "key: " + key;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SimpleKey other = (SimpleKey) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return true;
}
@Override
protected <K, V> RMap<K, V> getMap(String name) {
return redisson.getMapCache(name);
}
public static class SimpleValue implements Serializable {
private String value;
public SimpleValue() {
}
public SimpleValue(String field) {
this.value = field;
}
public void setValue(String field) {
this.value = field;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "value: " + value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SimpleValue other = (SimpleValue) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
@Override
protected <K, V> RMap<K, V> getMap(String name, Codec codec) {
return redisson.getMapCache(name, codec);
}
@Override
@ -156,7 +58,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testWriterPutIfAbsent() {
public void testWriterPutIfAbsentTTL() {
Map<String, String> store = new HashMap<>();
RMapCache<String, String> map = (RMapCache<String, String>) getWriterTestMap("test", store);
@ -365,37 +267,6 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testOrdering() {
Map<String, String> map = new LinkedHashMap<String, String>();
// General player data
map.put("name", "123");
map.put("ip", "4124");
map.put("rank", "none");
map.put("tokens", "0");
map.put("coins", "0");
// Arsenal player statistics
map.put("ar_score", "0");
map.put("ar_gameswon", "0");
map.put("ar_gameslost", "0");
map.put("ar_kills", "0");
map.put("ar_deaths", "0");
RMap<String, String> rmap = redisson.getMapCache("123");
rmap.putAll(map);
assertThat(rmap.keySet()).containsExactlyElementsOf(map.keySet());
assertThat(rmap.readAllKeySet()).containsExactlyElementsOf(map.keySet());
assertThat(rmap.values()).containsExactlyElementsOf(map.values());
assertThat(rmap.readAllValues()).containsExactlyElementsOf(map.values());
assertThat(rmap.entrySet()).containsExactlyElementsOf(map.entrySet());
assertThat(rmap.readAllEntrySet()).containsExactlyElementsOf(map.entrySet());
}
@Test
public void testCacheValues() {
final RMapCache<String, String> map = redisson.getMapCache("testRMapCacheValues");
@ -404,24 +275,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testGetAllBig() {
Map<Integer, String> joinMap = new HashMap<Integer, String>();
for (int i = 0; i < 10000; i++) {
joinMap.put(i, "" + i);
}
RMap<Integer, String> map = redisson.getMapCache("simple");
map.putAll(joinMap);
Map<Integer, String> s = map.getAll(joinMap.keySet());
assertThat(s).isEqualTo(joinMap);
assertThat(map.size()).isEqualTo(joinMap.size());
}
@Test
public void testGetAll() throws InterruptedException {
public void testGetAllTTL() throws InterruptedException {
RMapCache<Integer, Integer> map = redisson.getMapCache("getAll");
map.put(1, 100);
map.put(2, 200, 1, TimeUnit.SECONDS);
@ -494,70 +348,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
Assert.assertEquals(0, cache.size());
}
@Test
public void testIteratorRemoveHighVolume() throws InterruptedException {
RMapCache<Integer, Integer> map = redisson.getMapCache("simpleMap");
for (int i = 0; i < 10000; i++) {
map.put(i, i*10);
}
int cnt = 0;
Iterator<Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Integer, Integer> entry = iterator.next();
iterator.remove();
cnt++;
}
Assert.assertEquals(10000, cnt);
assertThat(map).isEmpty();
Assert.assertEquals(0, map.size());
}
@Test
public void testIteratorRandomRemoveFirst() throws InterruptedException {
RMapCache<Integer, Integer> map = redisson.getMapCache("simpleMap");
for (int i = 0; i < 1000; i++) {
map.put(i, i*10);
}
int cnt = 0;
int removed = 0;
Iterator<Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Integer, Integer> entry = iterator.next();
if (cnt < 20) {
iterator.remove();
removed++;
}
cnt++;
}
Assert.assertEquals(1000, cnt);
assertThat(map.size()).isEqualTo(cnt - removed);
}
@Test
public void testIteratorRandomRemoveHighVolume() throws InterruptedException {
RMapCache<Integer, Integer> map = redisson.getMapCache("simpleMap");
for (int i = 0; i < 10000; i++) {
map.put(i, i*10);
}
int cnt = 0;
int removed = 0;
Iterator<Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Integer, Integer> entry = iterator.next();
if (ThreadLocalRandom.current().nextBoolean()) {
iterator.remove();
removed++;
}
cnt++;
}
Assert.assertEquals(10000, cnt);
assertThat(map.size()).isEqualTo(cnt - removed);
}
@Test
public void testClearExpire() throws InterruptedException {
RMapCache<String, String> cache = redisson.getMapCache("simple");
@ -586,49 +377,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
assertThat(map.entrySet()).containsAll(expected.entrySet());
assertThat(map).hasSize(3);
}
@Test
public void testRemove() {
Map<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
map.remove(new SimpleKey("33"));
map.remove(new SimpleKey("5"));
Assert.assertEquals(1, map.size());
}
@Test
public void testPutAllBig() {
Map<Integer, String> joinMap = new HashMap<Integer, String>();
for (int i = 0; i < 100000; i++) {
joinMap.put(i, "" + i);
}
Map<Integer, String> map = redisson.getMapCache("simple");
map.putAll(joinMap);
assertThat(map.size()).isEqualTo(joinMap.size());
}
@Test
public void testPutAll() {
Map<Integer, String> map = redisson.getMapCache("simple");
map.put(1, "1");
map.put(2, "2");
map.put(3, "3");
Map<Integer, String> joinMap = new HashMap<Integer, String>();
joinMap.put(4, "4");
joinMap.put(5, "5");
joinMap.put(6, "6");
map.putAll(joinMap);
assertThat(map.keySet()).containsOnly(1, 2, 3, 4, 5, 6);
}
@Test
public void testKeySet() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple03");
@ -664,7 +413,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testKeySetByPattern() {
public void testKeySetByPatternTTL() {
RMapCache<String, String> map = redisson.getMapCache("simple", StringCodec.INSTANCE);
map.put("10", "100");
map.put("20", "200", 1, TimeUnit.MINUTES);
@ -676,7 +425,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testValuesByPattern() {
public void testValuesByPatternTTL() {
RMapCache<String, String> map = redisson.getMapCache("simple", StringCodec.INSTANCE);
map.put("10", "100");
map.put("20", "200", 1, TimeUnit.MINUTES);
@ -688,7 +437,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testEntrySetByPattern() {
public void testEntrySetByPatternTTL() {
RMapCache<String, String> map = redisson.getMapCache("simple", StringCodec.INSTANCE);
map.put("10", "100");
map.put("20", "200", 1, TimeUnit.MINUTES);
@ -701,8 +450,8 @@ public class RedissonMapCacheTest extends BaseMapTest {
@Test
public void testContainsValue() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple01", new MsgPackJacksonCodec());
public void testContainsValueTTL() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple01");
Assert.assertFalse(map.containsValue(new SimpleValue("34")));
map.put(new SimpleKey("33"), new SimpleValue("44"), 1, TimeUnit.SECONDS);
@ -715,7 +464,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testContainsKey() throws InterruptedException {
public void testContainsKeyTTL() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple30");
map.put(new SimpleKey("33"), new SimpleValue("44"), 1, TimeUnit.SECONDS);
@ -728,7 +477,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testRemoveValue() {
public void testRemoveValueTTL() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS);
@ -739,23 +488,16 @@ public class RedissonMapCacheTest extends BaseMapTest {
Assert.assertNull(val1);
Assert.assertEquals(0, map.size());
}
@Test
public void testRemoveValueTTL() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS);
map.put(new SimpleKey("3"), new SimpleValue("4"), 1, TimeUnit.SECONDS);
Thread.sleep(1000);
boolean res = map.remove(new SimpleKey("1"), new SimpleValue("2"));
Assert.assertFalse(res);
assertThat(map.remove(new SimpleKey("3"), new SimpleValue("4"))).isFalse();
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertNull(val1);
assertThat(map.get(new SimpleKey("3"))).isNull();
}
@Test
public void testRemoveValueFail() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
@ -799,18 +541,6 @@ public class RedissonMapCacheTest extends BaseMapTest {
Assert.assertEquals("3", val1.getValue());
}
@Test
public void testReplaceValue() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3"));
Assert.assertEquals("2", res.getValue());
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
}
@Test
public void testReplaceValueTTL() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
@ -825,24 +555,9 @@ public class RedissonMapCacheTest extends BaseMapTest {
assertThat(val1).isNull();
}
@Test
public void testReplace() {
Map<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
SimpleValue val1 = map.get(new SimpleKey("33"));
Assert.assertEquals("44", val1.getValue());
map.put(new SimpleKey("33"), new SimpleValue("abc"));
SimpleValue val2 = map.get(new SimpleKey("33"));
Assert.assertEquals("abc", val2.getValue());
}
@Test
public void testScheduler() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple3", new MsgPackJacksonCodec());
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple3");
Assert.assertNull(map.get(new SimpleKey("33")));
map.put(new SimpleKey("33"), new SimpleValue("44"), 5, TimeUnit.SECONDS);
@ -858,8 +573,8 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testPutGet() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple04", new MsgPackJacksonCodec());
public void testPutGetTTL() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple04");
Assert.assertNull(map.get(new SimpleKey("33")));
map.put(new SimpleKey("33"), new SimpleValue("44"), 2, TimeUnit.SECONDS);
@ -880,7 +595,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testPutIfAbsent() throws Exception {
public void testPutIfAbsentTTL() throws Exception {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
SimpleKey key = new SimpleKey("1");
SimpleValue value = new SimpleValue("2");
@ -907,79 +622,7 @@ public class RedissonMapCacheTest extends BaseMapTest {
}
@Test
public void testSize() {
Map<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertEquals(3, map.size());
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
Assert.assertEquals(3, map.size());
map.put(new SimpleKey("1"), new SimpleValue("21"));
map.put(new SimpleKey("3"), new SimpleValue("41"));
Assert.assertEquals(3, map.size());
map.put(new SimpleKey("51"), new SimpleValue("6"));
Assert.assertEquals(4, map.size());
map.remove(new SimpleKey("3"));
Assert.assertEquals(3, map.size());
}
@Test
public void testEmptyRemove() {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
Assert.assertFalse(map.remove(1, 3));
map.put(4, 5);
Assert.assertTrue(map.remove(4, 5));
}
@Test
public void testPutAsync() throws InterruptedException, ExecutionException {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
RFuture<Integer> future = map.putAsync(2, 3);
Assert.assertNull(future.get());
Assert.assertEquals((Integer) 3, map.get(2));
RFuture<Integer> future1 = map.putAsync(2, 4);
Assert.assertEquals((Integer) 3, future1.get());
Assert.assertEquals((Integer) 4, map.get(2));
}
@Test
public void testRemoveAsync() throws InterruptedException, ExecutionException {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
map.put(1, 3);
map.put(3, 5);
map.put(7, 8);
Assert.assertEquals((Integer) 3, map.removeAsync(1).get());
Assert.assertEquals((Integer) 5, map.removeAsync(3).get());
Assert.assertNull(map.removeAsync(10).get());
Assert.assertEquals((Integer) 8, map.removeAsync(7).get());
}
@Test
public void testFastRemoveAsync() throws InterruptedException, ExecutionException {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
map.put(1, 3);
map.put(3, 5);
map.put(4, 6);
map.put(7, 8);
Assert.assertEquals((Long) 3L, map.fastRemoveAsync(1, 3, 7).get());
Thread.sleep(1);
Assert.assertEquals(1, map.size());
}
@Test
public void testFastPutIfAbsent() throws Exception {
public void testFastPutIfAbsentTTL() throws Exception {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
SimpleKey key = new SimpleKey("1");
SimpleValue value = new SimpleValue("2");
@ -1152,17 +795,6 @@ public class RedissonMapCacheTest extends BaseMapTest {
map.removeListener(createListener1);
}
@Test
public void testFastPut() throws Exception {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
Assert.assertTrue(map.fastPut(1, 2));
assertThat(map.get(1)).isEqualTo(2);
Assert.assertFalse(map.fastPut(1, 3));
assertThat(map.get(1)).isEqualTo(3);
Assert.assertEquals(1, map.size());
}
@Test
public void testIdle() throws InterruptedException {
testIdleExpiration(map -> {
@ -1271,24 +903,6 @@ public class RedissonMapCacheTest extends BaseMapTest {
map.clear();
}
@Test
public void testEquals() {
RMapCache<String, String> map = redisson.getMapCache("simple");
map.put("1", "7");
map.put("2", "4");
map.put("3", "5");
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("1", "7");
testMap.put("2", "4");
testMap.put("3", "5");
Assert.assertEquals(testMap, map);
Assert.assertEquals(testMap.hashCode(), map.hashCode());
}
@Test
public void testExpireOverwrite() throws InterruptedException, ExecutionException {
RMapCache<String, Integer> set = redisson.getMapCache("simple");
@ -1306,34 +920,6 @@ public class RedissonMapCacheTest extends BaseMapTest {
Assert.assertFalse(set.containsKey("123"));
}
@Test
public void testFastRemoveEmpty() throws Exception {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
map.put(1, 3);
Assert.assertEquals(0, map.fastRemove());
Assert.assertEquals(1, map.size());
}
@Test(timeout = 5000)
public void testDeserializationErrorReturnsErrorImmediately() throws Exception {
redisson.getConfig().setCodec(new JsonJacksonCodec());
RMapCache<String, SimpleObjectWithoutDefaultConstructor> map = redisson.getMapCache("deserializationFailure");
SimpleObjectWithoutDefaultConstructor object = new SimpleObjectWithoutDefaultConstructor("test-val");
Assert.assertEquals("test-val", object.getTestField());
map.put("test-key", object);
try {
map.get("test-key");
Assert.fail("Expected exception from map.get() call");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testRMapCacheValues() {
final RMapCache<String, String> map = redisson.getMapCache("testRMapCacheValues");
@ -1351,33 +937,15 @@ public class RedissonMapCacheTest extends BaseMapTest {
assertThat(map.readAllEntrySet()).isEqualTo(map.entrySet());
}
@Test
public void testReadAllValues() {
public void testReadAllValuesTTL() {
final RMapCache<String, String> map = redisson.getMapCache("testRMapCacheAllValues");
map.put("1234", "5678", 1, TimeUnit.MINUTES, 60, TimeUnit.MINUTES);
assertThat(map.readAllValues()).containsOnly("5678");
}
public static class SimpleObjectWithoutDefaultConstructor {
private String testField;
SimpleObjectWithoutDefaultConstructor(String testField) {
this.testField = testField;
}
public String getTestField() {
return testField;
}
public void setTestField(String testField) {
this.testField = testField;
}
}
@Test
public void testAddAndGet() {
public void testAddAndGetTTL() {
RMapCache<String, Object> mapCache = redisson.getMapCache("test_put_if_absent", LongCodec.INSTANCE);
assertThat(mapCache.putIfAbsent("4", 0L, 10000L, TimeUnit.SECONDS)).isNull();
assertThat(mapCache.addAndGet("4", 1L)).isEqualTo(1L);

@ -2,140 +2,34 @@ package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.redisson.api.MapOptions;
import org.redisson.api.RFuture;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.codec.Codec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import net.bytebuddy.utility.RandomString;
public class RedissonMapTest extends BaseMapTest {
public static class SimpleKey implements Serializable {
private String key;
public SimpleKey() {
}
public SimpleKey(String field) {
this.key = field;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Override
public String toString() {
return "key: " + key;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SimpleKey other = (SimpleKey) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return true;
}
@Override
protected <K, V> RMap<K, V> getMap(String name) {
return redisson.getMap(name);
}
public static class SimpleValue implements Serializable {
private String value;
public SimpleValue() {
}
public SimpleValue(String field) {
this.value = field;
}
public void setValue(String field) {
this.value = field;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "value: " + value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SimpleValue other = (SimpleValue) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
@Override
protected <K, V> RMap<K, V> getMap(String name, Codec codec) {
return redisson.getMap(name, codec);
}
@Override
protected <K, V> RMap<K, V> getLoaderTestMap(String name, Map<K, V> map) {
MapOptions<K, V> options = MapOptions.<K, V>defaults().loader(createMapLoader(map));
return redisson.getMap("test", options);
return redisson.getMap("test", options);
}
@Override
@ -143,205 +37,7 @@ public class RedissonMapTest extends BaseMapTest {
MapOptions<K, V> options = MapOptions.<K, V>defaults().writer(createMapWriter(map));
return redisson.getMap("test", options);
}
@Test
public void testAddAndGet() throws InterruptedException {
RMap<Integer, Integer> map = redisson.getMap("getAll");
map.put(1, 100);
Integer res = map.addAndGet(1, 12);
assertThat(res).isEqualTo(112);
res = map.get(1);
assertThat(res).isEqualTo(112);
RMap<Integer, Double> map2 = redisson.getMap("getAll2");
map2.put(1, new Double(100.2));
Double res2 = map2.addAndGet(1, new Double(12.1));
assertThat(res2).isEqualTo(112.3);
res2 = map2.get(1);
assertThat(res2).isEqualTo(112.3);
RMap<String, Integer> mapStr = redisson.getMap("mapStr");
assertThat(mapStr.put("1", 100)).isNull();
assertThat(mapStr.addAndGet("1", 12)).isEqualTo(112);
assertThat(mapStr.get("1")).isEqualTo(112);
}
@Test
public void testValueSize() {
Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("3.2.0") > 0);
RMap<String, String> map = redisson.getMap("getAll");
map.put("1", "1234");
assertThat(map.valueSize("4")).isZero();
assertThat(map.valueSize("1")).isEqualTo(6);
}
@Test
public void testGetAllOrder() {
RMap<Integer, Integer> map = redisson.getMap("getAll");
map.put(1, 100);
map.put(2, 200);
map.put(3, 300);
map.put(4, 400);
map.put(5, 500);
map.put(6, 600);
map.put(7, 700);
map.put(8, 800);
Map<Integer, Integer> filtered = map.getAll(new HashSet<Integer>(Arrays.asList(2, 3, 5, 1, 7, 8)));
Map<Integer, Integer> expectedMap = new LinkedHashMap<Integer, Integer>();
expectedMap.put(1, 100);
expectedMap.put(2, 200);
expectedMap.put(3, 300);
expectedMap.put(5, 500);
expectedMap.put(7, 700);
expectedMap.put(8, 800);
assertThat(filtered.entrySet()).containsExactlyElementsOf(expectedMap.entrySet());
}
@Test
public void testGetAll() {
RMap<Integer, Integer> map = redisson.getMap("getAll");
map.put(1, 100);
map.put(2, 200);
map.put(3, 300);
map.put(4, 400);
Map<Integer, Integer> filtered = map.getAll(new HashSet<Integer>(Arrays.asList(2, 3, 5)));
Map<Integer, Integer> expectedMap = new HashMap<Integer, Integer>();
expectedMap.put(2, 200);
expectedMap.put(3, 300);
assertThat(filtered).isEqualTo(expectedMap);
}
@Test
public void testGetAllWithStringKeys() {
RMap<String, Integer> map = redisson.getMap("getAllStrings");
map.put("A", 100);
map.put("B", 200);
map.put("C", 300);
map.put("D", 400);
Map<String, Integer> filtered = map.getAll(new HashSet<String>(Arrays.asList("B", "C", "E")));
Map<String, Integer> expectedMap = new HashMap<String, Integer>();
expectedMap.put("B", 200);
expectedMap.put("C", 300);
assertThat(filtered).isEqualTo(expectedMap);
}
@Test
public void testStringCodec() {
Config config = createConfig();
config.setCodec(StringCodec.INSTANCE);
RedissonClient redisson = Redisson.create(config);
RMap<String, String> rmap = redisson.getMap("TestRMap01");
rmap.put("A", "1");
rmap.put("B", "2");
Iterator<Map.Entry<String, String>> iterator = rmap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
assertThat(next).isIn(new AbstractMap.SimpleEntry("A", "1"), new AbstractMap.SimpleEntry("B", "2"));
}
redisson.shutdown();
}
@Test
public void testInteger() {
Map<Integer, Integer> map = redisson.getMap("test_int");
map.put(1, 2);
map.put(3, 4);
assertThat(map.size()).isEqualTo(2);
Integer val = map.get(1);
assertThat(val).isEqualTo(2);
Integer val2 = map.get(3);
assertThat(val2).isEqualTo(4);
}
@Test
public void testLong() {
Map<Long, Long> map = redisson.getMap("test_long");
map.put(1L, 2L);
map.put(3L, 4L);
assertThat(map.size()).isEqualTo(2);
Long val = map.get(1L);
assertThat(val).isEqualTo(2);
Long val2 = map.get(3L);
assertThat(val2).isEqualTo(4);
}
@Test
public void testIteratorRemoveHighVolume() throws InterruptedException {
RMap<Integer, Integer> map = redisson.getMap("simpleMap");
for (int i = 0; i < 10000; i++) {
map.put(i, i*10);
}
int cnt = 0;
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Integer integer = iterator.next();
iterator.remove();
cnt++;
}
Assert.assertEquals(10000, cnt);
assertThat(map).isEmpty();
Assert.assertEquals(0, map.size());
}
@Test
public void testIterator() {
RMap<Integer, Integer> rMap = redisson.getMap("123");
int size = 1000;
for (int i = 0; i < size; i++) {
rMap.put(i,i);
}
assertThat(rMap.size()).isEqualTo(1000);
int counter = 0;
for (Integer key : rMap.keySet()) {
counter++;
}
assertThat(counter).isEqualTo(size);
counter = 0;
for (Integer value : rMap.values()) {
counter++;
}
assertThat(counter).isEqualTo(size);
counter = 0;
for (Entry<Integer, Integer> entry : rMap.entrySet()) {
counter++;
}
assertThat(counter).isEqualTo(size);
}
@Test(expected = NullPointerException.class)
public void testNullValue() {
Map<Integer, String> map = redisson.getMap("simple12");
map.put(1, null);
}
@Test(expected = NullPointerException.class)
public void testNullKey() {
Map<Integer, String> map = redisson.getMap("simple12");
map.put(null, "1");
}
@Test
public void testEntrySet() {
@ -377,77 +73,7 @@ public class RedissonMapTest extends BaseMapTest {
String val = map.get(2);
assertThat(val).isEqualTo("33");
}
@Test
public void testRemove() {
Map<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
map.remove(new SimpleKey("33"));
map.remove(new SimpleKey("5"));
assertThat(map.size()).isEqualTo(1);
}
@Test
public void testOrdering() {
Map<String, String> map = new LinkedHashMap<String, String>();
// General player data
map.put("name", "123");
map.put("ip", "4124");
map.put("rank", "none");
map.put("tokens", "0");
map.put("coins", "0");
// Arsenal player statistics
map.put("ar_score", "0");
map.put("ar_gameswon", "0");
map.put("ar_gameslost", "0");
map.put("ar_kills", "0");
map.put("ar_deaths", "0");
RMap<String, String> rmap = redisson.getMap("123");
rmap.putAll(map);
assertThat(rmap.keySet()).containsExactlyElementsOf(map.keySet());
assertThat(rmap.readAllKeySet()).containsExactlyElementsOf(map.keySet());
assertThat(rmap.values()).containsExactlyElementsOf(map.values());
assertThat(rmap.readAllValues()).containsExactlyElementsOf(map.values());
assertThat(rmap.entrySet()).containsExactlyElementsOf(map.entrySet());
assertThat(rmap.readAllEntrySet()).containsExactlyElementsOf(map.entrySet());
}
@Test
public void testWriteTimeout() {
Map<String, String> map = redisson.getMap("simple");
Map<String, String> joinMap = new HashMap<>();
for (int i = 0; i < 200000; i++) {
joinMap.put(RandomString.make(1024), RandomString.make(1024));
}
map.putAll(joinMap);
}
@Test
public void testPutAll() {
Map<Integer, String> map = redisson.getMap("simple");
map.put(1, "1");
map.put(2, "2");
map.put(3, "3");
Map<Integer, String> joinMap = new HashMap<Integer, String>();
joinMap.put(4, "4");
joinMap.put(5, "5");
joinMap.put(6, "6");
map.putAll(joinMap);
assertThat(map.keySet()).containsOnly(1, 2, 3, 4, 5, 6);
}
@Test
public void testKeySet() {
Map<SimpleKey, SimpleValue> map = redisson.getMap("simple");
@ -459,302 +85,6 @@ public class RedissonMapTest extends BaseMapTest {
Assert.assertFalse(map.keySet().contains(new SimpleKey("44")));
}
@Test
public void testKeySetByPattern() {
RMap<String, String> map = redisson.getMap("simple", StringCodec.INSTANCE);
map.put("10", "100");
map.put("20", "200");
map.put("30", "300");
assertThat(map.keySet("?0")).containsExactly("10", "20", "30");
assertThat(map.keySet("1")).isEmpty();
assertThat(map.keySet("10")).containsExactly("10");
}
@Test
public void testValuesByPattern() {
RMap<String, String> map = redisson.getMap("simple", StringCodec.INSTANCE);
map.put("10", "100");
map.put("20", "200");
map.put("30", "300");
assertThat(map.values("?0")).containsExactly("100", "200", "300");
assertThat(map.values("1")).isEmpty();
assertThat(map.values("10")).containsExactly("100");
}
@Test
public void testEntrySetByPattern() {
RMap<String, String> map = redisson.getMap("simple", StringCodec.INSTANCE);
map.put("10", "100");
map.put("20", "200");
map.put("30", "300");
assertThat(map.entrySet("?0")).containsExactly(new AbstractMap.SimpleEntry("10", "100"), new AbstractMap.SimpleEntry("20", "200"), new AbstractMap.SimpleEntry("30", "300"));
assertThat(map.entrySet("1")).isEmpty();
assertThat(map.entrySet("10")).containsExactly(new AbstractMap.SimpleEntry("10", "100"));
}
@Test
public void testReadAllKeySet() {
RMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
assertThat(map.readAllKeySet().size()).isEqualTo(3);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllKeySet()).containsOnlyElementsOf(testMap.keySet());
}
@Test
public void testReadAllKeySetHighAmount() {
RMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
for (int i = 0; i < 1000; i++) {
map.put(new SimpleKey("" + i), new SimpleValue("" + i));
}
assertThat(map.readAllKeySet().size()).isEqualTo(1000);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllKeySet()).containsOnlyElementsOf(testMap.keySet());
}
@Test
public void testReadAllValues() {
RMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
assertThat(map.readAllValues().size()).isEqualTo(3);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllValues()).containsOnlyElementsOf(testMap.values());
}
@Test
public void testContainsValue() {
Map<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertTrue(map.containsValue(new SimpleValue("2")));
Assert.assertFalse(map.containsValue(new SimpleValue("441")));
Assert.assertFalse(map.containsValue(new SimpleKey("5")));
}
@Test
public void testContainsKey() {
Map<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertTrue(map.containsKey(new SimpleKey("33")));
Assert.assertFalse(map.containsKey(new SimpleKey("34")));
}
@Test
public void testRemoveValue() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.remove(new SimpleKey("1"), new SimpleValue("2"));
Assert.assertTrue(res);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertNull(val1);
Assert.assertEquals(0, map.size());
}
@Test
public void testRemoveValueFail() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.remove(new SimpleKey("2"), new SimpleValue("1"));
Assert.assertFalse(res);
boolean res1 = map.remove(new SimpleKey("1"), new SimpleValue("3"));
Assert.assertFalse(res1);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("2", val1.getValue());
}
@Test
public void testReplaceOldValueFail() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("43"), new SimpleValue("31"));
Assert.assertFalse(res);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("2", val1.getValue());
}
@Test
public void testReplaceOldValueSuccess() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertTrue(res);
boolean res1 = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertFalse(res1);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
}
@Test
public void testReplaceValue() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3"));
Assert.assertEquals("2", res.getValue());
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
}
@Test
public void testReplace() {
Map<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
SimpleValue val1 = map.get(new SimpleKey("33"));
Assert.assertEquals("44", val1.getValue());
map.put(new SimpleKey("33"), new SimpleValue("abc"));
SimpleValue val2 = map.get(new SimpleKey("33"));
Assert.assertEquals("abc", val2.getValue());
}
@Test
public void testPutGet() {
Map<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
SimpleValue val1 = map.get(new SimpleKey("33"));
Assert.assertEquals("44", val1.getValue());
SimpleValue val2 = map.get(new SimpleKey("5"));
Assert.assertEquals("6", val2.getValue());
}
@Test
public void testPutIfAbsent() throws Exception {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
SimpleKey key = new SimpleKey("1");
SimpleValue value = new SimpleValue("2");
map.put(key, value);
Assert.assertEquals(value, map.putIfAbsent(key, new SimpleValue("3")));
Assert.assertEquals(value, map.get(key));
SimpleKey key1 = new SimpleKey("2");
SimpleValue value1 = new SimpleValue("4");
Assert.assertNull(map.putIfAbsent(key1, value1));
Assert.assertEquals(value1, map.get(key1));
}
@Test
public void testFastPutIfAbsent() throws Exception {
RMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
SimpleKey key = new SimpleKey("1");
SimpleValue value = new SimpleValue("2");
map.put(key, value);
assertThat(map.fastPutIfAbsent(key, new SimpleValue("3"))).isFalse();
assertThat(map.get(key)).isEqualTo(value);
SimpleKey key1 = new SimpleKey("2");
SimpleValue value1 = new SimpleValue("4");
assertThat(map.fastPutIfAbsent(key1, value1)).isTrue();
assertThat(map.get(key1)).isEqualTo(value1);
}
@Test
public void testSize() {
Map<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertEquals(3, map.size());
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
Assert.assertEquals(3, map.size());
map.put(new SimpleKey("1"), new SimpleValue("21"));
map.put(new SimpleKey("3"), new SimpleValue("41"));
Assert.assertEquals(3, map.size());
map.put(new SimpleKey("51"), new SimpleValue("6"));
Assert.assertEquals(4, map.size());
map.remove(new SimpleKey("3"));
Assert.assertEquals(3, map.size());
}
@Test
public void testEmptyRemove() {
RMap<Integer, Integer> map = redisson.getMap("simple");
Assert.assertFalse(map.remove(1, 3));
map.put(4, 5);
Assert.assertTrue(map.remove(4, 5));
}
@Test
public void testPutAsync() throws InterruptedException, ExecutionException {
RMap<Integer, Integer> map = redisson.getMap("simple");
RFuture<Integer> future = map.putAsync(2, 3);
Assert.assertNull(future.get());
Assert.assertEquals((Integer) 3, map.get(2));
RFuture<Integer> future1 = map.putAsync(2, 4);
Assert.assertEquals((Integer) 3, future1.get());
Assert.assertEquals((Integer) 4, map.get(2));
}
@Test
public void testRemoveAsync() throws InterruptedException, ExecutionException {
RMap<Integer, Integer> map = redisson.getMap("simple");
map.put(1, 3);
map.put(3, 5);
map.put(7, 8);
assertThat(map.removeAsync(1).get()).isEqualTo(3);
assertThat(map.removeAsync(3).get()).isEqualTo(5);
assertThat(map.removeAsync(10).get()).isNull();
assertThat(map.removeAsync(7).get()).isEqualTo(8);
}
@Test
public void testFastRemoveAsync() throws InterruptedException, ExecutionException {
RMap<Integer, Integer> map = redisson.getMap("simple");
map.put(1, 3);
map.put(3, 5);
map.put(4, 6);
map.put(7, 8);
assertThat(map.fastRemoveAsync(1, 3, 7).get()).isEqualTo(3);
Thread.sleep(1);
assertThat(map.size()).isEqualTo(1);
}
@Test
public void testKeyIterator() {
RMap<Integer, Integer> map = redisson.getMap("simple");
@ -775,91 +105,4 @@ public class RedissonMapTest extends BaseMapTest {
assertThat(keys.size()).isEqualTo(0);
}
@Test
public void testValueIterator() {
RMap<Integer, Integer> map = redisson.getMap("simple");
map.put(1, 0);
map.put(3, 5);
map.put(4, 6);
map.put(7, 8);
Collection<Integer> values = map.values();
assertThat(values).containsOnly(0, 5, 6, 8);
for (Iterator<Integer> iterator = map.values().iterator(); iterator.hasNext();) {
Integer value = iterator.next();
if (!values.remove(value)) {
Assert.fail();
}
}
assertThat(values.size()).isEqualTo(0);
}
@Test
public void testFastPut() throws Exception {
RMap<Integer, Integer> map = redisson.getMap("simple");
Assert.assertTrue(map.fastPut(1, 2));
Assert.assertFalse(map.fastPut(1, 3));
Assert.assertEquals(1, map.size());
}
@Test
public void testEquals() {
RMap<String, String> map = redisson.getMap("simple");
map.put("1", "7");
map.put("2", "4");
map.put("3", "5");
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("1", "7");
testMap.put("2", "4");
testMap.put("3", "5");
assertThat(map).isEqualTo(testMap);
assertThat(testMap.hashCode()).isEqualTo(map.hashCode());
}
@Test
public void testFastRemoveEmpty() throws Exception {
RMap<Integer, Integer> map = redisson.getMap("simple");
map.put(1, 3);
assertThat(map.fastRemove()).isZero();
assertThat(map.size()).isEqualTo(1);
}
@Test(timeout = 5000)
public void testDeserializationErrorReturnsErrorImmediately() throws Exception {
redisson.getConfig().setCodec(new JsonJacksonCodec());
RMap<String, SimpleObjectWithoutDefaultConstructor> map = redisson.getMap("deserializationFailure");
SimpleObjectWithoutDefaultConstructor object = new SimpleObjectWithoutDefaultConstructor("test-val");
Assert.assertEquals("test-val", object.getTestField());
map.put("test-key", object);
try {
map.get("test-key");
Assert.fail("Expected exception from map.get() call");
} catch (Exception e) {
e.printStackTrace();
}
}
public static class SimpleObjectWithoutDefaultConstructor {
private String testField;
SimpleObjectWithoutDefaultConstructor(String testField) {
this.testField = testField;
}
public String getTestField() {
return testField;
}
public void setTestField(String testField) {
this.testField = testField;
}
}
}

Loading…
Cancel
Save