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..e276441e17 --- /dev/null +++ b/src/test/java/redis/clients/jedis/commands/unified/pipeline/HashesPipelineCommandsTest.java @@ -0,0 +1,391 @@ +package redis.clients.jedis.commands.unified.pipeline; + +import static redis.clients.jedis.util.AssertUtil.assertPipelineSyncAll; + +import java.util.*; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import redis.clients.jedis.commands.unified.pooled.PooledCommandsTestHelper; + +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 42a1e20b79..82ecc600c8 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 @@ -13,18 +13,18 @@ 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"); return new JedisPooled(nodeInfo, DefaultJedisClientConfig.builder() .protocol(RedisProtocolUtil.getRedisProtocol()).password("foobared").build()); } - 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 5d2ca5518d..110be7e48e 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; @@ -105,4 +106,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); + } + } }