From 66bc61e193dfdfc8bb141d12013e4e081be760c9 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 8 Feb 2023 21:55:13 +0600 Subject: [PATCH 1/2] Test: Hashes pipeline commands --- .../pipeline/HashesPipelineCommandsTest.java | 415 ++++++++++++++++++ .../pipeline/PipelineCommandsTestBase.java | 28 ++ .../pooled/PooledCommandsTestHelper.java | 8 +- .../redis/clients/jedis/util/AssertUtil.java | 44 ++ 4 files changed, 491 insertions(+), 4 deletions(-) create mode 100644 src/test/java/redis/clients/jedis/commands/unified/pipeline/HashesPipelineCommandsTest.java create mode 100644 src/test/java/redis/clients/jedis/commands/unified/pipeline/PipelineCommandsTestBase.java diff --git a/src/test/java/redis/clients/jedis/commands/unified/pipeline/HashesPipelineCommandsTest.java b/src/test/java/redis/clients/jedis/commands/unified/pipeline/HashesPipelineCommandsTest.java new file mode 100644 index 0000000000..88cfea96ae --- /dev/null +++ b/src/test/java/redis/clients/jedis/commands/unified/pipeline/HashesPipelineCommandsTest.java @@ -0,0 +1,415 @@ +package redis.clients.jedis.commands.unified.pipeline; + +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 static redis.clients.jedis.util.AssertUtil.assertPipelineSyncAll; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import redis.clients.jedis.commands.unified.pooled.PooledCommandsTestHelper; +import redis.clients.jedis.params.ScanParams; +import redis.clients.jedis.resps.ScanResult; +import redis.clients.jedis.util.JedisByteHashMap; + +public class HashesPipelineCommandsTest extends PipelineCommandsTestBase { + + @BeforeClass + public static void prepare() throws InterruptedException { + jedis = PooledCommandsTestHelper.getPooled(); + } + + @AfterClass + public static void cleanUp() { + jedis.close(); + } +// +// @Before +// public void setUp() { +// PooledCommandsTestHelper.clearData(); +// } +// +// @After +// public void tearDown() { +// PooledCommandsTestHelper.clearData(); +// } + + final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 }; + final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 }; + final byte[] bcar = { 0x09, 0x0A, 0x0B, 0x0C }; + + final byte[] bbar1 = { 0x05, 0x06, 0x07, 0x08, 0x0A }; + final byte[] bbar2 = { 0x05, 0x06, 0x07, 0x08, 0x0B }; + final byte[] bbar3 = { 0x05, 0x06, 0x07, 0x08, 0x0C }; + final byte[] bbarstar = { 0x05, 0x06, 0x07, 0x08, '*' }; + + @Test + public void hset() { + pipe.hset("foo", "bar", "car"); + pipe.hset("foo", "bar", "foo"); + + // Binary + pipe.hset(bfoo, bbar, bcar); + pipe.hset(bfoo, bbar, bfoo); + + assertPipelineSyncAll( + Arrays.asList(1L, 0L, 1L, 0L), + pipe.syncAndReturnAll()); + } + + @Test + public void hget() { + pipe.hset("foo", "bar", "car"); + pipe.hget("bar", "foo"); + pipe.hget("foo", "car"); + pipe.hget("foo", "bar"); + + // Binary + pipe.hset(bfoo, bbar, bcar); + pipe.hget(bbar, bfoo); + pipe.hget(bfoo, bcar); + pipe.hget(bfoo, bbar); + + assertPipelineSyncAll( + Arrays.asList( + 1L, null, null, "car", + 1L, null, null, bcar), + pipe.syncAndReturnAll()); + } + + @Test + public void hsetnx() { + pipe.hsetnx("foo", "bar", "car"); + pipe.hget("foo", "bar"); + + pipe.hsetnx("foo", "bar", "foo"); + pipe.hget("foo", "bar"); + + pipe.hsetnx("foo", "car", "bar"); + pipe.hget("foo", "car"); + + // Binary + pipe.hsetnx(bfoo, bbar, bcar); + pipe.hget(bfoo, bbar); + + pipe.hsetnx(bfoo, bbar, bfoo); + pipe.hget(bfoo, bbar); + + pipe.hsetnx(bfoo, bcar, bbar); + pipe.hget(bfoo, bcar); + + assertPipelineSyncAll( + Arrays.asList( + 1L, "car", 0L, "car", 1L, "bar", + 1L, bcar, 0L, bcar, 1L, bbar), + pipe.syncAndReturnAll()); + } + + @Test + public void hmset() { + Map hash = new HashMap<>(); + hash.put("bar", "car"); + hash.put("car", "bar"); + pipe.hmset("foo", hash); + pipe.hget("foo", "bar"); + pipe.hget("foo", "car"); + + // Binary + Map bhash = new HashMap<>(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + pipe.hmset(bfoo, bhash); + pipe.hget(bfoo, bbar); + pipe.hget(bfoo, bcar); + + assertPipelineSyncAll( + Arrays.asList("OK", "car", "bar", "OK", bcar, bbar), + pipe.syncAndReturnAll()); + } + + @Test + public void hsetVariadic() { + Map hash = new HashMap<>(); + hash.put("bar", "car"); + hash.put("car", "bar"); + pipe.hset("foo", hash); + pipe.hget("foo", "bar"); + pipe.hget("foo", "car"); + + // Binary + Map bhash = new HashMap<>(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + pipe.hset(bfoo, bhash); + pipe.hget(bfoo, bbar); + pipe.hget(bfoo, bcar); + + assertPipelineSyncAll( + Arrays.asList(2L, "car", "bar", 2L, bcar, bbar), + pipe.syncAndReturnAll()); + } + + @Test + public void hmget() { + Map hash = new HashMap<>(); + hash.put("bar", "car"); + hash.put("car", "bar"); + pipe.hmset("foo", hash); + + pipe.hmget("foo", "bar", "car", "foo"); + List expected = new ArrayList<>(); + expected.add("car"); + expected.add("bar"); + expected.add(null); + + // Binary + Map bhash = new HashMap<>(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + pipe.hmset(bfoo, bhash); + + pipe.hmget(bfoo, bbar, bcar, bfoo); + List bexpected = new ArrayList<>(); + bexpected.add(bcar); + bexpected.add(bbar); + bexpected.add(null); + + assertPipelineSyncAll( + Arrays.asList( + "OK", Arrays.asList("car", "bar", null), + "OK", Arrays.asList(bcar, bbar, null)), + pipe.syncAndReturnAll()); + } + + @Test + public void hincrBy() { + pipe.hincrBy("foo", "bar", 1); + pipe.hincrBy("foo", "bar", -1); + pipe.hincrBy("foo", "bar", -10); + + // Binary + pipe.hincrBy(bfoo, bbar, 1); + pipe.hincrBy(bfoo, bbar, -1); + pipe.hincrBy(bfoo, bbar, -10); + + assertPipelineSyncAll( + Arrays.asList(1L, 0L, -10L, 1L, 0L, -10L), + pipe.syncAndReturnAll()); + } + + @Test + public void hincrByFloat() { + pipe.hincrByFloat("foo", "bar", 1.5d); + pipe.hincrByFloat("foo", "bar", -1.5d); + pipe.hincrByFloat("foo", "bar", -10.7d); + + // Binary + pipe.hincrByFloat(bfoo, bbar, 1.5d); + pipe.hincrByFloat(bfoo, bbar, -1.5d); + pipe.hincrByFloat(bfoo, bbar, -10.7d); + + assertPipelineSyncAll( + Arrays.asList(1.5, 0d, -10.7, 1.5, 0d, -10.7), + pipe.syncAndReturnAll()); + } + + @Test + public void hexists() { + Map hash = new HashMap<>(); + hash.put("bar", "car"); + hash.put("car", "bar"); + pipe.hset("foo", hash); + + pipe.hexists("bar", "foo"); + pipe.hexists("foo", "foo"); + pipe.hexists("foo", "bar"); + + // Binary + Map bhash = new HashMap<>(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + pipe.hset(bfoo, bhash); + + pipe.hexists(bbar, bfoo); + pipe.hexists(bfoo, bfoo); + pipe.hexists(bfoo, bbar); + + assertPipelineSyncAll( + Arrays.asList( + 2L, false, false, true, + 2L, false, false, true), + pipe.syncAndReturnAll()); + } + + @Test + public void hdel() { + Map hash = new HashMap<>(); + hash.put("bar", "car"); + hash.put("car", "bar"); + pipe.hset("foo", hash); + + pipe.hdel("bar", "foo"); + pipe.hdel("foo", "foo"); + pipe.hdel("foo", "bar"); + pipe.hget("foo", "bar"); + + // Binary + Map bhash = new HashMap<>(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + pipe.hset(bfoo, bhash); + + pipe.hdel(bbar, bfoo); + pipe.hdel(bfoo, bfoo); + pipe.hdel(bfoo, bbar); + pipe.hget(bfoo, bbar); + + assertPipelineSyncAll( + Arrays.asList( + 2L, 0L, 0L, 1L, null, + 2L, 0L, 0L, 1L, null), + pipe.syncAndReturnAll()); + } + + @Test + public void hlen() { + Map hash = new HashMap<>(); + hash.put("bar", "car"); + hash.put("car", "bar"); + pipe.hset("foo", hash); + + pipe.hlen("bar"); + pipe.hlen("foo"); + + // Binary + Map bhash = new HashMap<>(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + pipe.hset(bfoo, bhash); + + pipe.hlen(bbar); + pipe.hlen(bfoo); + + assertPipelineSyncAll( + Arrays.asList(2L, 0L, 2L, 2L, 0L, 2L), + pipe.syncAndReturnAll()); + } + + @Test + public void hkeys() { + Map hash = new LinkedHashMap<>(); + hash.put("bar", "car"); + hash.put("car", "bar"); + pipe.hset("foo", hash); + + pipe.hkeys("foo"); + Set expected = new LinkedHashSet<>(); + expected.add("bar"); + expected.add("car"); + + // Binary + Map bhash = new LinkedHashMap<>(); + bhash.put(bbar, bcar); + bhash.put(bcar, bbar); + pipe.hset(bfoo, bhash); + + pipe.hkeys(bfoo); + Set bexpected = new LinkedHashSet<>(); + bexpected.add(bbar); + bexpected.add(bcar); + + assertPipelineSyncAll( + Arrays.asList( + 2L, new HashSet<>(Arrays.asList("bar", "car")), + 2L, new HashSet<>(Arrays.asList(bbar, bcar))), + pipe.syncAndReturnAll()); + } + + @Test + public void hvals() { + Map hash = new LinkedHashMap<>(); + hash.put("bar", "car"); + //hash.put("car", "bar"); + pipe.hset("foo", hash); + + pipe.hvals("foo"); + + // Binary + Map bhash = new LinkedHashMap<>(); + bhash.put(bbar, bcar); + //bhash.put(bcar, bbar); + pipe.hset(bfoo, bhash); + + pipe.hvals(bfoo); + + assertPipelineSyncAll( + Arrays.asList( + //2L, Arrays.asList("bar", "car"), + //2L, Arrays.asList(bbar, bcar)), + 1L, Arrays.asList("car"), + 1L, Arrays.asList(bcar)), + pipe.syncAndReturnAll()); + } + + @Test + public void hgetAll() { + Map hash = new HashMap<>(); + hash.put("bar", "car"); + //hash.put("car", "bar"); + pipe.hset("foo", hash); + + pipe.hgetAll("foo"); + + // Binary + Map bhash = new HashMap<>(); + bhash.put(bbar, bcar); + //bhash.put(bcar, bbar); + pipe.hset(bfoo, bhash); + + pipe.hgetAll(bfoo); + +// assertPipelineSyncAll( +// Arrays.asList( +// 1L, hash, +// 1L, bhash), +// pipe.syncAndReturnAll()); + pipe.syncAndReturnAll(); + } + + @Test + public void hstrlen() { + pipe.hstrlen("foo", "key"); + pipe.hset("foo", "key", "value"); + pipe.hstrlen("foo", "key"); + + pipe.hstrlen(bfoo, bbar); + pipe.hset(bfoo, bbar, bcar); + pipe.hstrlen(bfoo, bbar); + + assertPipelineSyncAll( + Arrays.asList(0L, 1L, 5L, 0L, 1L, 4L), + pipe.syncAndReturnAll()); + } +} diff --git a/src/test/java/redis/clients/jedis/commands/unified/pipeline/PipelineCommandsTestBase.java b/src/test/java/redis/clients/jedis/commands/unified/pipeline/PipelineCommandsTestBase.java new file mode 100644 index 0000000000..c162954509 --- /dev/null +++ b/src/test/java/redis/clients/jedis/commands/unified/pipeline/PipelineCommandsTestBase.java @@ -0,0 +1,28 @@ +package redis.clients.jedis.commands.unified.pipeline; + +import org.junit.After; +import org.junit.Before; + +import redis.clients.jedis.JedisPooled; +import redis.clients.jedis.Pipeline; +import redis.clients.jedis.commands.unified.pooled.PooledCommandsTestHelper; + +public abstract class PipelineCommandsTestBase { + + protected static JedisPooled jedis; + protected Pipeline pipe; + + public PipelineCommandsTestBase() { + } + + @Before + public void setUp() { + PooledCommandsTestHelper.clearData(); + pipe = jedis.pipelined(); + } + + @After + public void tearDown() { + pipe.close(); + } +} diff --git a/src/test/java/redis/clients/jedis/commands/unified/pooled/PooledCommandsTestHelper.java b/src/test/java/redis/clients/jedis/commands/unified/pooled/PooledCommandsTestHelper.java index 733945db5f..317cde3f0f 100644 --- a/src/test/java/redis/clients/jedis/commands/unified/pooled/PooledCommandsTestHelper.java +++ b/src/test/java/redis/clients/jedis/commands/unified/pooled/PooledCommandsTestHelper.java @@ -11,16 +11,16 @@ public class PooledCommandsTestHelper { private static Jedis node; - static JedisPooled getPooled() throws InterruptedException { + public static JedisPooled getPooled() throws InterruptedException { node = new Jedis(nodeInfo); node.auth("foobared"); - node.flushAll(); + //node.flushAll(); return new JedisPooled(nodeInfo.getHost(), nodeInfo.getPort(), null, "foobared"); } - static void clearData() { - node.flushDB(); + public static void clearData() { + node.flushAll(); } } diff --git a/src/test/java/redis/clients/jedis/util/AssertUtil.java b/src/test/java/redis/clients/jedis/util/AssertUtil.java index aaf3fff59c..82851d9038 100644 --- a/src/test/java/redis/clients/jedis/util/AssertUtil.java +++ b/src/test/java/redis/clients/jedis/util/AssertUtil.java @@ -7,6 +7,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Set; import org.junit.ComparisonFailure; @@ -86,4 +87,47 @@ public static void assertByteArrayCollectionContainsAll(Collection all, } } + public static void assertPipelineSyncAll(List expected, List actual) { + assertEquals(expected.size(), actual.size()); + for (int n = 0; n < expected.size(); n++) { + Object expObj = expected.get(n); + Object actObj = actual.get(n); + if (expObj instanceof List) { + if (!(actObj instanceof List)) { + throw new ComparisonFailure(n + "'th element is not a list", + expObj.getClass().toString(), actObj.getClass().toString()); + } + assertPipelineSyncAll((List) expObj, (List) actObj); + } else if (expObj instanceof List) { + if (!(actObj instanceof List)) { + throw new ComparisonFailure(n + "'th element is not a list", + expObj.getClass().toString(), actObj.getClass().toString()); + } + assertPipelineSyncAll((List) expObj, (List) actObj); + } else if (expObj instanceof Set) { + if (!(actObj instanceof Set)) { + throw new ComparisonFailure(n + "'th element is not a set", + expObj.getClass().toString(), actObj.getClass().toString()); + } + assertPipelineSyncAllSet((Set) expObj, (Set) actObj); + } else if (expObj instanceof byte[]) { + if (!(actObj instanceof byte[])) { + throw new ComparisonFailure(n + "'th element is not byte array", + expObj.getClass().toString(), actObj.getClass().toString()); + } + assertArrayEquals((byte[]) expObj, (byte[]) actObj); + } else { + assertEquals(n + "'th element mismatched", expObj, actObj); + } + } + } + + private static void assertPipelineSyncAllSet(Set expected, Set actual) { + assertEquals(expected.size(), actual.size()); + if (expected.iterator().next() instanceof byte[]) { + assertByteArraySetEquals((Set) expected, (Set) actual); + } else { + assertEquals(expected, actual); + } + } } From 750bfbb98a9315a6d3d5d195c45e6f8b91b6b9e0 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 8 Feb 2023 22:03:40 +0600 Subject: [PATCH 2/2] remove unnecessary imports --- .../pipeline/HashesPipelineCommandsTest.java | 26 +------------------ 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/src/test/java/redis/clients/jedis/commands/unified/pipeline/HashesPipelineCommandsTest.java b/src/test/java/redis/clients/jedis/commands/unified/pipeline/HashesPipelineCommandsTest.java index 88cfea96ae..e276441e17 100644 --- a/src/test/java/redis/clients/jedis/commands/unified/pipeline/HashesPipelineCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/unified/pipeline/HashesPipelineCommandsTest.java @@ -1,38 +1,14 @@ package redis.clients.jedis.commands.unified.pipeline; -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 static redis.clients.jedis.util.AssertUtil.assertPipelineSyncAll; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import redis.clients.jedis.commands.unified.pooled.PooledCommandsTestHelper; -import redis.clients.jedis.params.ScanParams; -import redis.clients.jedis.resps.ScanResult; -import redis.clients.jedis.util.JedisByteHashMap; public class HashesPipelineCommandsTest extends PipelineCommandsTestBase {