diff --git a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java index 6e7388745e..7b9b07fa87 100644 --- a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java +++ b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java @@ -11,7 +11,6 @@ import static org.junit.Assert.fail; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -23,32 +22,20 @@ import org.hamcrest.CoreMatchers; import org.hamcrest.Matcher; -import org.junit.Before; import org.junit.Test; -import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.Pipeline; import redis.clients.jedis.Response; import redis.clients.jedis.Tuple; import redis.clients.jedis.exceptions.JedisDataException; +import redis.clients.jedis.tests.commands.JedisCommandTestBase; import redis.clients.jedis.util.SafeEncoder; -public class PipeliningTest { - private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); - - private Jedis jedis; - - @Before - public void setUp() throws Exception { - jedis = new Jedis(hnp.getHost(), hnp.getPort(), 2000); - jedis.connect(); - jedis.auth("foobared"); - jedis.flushAll(); - } +public class PipeliningTest extends JedisCommandTestBase { @Test - public void pipeline() throws UnsupportedEncodingException { + public void pipeline() { Pipeline p = jedis.pipelined(); p.set("foo", "bar"); p.get("foo"); @@ -57,7 +44,6 @@ public void pipeline() throws UnsupportedEncodingException { assertEquals(2, results.size()); assertEquals("OK", results.get(0)); assertEquals("bar", results.get(1)); - } @Test @@ -309,20 +295,53 @@ public void multiWithSync() { } @Test - public void multiWithWatch() { - String key = "foo"; - String val = "bar"; + public void multiWatch() { + final String key = "foo"; + assertEquals(Long.valueOf(5L), jedis.incrBy(key, 5L)); + + List expect = new ArrayList<>(); + List expMulti = null; // MULTI will fail + + Pipeline pipe = jedis.pipelined(); + pipe.watch(key); expect.add("OK"); + pipe.incrBy(key, 3L); expect.add(8L); + pipe.multi(); expect.add("OK"); + pipe.incrBy(key, 6L); expect.add("QUEUED"); + assertEquals(expect, pipe.syncAndReturnAll()); expect.clear(); + + try (Jedis tweak = createJedis()) { + assertEquals(Long.valueOf(10L), tweak.incrBy(key, 2L)); + } + + pipe.incrBy(key, 4L); expect.add("QUEUED"); + pipe.exec(); expect.add(expMulti); // failed MULTI + pipe.incrBy(key, 7L); expect.add(17L); + assertEquals(expect, pipe.syncAndReturnAll()); + } + + @Test + public void multiUnwatch() { + final String key = "foo"; + assertEquals(Long.valueOf(5L), jedis.incrBy(key, 5L)); + List expect = new ArrayList<>(); List expMulti = new ArrayList<>(); Pipeline pipe = jedis.pipelined(); - pipe.set(key, val); expect.add("OK"); - pipe.watch(key); expect.add("OK"); - pipe.multi(); expect.add("OK"); - pipe.unwatch(); expect.add("QUEUED"); expMulti.add("OK"); - pipe.get(key); expect.add("QUEUED"); expMulti.add(val); - pipe.exec(); expect.add(expMulti); + pipe.watch(key); expect.add("OK"); + pipe.incrBy(key, 3L); expect.add(8L); + pipe.unwatch(); expect.add("OK"); + pipe.multi(); expect.add("OK"); + pipe.incrBy(key, 6L); expect.add("QUEUED"); expMulti.add(16L); + assertEquals(expect, pipe.syncAndReturnAll()); expect.clear(); + + try (Jedis tweak = createJedis()) { + assertEquals(Long.valueOf(10L), tweak.incrBy(key, 2L)); + } + pipe.incrBy(key, 4L); expect.add("QUEUED"); expMulti.add(20L); + pipe.exec(); expect.add(expMulti); // successful MULTI + pipe.incrBy(key, 7L); expect.add(27L); assertEquals(expect, pipe.syncAndReturnAll()); } diff --git a/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java b/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java index 0ceaeddc8f..8b018a3ee8 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java +++ b/src/test/java/redis/clients/jedis/tests/commands/JedisCommandTestBase.java @@ -38,7 +38,6 @@ protected Jedis createJedis() { Jedis j = new Jedis(hnp); j.connect(); j.auth("foobared"); - j.flushAll(); return j; } diff --git a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java index e9cade776d..5bb276c729 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java @@ -547,17 +547,19 @@ public void linsert() { @Test public void brpoplpush() { - (new Thread(new Runnable() { + + new Thread(new Runnable() { + @Override public void run() { try { Thread.sleep(100); Jedis j = createJedis(); j.lpush("foo", "a"); } catch (InterruptedException e) { - e.printStackTrace(); + org.apache.logging.log4j.LogManager.getLogger().error("Interruption in string lpush", e); } } - })).start(); + }).start(); String element = jedis.brpoplpush("foo", "bar", 0); @@ -565,23 +567,26 @@ public void run() { assertEquals(1, jedis.llen("bar").longValue()); assertEquals("a", jedis.lrange("bar", 0, -1).get(0)); - (new Thread(new Runnable() { + // Binary + + new Thread(new Runnable() { + @Override public void run() { try { Thread.sleep(100); Jedis j = createJedis(); - j.lpush("foo", "a"); + j.lpush(bfoo, bA); } catch (InterruptedException e) { - e.printStackTrace(); + org.apache.logging.log4j.LogManager.getLogger().error("Interruption in binary lpush", e); } } - })).start(); + }).start(); - byte[] brpoplpush = jedis.brpoplpush("foo".getBytes(), "bar".getBytes(), 0); + byte[] belement = jedis.brpoplpush(bfoo, bbar, 0); - assertTrue(Arrays.equals("a".getBytes(), brpoplpush)); + assertArrayEquals(bA, belement); assertEquals(1, jedis.llen("bar").longValue()); - assertEquals("a", jedis.lrange("bar", 0, -1).get(0)); + assertArrayEquals(bA, jedis.lrange(bbar, 0, -1).get(0)); } } diff --git a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java index ed3749c303..f65351255c 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java @@ -110,10 +110,10 @@ public void watch() throws UnknownHostException, IOException { } @Test - public void unwatch() throws UnknownHostException, IOException { + public void unwatch() { jedis.watch("mykey"); - String val = jedis.get("mykey"); - val = "foo"; + jedis.get("mykey"); + String val = "foo"; String status = jedis.unwatch(); assertEquals("OK", status); Transaction t = jedis.multi(); @@ -130,8 +130,8 @@ public void unwatch() throws UnknownHostException, IOException { // Binary jedis.watch(bmykey); - byte[] bval = jedis.get(bmykey); - bval = bfoo; + jedis.get(bmykey); + byte[] bval = bfoo; status = jedis.unwatch(); assertEquals(Keyword.OK.name(), status); t = jedis.multi(); @@ -350,7 +350,6 @@ public void testCloseable() throws IOException { } } - @Test public void testTransactionWithGeneralCommand(){ Transaction t = jedis.multi(); @@ -369,17 +368,14 @@ public void testTransactionWithGeneralCommand(){ Response x = t.sendCommand(GET, "x"); t.exec(); - assertEquals("foo", string.get()); assertEquals("foo", list.get()); assertEquals("bar", hash.get()); assertEquals("foo", zset.get().iterator().next()); assertEquals("foo", set.get()); assertEquals("2", SafeEncoder.encode((byte[]) x.get())); - } - @Test public void transactionResponseWithErrorWithGeneralCommand() { Transaction t = jedis.multi(); @@ -401,4 +397,20 @@ public void transactionResponseWithErrorWithGeneralCommand() { assertEquals("1", SafeEncoder.encode((byte[]) x.get())); } + @Test + public void unwatchWithinMulti() { + final String key = "foo"; + final String val = "bar"; + jedis.set(key, val); + jedis.watch(key); + + List exp = new ArrayList(); + Transaction t = jedis.multi(); + t.get(key); exp.add(val); + t.unwatch(); exp.add("OK"); + t.get(key); exp.add(val); + List res = t.exec(); + assertEquals(exp, res); + } + } \ No newline at end of file