Unit test – unit test vertx – Java util. concurrent. TimeoutException
•
Java
I'm trying to test HTTP calls from the vertx webclient unit using the rxified version of vertxunitrunner and vertx
The problem is that my unit tests always fail due to timeout exceptions Are there different methods for unit testing webclient HTTP calls? Here is my code:
import io.vertx.core.AsyncResult; import io.vertx.core.http.HttpClientOptions; import io.vertx.core.http.HttpServerOptions; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; import io.vertx.rxjava.core.Vertx; import io.vertx.rxjava.core.buffer.Buffer; import io.vertx.rxjava.core.http.HttpServer; import io.vertx.rxjava.ext.web.client.HttpResponse; import io.vertx.rxjava.ext.web.client.WebClient; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import rx.Single; @RunWith(VertxUnitRunner.class) public class MyVertxTest { private Vertx vertx; private WebClient client; @Before public void setUp() throws Exception { vertx = Vertx.vertx(); } @Test public void testGetContactDetails(TestContext context) { System.out.println("start"); long start = System.currentTimeMillis(); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(TEST_SERVER_PORT)); server.requestStream().handler(req -> { req.response().setChunked(true).write("foo bar").end(); }); System.out.println("created server"); try { server.listen(9000,"localhost",(AsyncResult<HttpServer> ar) -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); System.out.println("created client"); Single<HttpResponse<Buffer>> single = client .get(9000,"/foo") .rxSend(); single.subscribe(s -> { System.out.println("inside subscribe"); context.assertEquals("foo bar",s.bodyAsString()); },e -> { context.fail(e); }); }); context.async().await(); System.out.println("total time : " + (System.currentTimeMillis() - start / 1000)+" seconds); } catch (Exception e) { context.fail(e); } finally { server.close(); } } }
The test always fails because of the timeout after 120 seconds
OUTPUT
start created server created client inside subscribe total time : 120 java.util.concurrent.TimeoutException at io.vertx.ext.unit.impl.TestContextImpl$Step.lambda$run$0(TestContextImpl.java:112) at java.lang.Thread.run(Thread.java:745)
Solution
Because your async use is wrong It is similar to Java countdownlatch It is described in docs
The correct usage is:
Async async = context.async(); //here server.listen(9000,(AsyncResult<HttpServer> ar) -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); System.out.println("created client"); Single<HttpResponse<Buffer>> single = client .get(9000,"/foo") .rxSend().subscribeOn(Schedulers.io()); single.subscribe(s -> { System.out.println("inside subscribe"); context.assertEquals("foo bar",s.bodyAsString()); async.complete(); //here },e -> { context.fail(e); }); }); async.awaitSuccess();
You can also block code from blocking asynchronous tests:
Single<HttpServer> obs = server.rxListen(9000,"localhost"); obs.toBlocking().value(); //here client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); System.out.println("created client"); Single<HttpResponse<Buffer>> single = client .get(9000,"/foo") .rxSend().subscribeOn(Schedulers.io()); Assert.assertEquals(single.toBlocking().value().bodyAsString(),"foo bar"); //here
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码