2017-06-27 143 views
0

我是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 

什么是一个更好的方式来启动多个客户端,并确保测试运行顺序或一些控制并行?

回答

4

OK,

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

解决方法是在via config中使用传递端口,并在测试中使用随机端口。

 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/

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

一些服务器在面前,其中服务器是越来越初始化你将不得不通过你的请求处理器来测试你的路由。

+0

没有,这里的问题是,端口需要进行的测试,在测试过程中没有回收。不管怎么说,还是要谢谢你 –