vert-x3 / vertx-rx

Reactive Extensions for Vert.x
Apache License 2.0
147 stars 72 forks source link

Unit test with Rxified Vertx #185

Closed aygalinc closed 5 years ago

aygalinc commented 5 years ago

Hi,

It is not clear in documentation how to test wth Vertx-Unit the Rxified vertx (I tried Rxified 2 version).

Async & test context have their pendant in Rx, but not VertxRunner or RunOnContext rule.

I use Rx2 vertx in some test & it always wait the timeout to fail, even if some assertion fail before. But they are not propagated to vertx context.

A quick reproducer is : ` import static org.assertj.core.api.Assertions.fail;

import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.RunTestOnContext; import io.vertx.ext.unit.junit.VertxUnitRunner;

import io.vertx.reactivex.core.Vertx; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith;

@RunWith(VertxUnitRunner.class) public class DummyTest {

@Rule public RunTestOnContext rule = new RunTestOnContext();

private Vertx vertx;

@Before public void setUp(TestContext context) { vertx = new io.vertx.reactivex.core.Vertx(rule.vertx()); vertx.exceptionHandler(context.exceptionHandler()); }

@Test(timeout = 1000L) public void dummyTest(TestContext context){ Async async = context.async();

vertx.rxUndeploy("test")
    .subscribe(() ->fail("always fail") ,throwable->fail("always fail"));

} } `

And I have to wait the timeout as shown in log :

io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.AssertionError: always fail at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367) at io.reactivex.internal.observers.CallbackCompletableObserver.onError(CallbackCompletableObserver.java:67) at io.vertx.reactivex.impl.AsyncResultCompletable.lambda$subscribeActual$0(AsyncResultCompletable.java:52) at io.vertx.core.impl.FutureImpl.tryFail(FutureImpl.java:170) at io.vertx.core.impl.FutureImpl.fail(FutureImpl.java:100) at io.vertx.core.impl.FutureImpl.handle(FutureImpl.java:153) at io.vertx.core.impl.FutureImpl.handle(FutureImpl.java:18) at io.vertx.core.impl.FutureImpl.tryFail(FutureImpl.java:170) at io.vertx.core.impl.FutureImpl.fail(FutureImpl.java:100) at io.vertx.core.impl.FutureImpl.handle(FutureImpl.java:153) at io.vertx.core.impl.FutureImpl.handle(FutureImpl.java:18) at io.vertx.core.impl.DeploymentManager.lambda$reportResult$6(DeploymentManager.java:457) at io.vertx.core.impl.ContextImpl.executeTask(ContextImpl.java:320) at io.vertx.core.impl.EventLoopContext.lambda$executeAsync$0(EventLoopContext.java:38) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:462) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.AssertionError: always fail at com.se.gxl.forum.service.consumer.helper.tcp.impl.test.DummyTest.lambda$dummyTest$1(DummyTest.java:37) at io.reactivex.internal.observers.CallbackCompletableObserver.onError(CallbackCompletableObserver.java:64) ... 18 more Exception in thread "vert.x-eventloop-thread-0" io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.AssertionError: always fail at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367) at io.reactivex.internal.observers.CallbackCompletableObserver.onError(CallbackCompletableObserver.java:67) at io.vertx.reactivex.impl.AsyncResultCompletable.lambda$subscribeActual$0(AsyncResultCompletable.java:52) at io.vertx.core.impl.FutureImpl.tryFail(FutureImpl.java:170) at io.vertx.core.impl.FutureImpl.fail(FutureImpl.java:100) at io.vertx.core.impl.FutureImpl.handle(FutureImpl.java:153) at io.vertx.core.impl.FutureImpl.handle(FutureImpl.java:18) at io.vertx.core.impl.FutureImpl.tryFail(FutureImpl.java:170) at io.vertx.core.impl.FutureImpl.fail(FutureImpl.java:100) at io.vertx.core.impl.FutureImpl.handle(FutureImpl.java:153) at io.vertx.core.impl.FutureImpl.handle(FutureImpl.java:18) at io.vertx.core.impl.DeploymentManager.lambda$reportResult$6(DeploymentManager.java:457) at io.vertx.core.impl.ContextImpl.executeTask(ContextImpl.java:320) at io.vertx.core.impl.EventLoopContext.lambda$executeAsync$0(EventLoopContext.java:38) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:462) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.AssertionError: always fail at com.se.gxl.forum.service.consumer.helper.tcp.impl.test.DummyTest.lambda$dummyTest$1(DummyTest.java:37) at io.reactivex.internal.observers.CallbackCompletableObserver.onError(CallbackCompletableObserver.java:64) ... 18 more

java.util.concurrent.TimeoutException at io.vertx.ext.unit.impl.TestContextImpl.lambda$run$1(TestContextImpl.java:79) at java.lang.Thread.run(Thread.java:748)

tsegismont commented 5 years ago

@aygalinc you're headed in the right direction. The problem in your test is that Assertions#fail does not report the failure to the TestContext. As a consequence, the test continues to run until it timeouts.

You should wrap the assertj assertions with TestContext#verify. Like this:

    vertx.rxUndeploy("test")
      .subscribe(() -> context.verify(v -> {
        fail("always fail");
      }), throwable -> context.verify(v -> {
        fail("always fail");
      }));

See http://vertx.io/docs/vertx-unit/java/#_using_third_party_assertion_framework

aygalinc commented 5 years ago

thank you !

But now in log I have all the stack trace of an UndeliverableException. Do you recommend to do before launching test : RxJavaPlugins.setErrorHandler(t -> {}); Or leave the UndeliverableException stack trace which can be confusing when someone read test log ?

tsegismont commented 5 years ago

I would just focus on the error reported by junit but that's just me :)