Exemplo n.º 1
0
  @Test
  public void awaitTest() {
    final LongRef ref = new LongRef();

    TestThread t =
        new TestThread() {
          @Override
          public void doRun() throws Exception {
            ref.await(2);
          }
        };

    t.start();
    sleepMs(500);

    assertAlive(t);

    ref.set(1);
    sleepMs(500);

    assertAlive(t);

    ref.set(2);
    joinAll(t);
  }
  @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 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);
  }
  @Test
  public void whenCommittedWhileWaiting() {
    barrier = new VetoCommitBarrier();

    TestThread thread =
        new TestThread() {
          @Override
          public void doRun() throws Exception {
            barrier.tryAwaitOpen(1, TimeUnit.DAYS);
          }
        };

    thread.setPrintStackTrace(false);
    thread.start();
    sleepMs(500);
    thread.interrupt();

    // thread.join();
    // thread.assertFailedWithException(InterruptedException.class);
  }
Exemplo n.º 5
0
 @Override
 public void doRun() throws Exception {
   sleepMs(300);
   txInt.set(value);
 }