Vert.x异步测试

时间:2017-06-27 15:23:17

标签: java vert.x

我是vert.x的新手。我试图运行一些基本的测试和基准等来评估框架(所以我可能做错了很多!)

我感兴趣的一件事是运行'控制器'水平测试。我设置了一个应该反复启动并拆除httpclient的测试。

@Repeat(100)
@Test
public void testMyApplication(TestContext context) {

    final Async async = context.async(1);
    vertx.createHttpClient().getNow(8080, "localhost", "/",
            response -> {
                response.handler(body -> {
                    context.assertTrue(body.toString().contains("Hello"));
                    context.assertEquals(200, response.statusCode());
                    async.complete();
                });
            });
    async.awaitSuccess();
}

然而,这偶尔会失败。

SEVERE: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:8080

什么是更好的方法来启动多个客户端并确保测试按顺序运行或具有一些受控制的并行性?

2 个答案:

答案 0 :(得分:6)

好的,

这里发生的事情是,由于异步调用,有时可能会在调用async.complete之前启动新测试。因此,已经有一个测试服务器在端口8080上运行。

解决方案是使用via config中的pass端口并在测试中使用随机端口。

      server.requestHandler(router::accept)
          .listen(
                  config().getInteger("http.port", 8080),
      ...

测试成为

@Before
public void setUp(TestContext context) throws Exception {
    ServerSocket socket = new ServerSocket(0);
    port = socket.getLocalPort();
    socket.close();

    DeploymentOptions options = new DeploymentOptions()
            .setConfig(new JsonObject().put("http.port", port)
            );
    vertx = Vertx.vertx();
    vertx.deployVerticle(HelloVerticle.class.getName(), options,
            context.asyncAssertSuccess());
}
...

@Repeat(100)
@Test
public void testMyApplication(TestContext context) {

    final Async async = context.async();
    vertx.createHttpClient().getNow(port, "localhost", "/",
            response -> {
                response.handler(body -> {
                    context.assertTrue(body.toString().contains("Hello"));
                    context.assertEquals(200, response.statusCode());
                    async.complete();
                });
            });
}

这篇博文有很多帮助http://vertx.io/blog/vert-x-application-configuration/

答案 1 :(得分:0)

错误显示Connection refused,这意味着没有服务器在端口8080上运行。在您的测试用例中,您需要在端口8080启动一些服务器。

@RunWith(VertxUnitRunner.class)
public class VertxText {
    Vertx vertx;
    HttpServer server;

    @Before
    public void before(TestContext context) {
        vertx = Vertx.vertx();
        server =
                vertx.createHttpServer().requestHandler(req -> req.response().end("Hello")).
                        listen(8080, context.asyncAssertSuccess());
    }

    @After
    public void after(TestContext context) {
        vertx.close(context.asyncAssertSuccess());
    }

    @Repeat(100)
    @Test
    public void testMyApplication(TestContext context) {
        final Async async = context.async(1);
        vertx.createHttpClient().getNow(8080, "localhost", "/",
                response -> {
                    response.handler(body -> {
                        context.assertTrue(body.toString().contains("Hello"));
                        context.assertEquals(200, response.statusCode());
                        async.complete();
                    });
                });
        async.awaitSuccess();
    }
}

在之前,服务器初始化的地方你必须通过你的请求处理程序来测试你的路由。