@Test public void runTestWithAsyncCompletion() throws Exception { BlockingQueue<Async> queue = new ArrayBlockingQueue<>(1); AtomicInteger count = new AtomicInteger(); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { count.compareAndSet(0, 1); queue.add(context.async()); }); TestReporter reporter = new TestReporter(); run(suite, reporter); Async async = queue.poll(2, TimeUnit.SECONDS); assertEquals(1, count.get()); assertFalse(reporter.completed()); completeAsync.accept(async); reporter.await(); assertTrue(reporter.completed()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertTrue(result.succeeded()); assertFalse(result.failed()); assertNull(result.failure()); }
@Test public void testAssertAsyncFailureHandlerSucceededAsync() throws Exception { BlockingQueue<Handler<AsyncResult<String>>> handlerQueue = new ArrayBlockingQueue<>(1); BlockingQueue<Async> resultQueue = new ArrayBlockingQueue<>(1); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { handlerQueue.add( context.<String>asyncAssertFailure( r -> { resultQueue.add(context.async()); })); }); TestReporter reporter = new TestReporter(); run(suite, reporter); Handler<AsyncResult<String>> handler = handlerQueue.poll(2, TimeUnit.SECONDS); handler.handle(Future.failedFuture(new Throwable())); Async result = resultQueue.poll(2, TimeUnit.SECONDS); assertFalse(reporter.completed()); result.complete(); reporter.await(); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); assertEquals("my_test", reporter.results.get(0).name()); assertFalse(reporter.results.get(0).failed()); }
@Test public void runTestWithAsyncCompletionAfterFailureInTest() throws Exception { AtomicBoolean completed = new AtomicBoolean(); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { Async async = context.async(); try { context.fail("msg"); } catch (AssertionError ignore) { } async.complete(); completed.set(true); }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); assertTrue(completed.get()); assertTrue(reporter.completed()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertTrue(result.failed()); }
@Test public void runTestWithAsyncCompletionCompletedInTest() throws Exception { AtomicBoolean ok = new AtomicBoolean(); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { Async async = context.async(); async.complete(); try { async.complete(); } catch (IllegalStateException ignore) { } ok.set(true); }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); assertTrue(ok.get()); assertTrue(reporter.completed()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertTrue(result.succeeded()); assertFalse(result.failed()); assertNull(result.failure()); }
@Test public void runTestWithFailBeforeAsync() throws Exception { AtomicReference<AssertionError> failure = new AtomicReference<>(); BlockingQueue<Async> queue = new ArrayBlockingQueue<>(1); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { try { context.fail(); } catch (AssertionError e) { failure.set(e); } queue.add(context.async()); }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); Async async = queue.poll(2, TimeUnit.SECONDS); async.complete(); assertTrue(reporter.completed()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertFalse(result.succeeded()); assertTrue(result.failed()); assertNotNull(result.failure()); assertSame(failure.get(), result.failure().cause()); }
@Test public void runTestWithFailAfterAsync() throws Exception { RuntimeException failure = new RuntimeException(); BlockingQueue<Async> queue = new ArrayBlockingQueue<>(1); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { queue.add(context.async()); throw failure; }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); Async async = queue.poll(2, TimeUnit.SECONDS); async.complete(); assertTrue(reporter.completed()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertFalse(result.succeeded()); assertTrue(result.failed()); assertNotNull(result.failure()); assertSame(failure, result.failure().cause()); }
@Test public void runTestWithAsyncFailure() throws Exception { BlockingQueue<TestContext> queue = new ArrayBlockingQueue<>(1); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { context.async(); queue.add(context); }); TestReporter reporter = new TestReporter(); run(suite, reporter); assertFalse(reporter.completed()); TestContext test = queue.poll(2, TimeUnit.SECONDS); try { test.fail("the_message"); } catch (AssertionError ignore) { } reporter.await(); assertTrue(reporter.completed()); }
@Test public void runAfterWithAsyncCompletion() throws Exception { for (int i = 0; i < 2; i++) { AtomicInteger count = new AtomicInteger(); BlockingQueue<Async> queue = new ArrayBlockingQueue<>(1); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { count.compareAndSet(0, 1); }); if (i == 0) { suite = suite.after( context -> { count.compareAndSet(1, 2); queue.add(context.async()); }); } else { suite = suite.afterEach( context -> { count.compareAndSet(1, 2); queue.add(context.async()); }); } TestReporter reporter = new TestReporter(); run(suite, reporter); Async async = queue.poll(2, TimeUnit.SECONDS); assertFalse(reporter.completed()); assertEquals(2, count.get()); completeAsync.accept(async); reporter.await(); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); assertEquals("my_test", reporter.results.get(0).name()); assertTrue(reporter.results.get(0).succeeded()); } }