Search in sources :

Example 26 with RTransaction

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

the class RedissonBaseTransactionalMapTest method testPutRemove.

@Test
public void testPutRemove() {
    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.put("3", "5")).isNull();
    assertThat(map.get("3")).isEqualTo("5");
    assertThat(m.get("3")).isEqualTo("4");
    transaction.commit();
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("5");
}
Also used : RTransaction(org.redisson.api.RTransaction) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 27 with RTransaction

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

the class RedissonBaseTransactionalMapTest method testPutIfAbsentRemove.

@Test
public void testPutIfAbsentRemove() {
    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.putIfAbsent("3", "2")).isEqualTo("4");
    assertThat(map.putIfAbsent("5", "6")).isNull();
    assertThat(map.putIfAbsent("5", "7")).isEqualTo("6");
    assertThat(map.remove("5")).isEqualTo("6");
    assertThat(map.putIfAbsent("5", "8")).isNull();
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.size()).isEqualTo(2);
    transaction.commit();
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isEqualTo("8");
}
Also used : RTransaction(org.redisson.api.RTransaction) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 28 with RTransaction

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

the class RedissonBaseTransactionalMapTest method testParallel.

@Test
public void testParallel() {
    Assertions.assertThrows(TransactionTimeoutException.class, () -> {
        RMap<Integer, String> m = redisson.getMap("test");
        m.put(1, "test");
        RTransaction transaction1 = redisson.createTransaction(TransactionOptions.defaults().timeout(10, TimeUnit.SECONDS));
        RMap<Integer, String> map1 = transaction1.getMap("put_test");
        map1.remove(1);
        RTransaction transaction2 = redisson.createTransaction(TransactionOptions.defaults());
        RMap<Integer, String> map2 = transaction2.getMap("put_test");
        map2.put(1, "aryan");
        try {
            transaction2.commit();
            Assertions.fail();
        } catch (TransactionTimeoutException e) {
        // skip
        }
        transaction1.commit();
        assertThat(m.size()).isZero();
    });
}
Also used : RTransaction(org.redisson.api.RTransaction) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 29 with RTransaction

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

the class RedissonBaseTransactionalMapTest method testKeySet.

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

Example 30 with RTransaction

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

the class RedissonBaseTransactionalMapTest method testPutIfAbsent.

@Test
public void testPutIfAbsent() {
    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.putIfAbsent("3", "2")).isEqualTo("4");
    assertThat(map.putIfAbsent("5", "6")).isNull();
    assertThat(map.putIfAbsent("5", "7")).isEqualTo("6");
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.size()).isEqualTo(2);
    transaction.commit();
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isEqualTo("6");
}
Also used : RTransaction(org.redisson.api.RTransaction) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

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