Skip to content

Commit

Permalink
Polishing #3425 and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed May 21, 2023
1 parent 50ec110 commit 293bf1e
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 157 deletions.
1 change: 1 addition & 0 deletions docs/jedis5-breaking.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Following BuilderFactory implementations have been removed:
- `BYTE_ARRAY` (use `BINARY`)
- `BYTE_ARRAY_LIST` (use `BINARY_LIST`)
- `BINARY_MAP_FROM_PAIRS`

<!--- Deprecated in Jedis 4 --->

Expand Down
32 changes: 15 additions & 17 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ public String toString() {
}
};

public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST = new Builder<List<Map.Entry<byte[], byte[]>>>() {
public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST
= new Builder<List<Map.Entry<byte[], byte[]>>>() {
@Override
@SuppressWarnings("unchecked")
public List<Map.Entry<byte[], byte[]>> build(Object data) {
Expand All @@ -261,7 +262,8 @@ public String toString() {
}
};

public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST_FROM_PAIRS = new Builder<List<Map.Entry<byte[], byte[]>>>() {
public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST_FROM_PAIRS
= new Builder<List<Map.Entry<byte[], byte[]>>>() {
@Override
@SuppressWarnings("unchecked")
public List<Map.Entry<byte[], byte[]>> build(Object data) {
Expand Down Expand Up @@ -326,7 +328,8 @@ public String toString() {
@SuppressWarnings("unchecked")
public Set<String> build(Object data) {
if (null == data) return null;
return ((List<Object>) data).stream().map(STRING::build).collect(Collectors.toCollection(LinkedHashSet::new));
return ((List<Object>) data).stream().map(STRING::build)
.collect(Collectors.toCollection(LinkedHashSet::new));
}

@Override
Expand Down Expand Up @@ -355,15 +358,16 @@ public String toString() {
}
};

public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST = new Builder<List<Map.Entry<String, String>>>() {
public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST
= new Builder<List<Map.Entry<String, String>>>() {
@Override
@SuppressWarnings("unchecked")
public List<Map.Entry<String, String>> build(Object data) {
final List<byte[]> flatHash = (List<byte[]>) data;
final List<Map.Entry<String, String>> pairList = new ArrayList<>();
final List<Map.Entry<String, String>> pairList = new ArrayList<>(flatHash.size() / 2);
final Iterator<byte[]> iterator = flatHash.iterator();
while (iterator.hasNext()) {
pairList.add(new AbstractMap.SimpleEntry<>(STRING.build(iterator.next()), STRING.build(iterator.next())));
pairList.add(KeyValue.of(STRING.build(iterator.next()), STRING.build(iterator.next())));
}

return pairList;
Expand All @@ -373,28 +377,22 @@ public List<Map.Entry<String, String>> build(Object data) {
public String toString() {
return "List<Map.Entry<String, String>>";
}

};

public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST_FROM_PAIRS = new Builder<List<Map.Entry<String, String>>>() {
public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST_FROM_PAIRS
= new Builder<List<Map.Entry<String, String>>>() {
@Override
@SuppressWarnings("unchecked")
public List<Map.Entry<String, String>> build(Object data) {
final List<Object> list = (List<Object>) data;
final List<Map.Entry<String, String>> pairList = new ArrayList<>();
for (Object object : list) {
final List<byte[]> flat = (List<byte[]>) object;
pairList.add(new AbstractMap.SimpleEntry<>(SafeEncoder.encode(flat.get(0)), SafeEncoder.encode(flat.get(1))));
}

return pairList;
return ((List<Object>) data).stream().map(o -> (List<Object>) o)
.map(l -> KeyValue.of(STRING.build(l.get(0)), STRING.build(l.get(1))))
.collect(Collectors.toList());
}

@Override
public String toString() {
return "List<Map.Entry<String, String>>";
}

};

public static final Builder<KeyedListElement> KEYED_LIST_ELEMENT = new Builder<KeyedListElement>() {
Expand Down
16 changes: 9 additions & 7 deletions src/test/java/redis/clients/jedis/ClusterPipeliningTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -20,6 +19,7 @@
import redis.clients.jedis.resps.GeoRadiusResponse;
import redis.clients.jedis.resps.StreamEntry;
import redis.clients.jedis.resps.Tuple;
import redis.clients.jedis.util.AssertUtil;
import redis.clients.jedis.util.JedisClusterTestUtil;
import redis.clients.jedis.util.SafeEncoder;

Expand Down Expand Up @@ -650,10 +650,11 @@ public void clusterPipelineHash() {
Response<List<String>> r11 = p.hvals("mynewhash");
Response<List<String>> r12 = p.hmget("myhash", "field1", "field2");
Response<String> r13 = p.hrandfield("myotherhash");
Response<List<String>> r14 = p.hrandfield("myotherhash", 2);
Response<List<Map.Entry<String, String>>> r15 = p.hrandfieldWithValues("myotherhash", 2);
Response<List<String>> r14 = p.hrandfield("myotherhash", 4);
Response<List<String>> r15 = p.hrandfield("myotherhash", -4);
Response<Long> r16 = p.hstrlen("myhash", "field1");
Response<List<Map.Entry<String, String>>> r17 = p.hrandfieldWithValues("myotherhash", -2);
Response<List<Map.Entry<String, String>>> r17 = p.hrandfieldWithValues("myotherhash", 4);
Response<List<Map.Entry<String, String>>> r18 = p.hrandfieldWithValues("myotherhash", -4);

p.sync();
assertEquals(Long.valueOf(1), r1.get());
Expand All @@ -668,11 +669,12 @@ public void clusterPipelineHash() {
assertEquals(keys, r10.get());
assertEquals(vals, r11.get());
assertEquals(vals2, r12.get());
assertTrue(hm.keySet().contains(r13.get()));
AssertUtil.assertCollectionContains(hm.keySet(), r13.get());
assertEquals(2, r14.get().size());
Assert.assertTrue(r15.get().contains(new AbstractMap.SimpleEntry<>("field3", "5")));
assertEquals(4, r15.get().size());
assertEquals(Long.valueOf(5), r16.get());
Assert.assertTrue(r17.get().contains(new AbstractMap.SimpleEntry<>("field3", "5")) || r17.get().contains(new AbstractMap.SimpleEntry<>("field2", "2")));
assertEquals(2, r17.get().size());
assertEquals(4, r18.get().size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import static redis.clients.jedis.Protocol.Command.XINFO;
import static redis.clients.jedis.params.ScanParams.SCAN_POINTER_START;
import static redis.clients.jedis.params.ScanParams.SCAN_POINTER_START_BINARY;
import static redis.clients.jedis.params.SetParams.setParams;
import static redis.clients.jedis.util.AssertUtil.assertByteArrayListEquals;
import static redis.clients.jedis.util.AssertUtil.assertCollectionContains;

import java.util.*;
import org.hamcrest.MatcherAssert;
Expand All @@ -38,6 +35,8 @@
import redis.clients.jedis.util.KeyValue;
import redis.clients.jedis.util.SafeEncoder;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.util.AssertUtil;

public class AllKindOfValuesCommandsTest extends JedisCommandsTestBase {
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
Expand Down Expand Up @@ -216,28 +215,20 @@ public void keys() {
jedis.set("foobar", "bar");

Set<String> keys = jedis.keys("foo*");
Set<String> expected = new HashSet<>();
expected.add("foo");
expected.add("foobar");
assertEquals(expected, keys);
AssertUtil.assertCollectionContains(keys, "foo");
AssertUtil.assertCollectionContains(keys, "foobar");

expected = new HashSet<>();
keys = jedis.keys("bar*");

assertEquals(expected, keys);
assertEquals(Collections.emptySet(), jedis.keys("bar*"));

// Binary
jedis.set(bfoo, bbar);
jedis.set(bfoobar, bbar);

Set<byte[]> bkeys = jedis.keys(bfoostar);
assertEquals(2, bkeys.size());
assertCollectionContains(bkeys, bfoo);
assertCollectionContains(bkeys, bfoobar);

bkeys = jedis.keys(bbarstar);
AssertUtil.assertByteArrayCollectionContains(bkeys, bfoo);
AssertUtil.assertByteArrayCollectionContains(bkeys, bfoobar);

assertEquals(0, bkeys.size());
assertEquals(Collections.emptySet(), jedis.keys(bbarstar));
}

@Test
Expand Down Expand Up @@ -880,11 +871,11 @@ public void scanType() {
assertEquals(4, page1Count + page2Count);

binaryResult = jedis.scan(SCAN_POINTER_START_BINARY, noParams, hash);
assertByteArrayListEquals(Collections.singletonList(new byte[]{98}), binaryResult.getResult());
AssertUtil.assertByteArrayListEquals(Collections.singletonList(new byte[]{98}), binaryResult.getResult());
binaryResult = jedis.scan(SCAN_POINTER_START_BINARY, noParams, set);
assertByteArrayListEquals(Collections.singletonList(new byte[]{100}), binaryResult.getResult());
AssertUtil.assertByteArrayListEquals(Collections.singletonList(new byte[]{100}), binaryResult.getResult());
binaryResult = jedis.scan(SCAN_POINTER_START_BINARY, noParams, zset);
assertByteArrayListEquals(Collections.singletonList(new byte[]{102}), binaryResult.getResult());
AssertUtil.assertByteArrayListEquals(Collections.singletonList(new byte[]{102}), binaryResult.getResult());
}

@Test
Expand Down Expand Up @@ -917,10 +908,10 @@ private ScanResult<String> scanCompletely(String cursor) {

@Test
public void setNxExAndGet() {
assertEquals("OK", jedis.set("hello", "world", setParams().nx().ex(expireSeconds)));
assertEquals("OK", jedis.set("hello", "world", SetParams.setParams().nx().ex(expireSeconds)));
assertEquals("world", jedis.get("hello"));

assertNull(jedis.set("hello", "bar", setParams().nx().ex(expireSeconds)));
assertNull(jedis.set("hello", "bar", SetParams.setParams().nx().ex(expireSeconds)));
assertEquals("world", jedis.get("hello"));

long ttl = jedis.ttl("hello");
Expand All @@ -930,10 +921,10 @@ public void setNxExAndGet() {
byte[] bworld = { 0x77, 0x6F, 0x72, 0x6C, 0x64 };
byte[] bhello = { 0x68, 0x65, 0x6C, 0x6C, 0x6F };

assertEquals("OK", jedis.set(bworld, bhello, setParams().nx().ex(expireSeconds)));
assertEquals("OK", jedis.set(bworld, bhello, SetParams.setParams().nx().ex(expireSeconds)));
assertArrayEquals(bhello, jedis.get(bworld));

assertNull(jedis.set(bworld, bbar, setParams().nx().ex(expireSeconds)));
assertNull(jedis.set(bworld, bbar, SetParams.setParams().nx().ex(expireSeconds)));
assertArrayEquals(bhello, jedis.get(bworld));

long bttl = jedis.ttl(bworld);
Expand All @@ -958,12 +949,12 @@ public void setGet() {
assertEquals("OK", jedis.set("hello", "world"));

// GET old value
assertEquals("world", jedis.setGet("hello", "jedis", setParams()));
assertEquals("world", jedis.setGet("hello", "jedis", SetParams.setParams()));

assertEquals("jedis", jedis.get("hello"));

// GET null value
assertNull(jedis.setGet("key", "value", setParams()));
assertNull(jedis.setGet("key", "value", SetParams.setParams()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ public void roleSentinel() {
List<Object> role = sentinel.role();
assertEquals("sentinel", role.get(0));
assertTrue(role.get(1) instanceof List);
assertTrue(((List) role.get(1)).contains("mymaster"));
AssertUtil.assertCollectionContains((List) role.get(1), "mymaster");

// binary
List<Object> brole = sentinel.roleBinary();
assertArrayEquals("sentinel".getBytes(), (byte[]) brole.get(0));
assertTrue(brole.get(1) instanceof List);
AssertUtil.assertCollectionContains((List) brole.get(1), "mymaster".getBytes());
AssertUtil.assertByteArrayCollectionContains((List) brole.get(1), "mymaster".getBytes());
}
}

Expand Down Expand Up @@ -341,7 +341,7 @@ public Long call() throws Exception {

assertThat(latencyRead.get(), Matchers.lessThan(100L));

assertThat(latencyWrite.get(), greaterThan(100L));
assertThat(latencyWrite.get(), Matchers.greaterThan(100L));

} finally {
executorService.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;

import static redis.clients.jedis.params.ScanParams.SCAN_POINTER_START;
import static redis.clients.jedis.params.ScanParams.SCAN_POINTER_START_BINARY;
import static redis.clients.jedis.util.AssertUtil.assertByteArrayListEquals;
import static redis.clients.jedis.util.AssertUtil.assertByteArraySetEquals;
import static redis.clients.jedis.util.AssertUtil.assertCollectionContains;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -28,6 +24,7 @@
import redis.clients.jedis.Response;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;
import redis.clients.jedis.util.AssertUtil;
import redis.clients.jedis.util.JedisByteHashMap;

public class HashesCommandsTest extends JedisCommandsTestBase {
Expand Down Expand Up @@ -149,7 +146,7 @@ public void hmget() {
bexpected.add(bbar);
bexpected.add(null);

assertByteArrayListEquals(bexpected, bvalues);
AssertUtil.assertByteArrayListEquals(bexpected, bvalues);
}

@Test
Expand Down Expand Up @@ -272,7 +269,7 @@ public void hkeys() {
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
bexpected.add(bbar);
bexpected.add(bcar);
assertByteArraySetEquals(bexpected, bkeys);
AssertUtil.assertByteArraySetEquals(bexpected, bkeys);
}

@Test
Expand All @@ -284,8 +281,8 @@ public void hvals() {

List<String> vals = jedis.hvals("foo");
assertEquals(2, vals.size());
assertTrue(vals.contains("bar"));
assertTrue(vals.contains("car"));
AssertUtil.assertCollectionContains(vals, "bar");
AssertUtil.assertCollectionContains(vals, "car");

// Binary
Map<byte[], byte[]> bhash = new LinkedHashMap<byte[], byte[]>();
Expand All @@ -296,8 +293,8 @@ public void hvals() {
List<byte[]> bvals = jedis.hvals(bfoo);

assertEquals(2, bvals.size());
assertCollectionContains(bvals, bbar);
assertCollectionContains(bvals, bcar);
AssertUtil.assertByteArrayCollectionContains(bvals, bbar);
AssertUtil.assertByteArrayCollectionContains(bvals, bcar);
}

@Test
Expand Down Expand Up @@ -459,16 +456,16 @@ public void hrandfield() {
assertEquals(2, jedis.hrandfield("foo", 2).size());

List<Map.Entry<String, String>> actual = jedis.hrandfieldWithValues("foo", 2);
assertNotNull(actual);
assertEquals(2, actual.size());
Map.Entry entry = actual.get(0);
assertEquals(hash.get(entry.getKey()), entry.getValue());
actual.forEach(e -> assertEquals(hash.get(e.getKey()), e.getValue()));

actual = jedis.hrandfieldWithValues("foo", -2);
assertNotNull(actual);
assertEquals(2, actual.size());
entry = actual.get(0);
assertEquals(hash.get(entry.getKey()), entry.getValue());
actual = jedis.hrandfieldWithValues("foo", 5);
assertEquals(3, actual.size());
actual.forEach(e -> assertEquals(hash.get(e.getKey()), e.getValue()));

actual = jedis.hrandfieldWithValues("foo", -5);
assertEquals(5, actual.size());
actual.forEach(e -> assertEquals(hash.get(e.getKey()), e.getValue()));

// binary
assertNull(jedis.hrandfield(bfoo));
Expand All @@ -487,15 +484,15 @@ public void hrandfield() {
assertEquals(2, jedis.hrandfield(bfoo, 2).size());

List<Map.Entry<byte[], byte[]>> bactual = jedis.hrandfieldWithValues(bfoo, 2);
assertNotNull(bactual);
assertEquals(2, bactual.size());
Map.Entry bentry = bactual.get(0);
assertArrayEquals(bhash.get(bentry.getKey()), (byte[]) bentry.getValue());
bactual.forEach(e -> assertArrayEquals(bhash.get(e.getKey()), e.getValue()));

bactual = jedis.hrandfieldWithValues(bfoo, -2);
assertNotNull(bactual);
assertEquals(2, bactual.size());
bentry = bactual.get(0);
assertArrayEquals(bhash.get(bentry.getKey()), (byte[]) bentry.getValue());
bactual = jedis.hrandfieldWithValues(bfoo, 5);
assertEquals(3, bactual.size());
bactual.forEach(e -> assertArrayEquals(bhash.get(e.getKey()), e.getValue()));

bactual = jedis.hrandfieldWithValues(bfoo, -5);
assertEquals(5, bactual.size());
bactual.forEach(e -> assertArrayEquals(bhash.get(e.getKey()), e.getValue()));
}
}
Loading

0 comments on commit 293bf1e

Please sign in to comment.