Search in sources :

Example 1 with Transaction

use of io.atomix.core.transaction.Transaction in project atomix by atomix.

the class ConsistentMapTest method testTransaction.

@Test
public void testTransaction() throws Throwable {
    Transaction transaction1 = atomix().transactionBuilder().withIsolation(Isolation.READ_COMMITTED).build();
    transaction1.begin();
    TransactionalMap<String, String> map1 = transaction1.<String, String>mapBuilder("test-transactional-map", protocol()).build();
    Transaction transaction2 = atomix().transactionBuilder().withIsolation(Isolation.REPEATABLE_READS).build();
    transaction2.begin();
    TransactionalMap<String, String> map2 = transaction2.<String, String>mapBuilder("test-transactional-map", protocol()).build();
    assertNull(map1.get("foo"));
    assertFalse(map1.containsKey("foo"));
    assertNull(map2.get("foo"));
    assertFalse(map2.containsKey("foo"));
    map1.put("foo", "bar");
    map1.put("bar", "baz");
    assertNull(map1.get("foo"));
    assertEquals(transaction1.commit(), CommitStatus.SUCCESS);
    assertNull(map2.get("foo"));
    assertEquals(map2.get("bar"), "baz");
    map2.put("foo", "bar");
    assertEquals(map2.get("foo"), "bar");
    map2.remove("foo");
    assertFalse(map2.containsKey("foo"));
    map2.put("foo", "baz");
    assertEquals(transaction2.commit(), CommitStatus.FAILURE);
    Transaction transaction3 = atomix().transactionBuilder().withIsolation(Isolation.REPEATABLE_READS).build();
    transaction3.begin();
    TransactionalMap<String, String> map3 = transaction3.<String, String>mapBuilder("test-transactional-map", protocol()).build();
    assertEquals(map3.get("foo"), "bar");
    map3.put("foo", "baz");
    assertEquals(map3.get("foo"), "baz");
    assertEquals(transaction3.commit(), CommitStatus.SUCCESS);
    ConsistentMap<String, String> map = atomix().<String, String>consistentMapBuilder("test-transactional-map", protocol()).build();
    assertEquals(map.get("foo").value(), "baz");
    assertEquals(map.get("bar").value(), "baz");
    Map<String, Versioned<String>> result = map.getAllPresent(Collections.singleton("foo"));
    assertNotNull(result);
    assertTrue(result.size() == 1);
    assertEquals(result.get("foo").value(), "baz");
}
Also used : Transaction(io.atomix.core.transaction.Transaction) Versioned(io.atomix.utils.time.Versioned) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Aggregations

AbstractPrimitiveTest (io.atomix.core.AbstractPrimitiveTest)1 Transaction (io.atomix.core.transaction.Transaction)1 Versioned (io.atomix.utils.time.Versioned)1 Test (org.junit.Test)1