Search in sources :

Example 81 with Transaction

use of redis.clients.jedis.Transaction in project jedis by xetorthio.

the class TransactionCommandsTest method transactionResponseBinary.

@Test
public void transactionResponseBinary() {
    jedis.set("string", "foo");
    jedis.lpush("list", "foo");
    jedis.hset("hash", "foo", "bar");
    jedis.zadd("zset", 1, "foo");
    jedis.sadd("set", "foo");
    Transaction t = jedis.multi();
    Response<byte[]> string = t.get("string".getBytes());
    Response<byte[]> list = t.lpop("list".getBytes());
    Response<byte[]> hash = t.hget("hash".getBytes(), "foo".getBytes());
    Response<Set<byte[]>> zset = t.zrange("zset".getBytes(), 0, -1);
    Response<byte[]> set = t.spop("set".getBytes());
    t.exec();
    assertArrayEquals("foo".getBytes(), string.get());
    assertArrayEquals("foo".getBytes(), list.get());
    assertArrayEquals("bar".getBytes(), hash.get());
    assertArrayEquals("foo".getBytes(), zset.get().iterator().next());
    assertArrayEquals("foo".getBytes(), set.get());
}
Also used : Set(java.util.Set) Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 82 with Transaction

use of redis.clients.jedis.Transaction in project jedis by xetorthio.

the class TransactionCommandsTest method testCloseable.

@Test
public void testCloseable() throws IOException {
    // we need to test with fresh instance of Jedis
    Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
    jedis2.auth("foobared");
    Transaction transaction = jedis2.multi();
    transaction.set("a", "1");
    transaction.set("b", "2");
    transaction.close();
    try {
        transaction.exec();
        fail("close should discard transaction");
    } catch (JedisDataException e) {
        assertTrue(e.getMessage().contains("EXEC without MULTI"));
    // pass
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Example 83 with Transaction

use of redis.clients.jedis.Transaction in project jedis by xetorthio.

the class TransactionCommandsTest method execGetResponse.

@Test
public void execGetResponse() {
    Transaction t = jedis.multi();
    t.set("foo", "bar");
    t.smembers("foo");
    t.get("foo");
    List<Response<?>> lr = t.execGetResponse();
    try {
        lr.get(1).get();
        fail("We expect exception here!");
    } catch (JedisDataException e) {
    // that is fine we should be here
    }
    assertEquals("bar", lr.get(2).get());
}
Also used : Response(redis.clients.jedis.Response) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Example 84 with Transaction

use of redis.clients.jedis.Transaction in project jedis by xetorthio.

the class TransactionCommandsTest method testResetStateWhenInWatch.

@Test
public void testResetStateWhenInWatch() {
    jedis.watch("mykey", "somekey");
    // state reset : unwatch
    jedis.resetState();
    Transaction t = jedis.multi();
    nj.connect();
    nj.auth("foobared");
    nj.set("mykey", "bar");
    nj.disconnect();
    t.set("mykey", "foo");
    List<Object> resp = t.exec();
    assertNotNull(resp);
    assertEquals(1, resp.size());
    assertEquals("foo", jedis.get("mykey"));
}
Also used : Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 85 with Transaction

use of redis.clients.jedis.Transaction in project jedis by xetorthio.

the class TransactionCommandsTest method transactionResponseWithError.

@Test
public void transactionResponseWithError() {
    Transaction t = jedis.multi();
    t.set("foo", "bar");
    Response<Set<String>> error = t.smembers("foo");
    Response<String> r = t.get("foo");
    List<Object> l = t.exec();
    assertEquals(JedisDataException.class, l.get(1).getClass());
    try {
        error.get();
        fail("We expect exception here!");
    } catch (JedisDataException e) {
    // that is fine we should be here
    }
    assertEquals(r.get(), "bar");
}
Also used : Set(java.util.Set) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Aggregations

Transaction (redis.clients.jedis.Transaction)140 Test (org.junit.Test)84 Jedis (redis.clients.jedis.Jedis)56 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)16 Set (java.util.Set)14 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)8 List (java.util.List)8 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)8 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)6 Protocol (redis.clients.jedis.Protocol)4 Response (redis.clients.jedis.Response)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 HashMap (java.util.HashMap)3 Connection (redis.clients.jedis.Connection)3 JedisPool (redis.clients.jedis.JedisPool)3 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)3 RedisException (io.seata.common.exception.RedisException)2 StoreException (io.seata.common.exception.StoreException)2 LinkedHashMap (java.util.LinkedHashMap)2