Example #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 whenTimeout() throws InterruptedException {
    barrier = new VetoCommitBarrier();

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

    thread.setPrintStackTrace(false);
    thread.start();
    joinAll(thread);
  }
  @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);
  }