Vert.x 异步测试

Vert.x Async Testing

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

我感兴趣的一件事是 运行 宁 'controller' 级别测试的性能成本。我设置了一个应该反复启动和拆除 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

启动多个客户端并确保测试 运行 按顺序或以某种受控的并行性进行测试的更好方法是什么?

错误显示 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();
    }
}

之前,在初始化服务器的地方,您必须传递将测试您的路由的请求处理程序。

好的,

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

解决方案是通过配置传递一个端口并在测试中使用随机端口。

      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();
                });
            });
}

这个博客post帮助很大http://vertx.io/blog/vert-x-application-configuration/