@Test public void whenNoPendingTransactions() { VetoCommitBarrier barrier = new VetoCommitBarrier(); barrier.vetoCommit(); assertTrue(barrier.isCommitted()); }
@Test public void whenPendingTransactions() { VetoCommitBarrier barrier = new VetoCommitBarrier(); IntRef ref1 = new IntRef(); IntRef ref2 = new IntRef(); IntRef ref3 = new IntRef(); IncThread thread1 = new IncThread(ref1, barrier); IncThread thread2 = new IncThread(ref2, barrier); IncThread thread3 = new IncThread(ref3, barrier); startAll(thread1, thread2, thread3); sleepMs(500); barrier.vetoCommit(); joinAll(thread1, thread2, thread3); assertIsCommitted(thread1.tx); assertIsCommitted(thread2.tx); assertIsCommitted(thread3.tx); assertEquals(1, ref1.get()); assertEquals(1, ref2.get()); assertEquals(1, ref3.get()); }
@Test public void whenBarrierCommitted_thenIgnored() { VetoCommitBarrier barrier = new VetoCommitBarrier(); barrier.vetoCommit(); barrier.vetoCommit(); assertTrue(barrier.isCommitted()); }
@Test public void whenCommitted() throws InterruptedException { barrier = new VetoCommitBarrier(); barrier.atomicVetoCommit(); barrier.awaitOpen(); assertTrue(barrier.isCommitted()); }
@Test public void whenAborted() throws InterruptedException { barrier = new VetoCommitBarrier(); barrier.abort(); boolean success = barrier.tryAwaitOpen(1, TimeUnit.DAYS); assertTrue(barrier.isAborted()); assertTrue(success); }
@Test public void whenBarrierAborted_thenCommitBarrierOpenException() { VetoCommitBarrier barrier = new VetoCommitBarrier(); barrier.abort(); try { barrier.vetoCommit(); fail(); } catch (CommitBarrierOpenException expected) { } assertTrue(barrier.isAborted()); }
@Test public void whenNullTimeout_thenNullPointerException() throws InterruptedException { barrier = new VetoCommitBarrier(); try { barrier.tryAwaitOpen(1, null); fail(); } catch (NullPointerException expected) { } assertTrue(barrier.isClosed()); }
@Test public void whenAlreadyInterrupted() { Thread.currentThread().interrupt(); barrier = new VetoCommitBarrier(); try { barrier.tryAwaitOpen(1, TimeUnit.DAYS); fail(); } catch (InterruptedException expected) { } assertTrue(barrier.isClosed()); }
@Override @TransactionalMethod public void doRun() throws Exception { tx = getThreadLocalTransaction(); ref.inc(); barrier.joinCommit(tx); }
@Test public void whenAbortedWhileWaiting() throws InterruptedException { barrier = new VetoCommitBarrier(); TestThread thread = new TestThread() { @Override public void doRun() throws Exception { boolean result = barrier.tryAwaitOpen(1, TimeUnit.DAYS); assertTrue(result); } }; thread.setPrintStackTrace(false); thread.start(); sleepMs(500); barrier.abort(); joinAll(thread); }