vert.x:你如何正确发送帖子请求?

时间:2016-09-16 13:53:13

标签: java unit-testing post reactive-programming vert.x

我有vert.x个app-server,拥有最简单的授权,其路由如下:

router.post("/user/sign").handler(this::userSignIn);

虽然我是从GUI执行此操作,但它运行正常。

但我想为此操作创建Unit-Test。 我做的代码:

@Test
public void testServerUserRegister(TestContext context) {
  HttpClient client = vertx.createHttpClient();
  HttpClientRequest request = client.post("user/sign", response -> {
  System.out.println("Some callback " + response.statusCode());
  });

  String body = "{'username':'www','password':'www'}";
  request.putHeader("content-length", "1000");
  request.putHeader("content-type", "application/x-www-form-urlencoded");
  request.write(body);
  request.end();
}

当我开始此测试时:mvn test

我看到了结果但是:

1-我没有看到任何类似的字符串"有些回调......"

2-我得到RejectedExecutionException

вер. 16, 2016 4:29:20 PM io.vertx.core.http.impl.HttpClientImpl
  SEVERE: java.nio.channels.ClosedChannelException
  вер. 16, 2016 4:29:20 PM io.netty.channel.AbstractChannel$AbstractUnsafe invokeLater
  WARNING: Can't invoke task later as EventLoop rejected it
  java.util.concurrent.RejectedExecutionException: event executor terminated

  at io.netty.util.concurrent.SingleThreadEventExecutor
     .reject(SingleThreadEventExecutor.java:707)
  at io.netty.util.concurrent.SingleThreadEventExecutor
     .addTask(SingleThreadEventExecutor.java:299)
  at io.netty.util.concurrent.SingleThreadEventExecutor
     .execute(SingleThreadEventExecutor.java:687)
  at io.netty.channel.AbstractChannel$AbstractUnsafe
     .invokeLater(AbstractChannel.java:826)
  at io.netty.channel.AbstractChannel$AbstractUnsafe
     .deregister(AbstractChannel.java:656)
  at io.netty.channel.AbstractChannel$AbstractUnsafe
     .fireChannelInactiveAndDeregister(AbstractChannel.java:626)
  at io.netty.channel.AbstractChannel$AbstractUnsafe
     .close(AbstractChannel.java:600)
  at io.netty.channel.nio.NioEventLoop.closeAll(NioEventLoop.java:576)
  at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:361)
  at io.netty.util.concurrent.SingleThreadEventExecutor$2
     .run(SingleThreadEventExecutor.java:111)
  at java.lang.Thread.run(Thread.java:745)

知道我在这里做错了什么吗?

1 个答案:

答案 0 :(得分:4)

调用request.end()后,JUnit测试退出,Vert.x实例关闭(因此RejectedExecutionException)。

请记住,Vert.x API是异步的。看看Vert.x Unit section about asynchronous testing

你应该做的是:

@Test
public void testServerUserRegister(TestContext context) {
  // Get an async object to control the completion of the test
  Async async = context.async();
  HttpClient client = vertx.createHttpClient();
  HttpClientRequest request = client.post("user/sign", response -> {
    // You may want to check response code here
    // to either complete or fail the test
    async.complete();
    System.out.println("Some callback " + response.statusCode());
  });

  String body = "{'username':'www','password':'www'}";
  request.putHeader("content-length", "1000");
  request.putHeader("content-type", "application/x-www-form-urlencoded");
  request.write(body);
  request.end();
}