コード例 #1
0
  @Test
  public void whenNoPendingTransactions() {
    VetoCommitBarrier barrier = new VetoCommitBarrier();
    barrier.vetoCommit();

    assertTrue(barrier.isCommitted());
  }
コード例 #2
0
  @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());
  }
コード例 #3
0
  @Test
  public void whenBarrierCommitted_thenIgnored() {
    VetoCommitBarrier barrier = new VetoCommitBarrier();
    barrier.vetoCommit();

    barrier.vetoCommit();
    assertTrue(barrier.isCommitted());
  }
コード例 #4
0
  @Test
  public void whenCommitted() throws InterruptedException {
    barrier = new VetoCommitBarrier();
    barrier.atomicVetoCommit();

    barrier.awaitOpen();
    assertTrue(barrier.isCommitted());
  }
コード例 #5
0
  @Test
  public void whenAborted() throws InterruptedException {
    barrier = new VetoCommitBarrier();
    barrier.abort();

    boolean success = barrier.tryAwaitOpen(1, TimeUnit.DAYS);
    assertTrue(barrier.isAborted());
    assertTrue(success);
  }
コード例 #6
0
  @Test
  public void whenBarrierAborted_thenCommitBarrierOpenException() {
    VetoCommitBarrier barrier = new VetoCommitBarrier();
    barrier.abort();

    try {
      barrier.vetoCommit();
      fail();
    } catch (CommitBarrierOpenException expected) {
    }
    assertTrue(barrier.isAborted());
  }
コード例 #7
0
  @Test
  public void whenNullTimeout_thenNullPointerException() throws InterruptedException {
    barrier = new VetoCommitBarrier();

    try {
      barrier.tryAwaitOpen(1, null);
      fail();
    } catch (NullPointerException expected) {
    }

    assertTrue(barrier.isClosed());
  }
コード例 #8
0
  @Test
  public void whenAlreadyInterrupted() {
    Thread.currentThread().interrupt();

    barrier = new VetoCommitBarrier();
    try {
      barrier.tryAwaitOpen(1, TimeUnit.DAYS);
      fail();
    } catch (InterruptedException expected) {
    }

    assertTrue(barrier.isClosed());
  }
コード例 #9
0
 @Override
 @TransactionalMethod
 public void doRun() throws Exception {
   tx = getThreadLocalTransaction();
   ref.inc();
   barrier.joinCommit(tx);
 }
コード例 #10
0
  @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);
  }