/** * Run the testsuite with the default options.<p/> * * When the test suite is executed in a Vertx context (i.e `Vertx.currentContext()` returns a context) this * context's event loop is used for running the test suite. Otherwise it is executed in the current thread.<p/> * * The returned {@link io.vertx.rxjava.ext.unit.Completion} object can be used to get a completion callback. * @return the related test completion */ public io.vertx.rxjava.ext.unit.TestCompletion run() { io.vertx.rxjava.ext.unit.TestCompletion ret = io.vertx.rxjava.ext.unit.TestCompletion.newInstance(delegate.run()); return ret; }
/** * Create and return a new test suite. * @param name the test suite name * @return the created test suite */ public static io.vertx.rxjava.ext.unit.TestSuite create(String name) { io.vertx.rxjava.ext.unit.TestSuite ret = io.vertx.rxjava.ext.unit.TestSuite.newInstance(io.vertx.ext.unit.TestSuite.create(name)); return ret; }
/** * Add a new test case to the suite. * @param name the test case name * @param testCase the test case * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.unit.TestSuite test(String name, Handler<io.vertx.rxjava.ext.unit.TestContext> testCase) { delegate.test(name, new Handler<io.vertx.ext.unit.TestContext>() { public void handle(io.vertx.ext.unit.TestContext event) { testCase.handle(io.vertx.rxjava.ext.unit.TestContext.newInstance(event)); } }); return this; }
private void testSucceeded(Consumer<Completion> consumer) { TestSuite suite = TestSuite.create("my_suite").test("my_test", context -> { }); Completion completion = suite.run(Vertx.vertx()); consumer.accept(completion); assertTrue(completion.isCompleted()); assertTrue(completion.isSucceeded()); assertFalse(completion.isFailed()); }
@Test public void testTimeoutThreadGarbaged() throws Exception { Set<String> threadNames = Thread.getAllStackTraces(). keySet(). stream(). filter(t -> t.getName().startsWith("vert.x-unit-timeout-thread-")). map(Thread::getName). collect(Collectors.toSet()); AtomicReference<Thread> timeoutThread = new AtomicReference<>(); TestSuite suite = TestSuite.create("my_suite"). test("my_test0", context -> { while (timeoutThread.get() == null) { for (Thread thread : Thread.getAllStackTraces().keySet()) { if (thread.getName().startsWith("vert.x-unit-timeout-thread-")) { if (!threadNames.contains(thread.getName())) { timeoutThread.set(thread); } } } } }); TestReporter reporter = new TestReporter(); run(suite, reporter, 20000); reporter.await(); assertNotNull(timeoutThread.get()); timeoutThread.get().join(20000); assertEquals(timeoutThread.get().getState(), Thread.State.TERMINATED); }
@org.junit.Test public void testReportToFailureCompletionHandler() { RuntimeException e = new RuntimeException(); Handler<TestContext> fails = context -> { throw e; }; Handler<TestContext> pass = context -> { }; TestSuite[] suites = { TestSuite.create("my_suite").test("first_test", fails), TestSuite.create("my_suite").before(fails).test("first_test", pass), TestSuite.create("my_suite").test("first_test", pass).after(fails) }; AtomicInteger count = new AtomicInteger(); for (TestSuite suite : suites) { suite.run(new TestOptions()).handler(ar -> { assertTrue(ar.failed()); assertSame(e, ar.cause()); if (count.incrementAndGet() == 3) { testComplete(); } }); } await(); } }
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());
List<Integer> afterEach = Collections.synchronizedList(new ArrayList<>()); List<Integer> after = Collections.synchronizedList(new ArrayList<>()); TestSuite suite = TestSuite.create("my_suite").before(context -> { Integer value = context.get("value"); if (value != null) { }).beforeEach(context -> { Integer value = context.get("value"); beforeEach.add(value); context.put("value", count.getAndIncrement()); }).test("my_test0", context -> { int value = context.get("value"); test0.set(value); context.put("value", value * 2); }).test("my_test1", context -> { int value = context.get("value"); test1.set(context.get("value")); context.put("value", value * 2); }).afterEach(context -> { int value = context.get("value"); afterEach.add(value); }).after(context -> { int value = context.get("value"); after.add(value);
AtomicBoolean sameContext = new AtomicBoolean(); BlockingQueue<Async> queue = new ArrayBlockingQueue<>(1); TestSuite suite = TestSuite.create("my_suite").test("my_test", context -> { count.compareAndSet(1, 2); sameContext.set(checkTest(context)); }); if (i == 0) { suite = suite.before(context -> { count.compareAndSet(0, 1); queue.add(context.async()); }); } else { suite = suite.beforeEach(context -> { count.compareAndSet(0, 1); queue.add(context.async());
@Test public void testRepeatBeforeAfterEach() throws Exception { List<String> events = Collections.synchronizedList(new ArrayList<String>()); TestSuite suite = TestSuite.create("my_suite").beforeEach(ctx -> { events.add("before"); }).test("my_test", 3, ctx -> { events.add("test"); }).afterEach(ctx -> { events.add("after"); }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); assertEquals(1, reporter.results.size()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); assertEquals(Arrays.asList("before", "test", "after", "before", "test", "after", "before", "test", "after"), events); assertEquals("my_test", reporter.results.get(0).name()); assertFalse(reporter.results.get(0).failed()); }
/** * Set a callback executed before each test and after the suite <code>before</code> callback. * @param callback the callback * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.unit.TestSuite beforeEach(Handler<io.vertx.rxjava.ext.unit.TestContext> callback) { delegate.beforeEach(new Handler<io.vertx.ext.unit.TestContext>() { public void handle(io.vertx.ext.unit.TestContext event) { callback.handle(io.vertx.rxjava.ext.unit.TestContext.newInstance(event)); } }); return this; }
/** * Set a callback executed after the tests. * @param callback the callback * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.unit.TestSuite after(Handler<io.vertx.rxjava.ext.unit.TestContext> callback) { delegate.after(new Handler<io.vertx.ext.unit.TestContext>() { public void handle(io.vertx.ext.unit.TestContext event) { callback.handle(io.vertx.rxjava.ext.unit.TestContext.newInstance(event)); } }); return this; }
/** * Set a callback executed before the tests. * @param callback the callback * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.unit.TestSuite before(Handler<io.vertx.rxjava.ext.unit.TestContext> callback) { delegate.before(new Handler<io.vertx.ext.unit.TestContext>() { public void handle(io.vertx.ext.unit.TestContext event) { callback.handle(io.vertx.rxjava.ext.unit.TestContext.newInstance(event)); } }); return this; }
/** * Set a callback executed after each test and before the suite <code>after</code> callback. * @param callback the callback * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.unit.TestSuite afterEach(Handler<io.vertx.rxjava.ext.unit.TestContext> callback) { delegate.afterEach(new Handler<io.vertx.ext.unit.TestContext>() { public void handle(io.vertx.ext.unit.TestContext event) { callback.handle(io.vertx.rxjava.ext.unit.TestContext.newInstance(event)); } }); return this; }
@Test public void testAssertAsyncFailureHandlerSucceeded() throws Exception { BlockingQueue<Handler<AsyncResult<String>>> handlerQueue = new ArrayBlockingQueue<>(1); BlockingQueue<Throwable> resultQueue = new ArrayBlockingQueue<>(1); TestSuite suite = TestSuite.create("my_suite").test("my_test", context -> { handlerQueue.add(context.<String>asyncAssertFailure(resultQueue::add)); }); TestReporter reporter = new TestReporter(); run(suite, reporter); Handler<AsyncResult<String>> handler = handlerQueue.poll(2, TimeUnit.SECONDS); Throwable expected = new Throwable(); handler.handle(Future.failedFuture(expected)); Throwable cause = resultQueue.poll(2, TimeUnit.SECONDS); assertSame(expected, cause); 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()); }
consumer.completionHandler(ar -> { assertTrue(ar.succeeded()); TestSuite suite = TestSuite.create(testSuiteName). test(testCaseName, context -> { try { Thread.sleep(10); } catch (InterruptedException ignore) { }).after(context -> { throw error; }); suite.run(vertx, new TestOptions().addReporter(new ReportOptions().setTo("bus:" + address))); }); testReporter.await();
@Test public void afterIsRunAfterFailure() throws Exception { for (int i = 0;i < 2;i++) { AtomicInteger count = new AtomicInteger(); TestSuite suite = TestSuite.create("my_suite").test("my_test", context -> { count.compareAndSet(0, 1); context.fail("the_message"); }); if (i == 0) { suite = suite.after(context -> count.compareAndSet(1, 2)); } else { suite = suite.afterEach(context -> count.compareAndSet(1, 2)); } TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); assertEquals(2, count.get()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); assertEquals("my_test", reporter.results.get(0).name()); assertTrue(reporter.results.get(0).failed()); assertEquals("the_message", reporter.results.get(0).failure().message()); } }