Search in sources :

Example 31 with RTransaction

use of org.redisson.api.RTransaction in project redisson by redisson.

the class RedissonBaseTransactionalMapTest method testRollback.

@Test
public void testRollback() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.get("1")).isEqualTo("2");
    assertThat(map.remove("3")).isEqualTo("4");
    assertThat(m.get("3")).isEqualTo("4");
    transaction.rollback();
    assertThat(redisson.getKeys().count()).isEqualTo(1);
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("4");
}
Also used : RTransaction(org.redisson.api.RTransaction) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 32 with RTransaction

use of org.redisson.api.RTransaction in project redisson by redisson.

the class RedissonBaseTransactionalMapTest method testReplace2.

@Test
public void testReplace2() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.replace("3", "4", "10")).isTrue();
    assertThat(map.replace("1", "1", "3")).isFalse();
    assertThat(map.replace("3", "10", "11")).isTrue();
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.size()).isEqualTo(2);
    transaction.commit();
    assertThat(m.size()).isEqualTo(2);
    assertThat(m.get("3")).isEqualTo("11");
    assertThat(m.get("1")).isEqualTo("2");
}
Also used : RTransaction(org.redisson.api.RTransaction) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 33 with RTransaction

use of org.redisson.api.RTransaction in project redisson by redisson.

the class RedissonBaseTransactionalMapTest method testRemove.

@Test
public void testRemove() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.get("1")).isEqualTo("2");
    assertThat(map.remove("3")).isEqualTo("4");
    assertThat(map.remove("3")).isNull();
    assertThat(map.remove("3")).isNull();
    assertThat(m.get("3")).isEqualTo("4");
    transaction.commit();
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isNull();
}
Also used : RTransaction(org.redisson.api.RTransaction) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 34 with RTransaction

use of org.redisson.api.RTransaction in project redisson by redisson.

the class RedissonTransactionalBucketTest method testRollback.

@Test
public void testRollback() {
    RBucket<Object> b = redisson.getBucket("test");
    b.set("1234");
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RBucket<Object> bucket = transaction.getBucket("test");
    assertThat(bucket.get()).isEqualTo("1234");
    assertThat(bucket.getAndDelete()).isEqualTo("1234");
    assertThat(b.get()).isEqualTo("1234");
    transaction.rollback();
    assertThat(redisson.getKeys().count()).isEqualTo(1);
    assertThat(b.get()).isEqualTo("1234");
}
Also used : RTransaction(org.redisson.api.RTransaction) Test(org.junit.jupiter.api.Test) BaseTest(org.redisson.BaseTest)

Example 35 with RTransaction

use of org.redisson.api.RTransaction in project redisson by redisson.

the class RedissonTransactionalBucketTest method testTimeout.

@Test
public void testTimeout() throws InterruptedException {
    RBucket<String> b = redisson.getBucket("test");
    b.set("123");
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults().timeout(3, TimeUnit.SECONDS));
    RBucket<String> bucket = transaction.getBucket("test");
    bucket.set("234");
    Thread.sleep(4000);
    try {
        transaction.commit();
        Assertions.fail();
    } catch (TransactionException e) {
    // skip
    }
    Thread.sleep(1000);
    assertThat(b.get()).isEqualTo("123");
}
Also used : RTransaction(org.redisson.api.RTransaction) Test(org.junit.jupiter.api.Test) BaseTest(org.redisson.BaseTest)

Aggregations

RTransaction (org.redisson.api.RTransaction)49 Test (org.junit.jupiter.api.Test)38 BaseTest (org.redisson.BaseTest)37 Transactional (org.springframework.transaction.annotation.Transactional)9 HashSet (java.util.HashSet)5 RBuckets (org.redisson.api.RBuckets)3 LinkedHashMap (java.util.LinkedHashMap)2 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 TransactionOptions (org.redisson.api.TransactionOptions)1 NoTransactionException (org.springframework.transaction.NoTransactionException)1