/** * Marks a failure with no cause. This causes an exception to be thrown in the calling thread, * as well was any blocking thread waiting at {@link #waitForTest()}. */ public void fail() { fail(""); }
public static void optimizeDoneListenerExecutorTest(ListenableFuture<?> lf) throws InterruptedException, TimeoutException { AsyncVerifier av = new AsyncVerifier(); Thread t = Thread.currentThread(); lf.addListener(() -> {av.assertTrue(Thread.currentThread() == t) ; av.signalComplete();}, CentralThreadlyPool.computationPool(), ListenerOptimizationStrategy.SingleThreadIfExecutorMatchOrDone); av.waitForTest(); }
@Override public void handleRunStart() { try { int runCount = scheduler.blockingTick(null); // should block av.assertEquals(1, runCount); av.signalComplete(); } catch (InterruptedException e) { av.fail(e); } } };
@Override public void handleFailure(Throwable t) { av.assertEquals(msg, t.getMessage()); av.signalComplete(); } });
@Override public void run() { interruptClearedAV.assertFalse(Thread.currentThread().isInterrupted()); interruptClearedAV.signalComplete(); } });
public static void getLongRunningTasksWrappedFutureTest(final StatisticPriorityScheduler scheduler) throws InterruptedException, TimeoutException { final AsyncVerifier av = new AsyncVerifier(); scheduler.submit(new ClockUpdateRunnable() { @Override public void handleRunStart() { // even submitted (and thus wrapped in a future), we should get our direct reference av.assertTrue(scheduler.getLongRunningTasks(-1).get(0).getLeft() == this); av.signalComplete(); } }); av.waitForTest(); }
/** * Waits for a default of 10 seconds, or until signalComplete has been called once, or until a * failure occurs. If signalComplete has been called before this, this call will never block. * * @throws InterruptedException Thrown if thread is interrupted while waiting * @throws TimeoutException Thrown if timeout occurs without signalComplete being called */ public void waitForTest() throws InterruptedException, TimeoutException { waitForTest(DEFAULT_TIMEOUT, 1); }
@Test public void singleThreadPriorityTest() throws InterruptedException, TimeoutException { AsyncVerifier av = new AsyncVerifier(); CentralThreadlyPool.singleThreadPool(false, null, Thread.MIN_PRIORITY).execute(() -> { av.assertEquals(Thread.MIN_PRIORITY, Thread.currentThread().getPriority()); av.signalComplete(); }); av.waitForTest(); }
@Override public void handleRunStart() { try { long startTime = Clock.accurateForwardProgressingMillis(); scheduler.schedule(testTask, DELAY_TIME); int runCount = scheduler.blockingTick(null); // should block long finishTime = Clock.accurateForwardProgressingMillis(); av.assertEquals(1, runCount); av.assertTrue(finishTime - startTime >= DELAY_TIME); av.assertTrue(testTask.ranOnce()); av.signalComplete(); } catch (InterruptedException e) { av.fail(e); } } };
@Override public void run() { try { rfca.run(); av.fail("Exception did not throw"); } catch (RuntimeException expected) { av.signalComplete(); } } });
@Test public void signalCompleteBeforeWaitTest() throws InterruptedException, TimeoutException { verifier.signalComplete(); verifier.waitForTest(1); // no exception should throw as no blocking is needed }
@Override public void handleRunStart() { if (fLastRun != null) { av.assertTrue(fLastRun.ranOnce()); } av.signalComplete(); } };
@Test public void assertEqualsTest() { Object o = new Object(); verifier.assertEquals(o, o); verifier.assertEquals(null, null); verifier.assertEquals(1, 1); verifier.assertEquals(1L, 1L); verifier.assertEquals(1.1, 1.1); }
@Override public void run() { try { Thread.sleep(ASYNC_WAIT_AMOUNT); } catch (InterruptedException e) { // let thread exit return; } verifier.assertNull(new Object()); } }).start();
@Override public void run() { try { Thread.sleep(ASYNC_WAIT_AMOUNT); } catch (InterruptedException e) { // let thread exit return; } verifier.assertNotNull(null); } }).start();
@Override public void handleRunFinish() { running.decrementAndGet(); verifier.signalComplete(); } };
@Override public void run() { try { Thread.sleep(ASYNC_WAIT_AMOUNT); } catch (InterruptedException e) { // let thread exit return; } verifier.assertTrue(false); } }).start();
@Override public void run() { try { Thread.sleep(ASYNC_WAIT_AMOUNT); } catch (InterruptedException e) { // let thread exit return; } verifier.assertFalse(true); } }).start();
@Before public void setup() { verifier = new AsyncVerifier(); }
private static void verifyCancelationExceptionMessageInCallback(String msg, ListenableFuture<?> lf) throws InterruptedException, TimeoutException { AsyncVerifier av = new AsyncVerifier(); lf.addCallback(new FutureCallback<Object>() { @Override public void handleResult(Object result) { av.fail(); } @Override public void handleFailure(Throwable t) { av.assertEquals(msg, t.getMessage()); av.signalComplete(); } }); av.waitForTest(); }