Skip to content

Commit

Permalink
Merge pull request #2086 from sazzad16/pipe-tx-tests
Browse files Browse the repository at this point in the history
Tests for watch and unwatch

- Test: Redis supports UNWATCH within MULTI
- Tests for watch and unwatch while pipelining
- Fix test for brpoplpush

References: 
- #2033 (comment) 
- #2033 (comment) 
- #2033 (comment)
  • Loading branch information
sazzad16 committed Nov 25, 2019
2 parents a64bb5d + 4a1d06b commit 033ad67
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 46 deletions.
71 changes: 45 additions & 26 deletions src/test/java/redis/clients/jedis/tests/PipeliningTest.java
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -57,7 +44,6 @@ public void pipeline() throws UnsupportedEncodingException {
assertEquals(2, results.size());
assertEquals("OK", results.get(0));
assertEquals("bar", results.get(1));

}

@Test
Expand Down Expand Up @@ -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<Object> expect = new ArrayList<>();
List<Object> 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<Object> expect = new ArrayList<>();
List<Object> 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());
}

Expand Down
Expand Up @@ -38,7 +38,6 @@ protected Jedis createJedis() {
Jedis j = new Jedis(hnp);
j.connect();
j.auth("foobared");
j.flushAll();
return j;
}

Expand Down
Expand Up @@ -547,41 +547,46 @@ 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);

assertEquals("a", element);
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));

}
}
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -350,7 +350,6 @@ public void testCloseable() throws IOException {
}
}


@Test
public void testTransactionWithGeneralCommand(){
Transaction t = jedis.multi();
Expand All @@ -369,17 +368,14 @@ public void testTransactionWithGeneralCommand(){
Response<Object> 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();
Expand All @@ -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<Object> res = t.exec();
assertEquals(exp, res);
}

}

0 comments on commit 033ad67

Please sign in to comment.