Txn { public static <T extends Transactional> void executeWrite(T txn, Runnable r) { exec(txn, TxnType.WRITE, r); }  static void execute(T txn, Runnable r); static X calculate(T txn, Supplier<X> r); static void exec(T txn, TxnType txnType, Runnable r); static X calc(T txn, TxnType txnType, Supplier<X> r); static void executeRead(T txn, Runnable r); static X calculateRead(T txn, Supplier<X> r); static void executeWrite(T txn, Runnable r); static X calculateWrite(T txn, Supplier<X> r);  }
@Test public void txn_write_03() { Txn.executeWrite(counter, () -> { counter.inc() ; assertEquals("In W, value()", 0, counter.value()) ; assertEquals("In W, get()",1, counter.get()) ; counter.commit() ; }) ; assertEquals("Outside W",1, counter.value()) ; }
@Test public void txn_write_04() { Txn.executeWrite(counter, () -> { counter.inc() ; assertEquals("In W, value()", 0, counter.value()) ; assertEquals("In W, get()",1, counter.get()) ; counter.abort() ; }) ; assertEquals("Outside W", 0, counter.value()) ; }
@Test public void txn_continue_1() { Txn.executeWrite(counter, ()->counter.set(91)) ; Txn.executeWrite(counter, ()-> { assertEquals("In txn, value()", 91, counter.value()) ; assertEquals("In txn, read()", 91, counter.read()) ; counter.inc(); Txn.executeWrite(counter, ()->{ assertEquals("In txn, value()", 91, counter.value()) ; assertEquals("In txn, get()", 92, counter.read()) ; }) ; }); assertEquals(92,counter.value()) ; }
@Test public void txn_continue_2() { Txn.executeWrite(counter, ()->counter.set(91)) ; Txn.executeWrite(counter, ()-> { assertEquals("In txn, value()", 91, counter.value()) ; assertEquals("In txn, read()", 91, counter.read()) ; counter.inc(); Txn.executeWrite(counter, ()->{ assertEquals("In txn, value()", 91, counter.value()) ; assertEquals("In txn, get()", 92, counter.read()) ; counter.inc(); }) ; assertEquals("In txn, value()", 91, counter.value()) ; assertEquals("In txn, read()", 93, counter.read()) ; counter.inc(); }); assertEquals(94,counter.value()) ; }
@Test(expected=ExceptionFromTest.class) public void txn_exception_01() { Txn.executeWrite(counter, counter::inc) ; Txn.executeWrite(counter, () -> { counter.inc() ; assertEquals("In W, value()", 1, counter.value()) ; assertEquals("In W, get()",2, counter.get()) ; throw new ExceptionFromTest() ; }) ; }
@Test public void txn_exception_02() { Txn.executeWrite(counter, ()->counter.set(8)) ; try { Txn.executeWrite(counter, () -> { counter.inc(); assertEquals("In W, value()", 8, counter.value()); assertEquals("In W, get()", 9, counter.get()); throw new ExceptionFromTest(); }); } catch (ExceptionFromTest ex) {} assertEquals("After W/abort, get()", 8, counter.get()); }