加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

单元测试 – 单元测试Vertx – java.util.concurrent.TimeoutExc

发布时间:2020-12-15 04:39:11 所属栏目:Java 来源:网络整理
导读:我正在尝试使用VertxUnitRunner和Vertx的RXified版本从Vertx WebClient单元测试http调用. 问题是我的单元测试总是因超时异常而失败.单元测试WebClient http调用有不同的方法吗?以下是我的代码: import io.vertx.core.AsyncResult;import io.vertx.core.htt
我正在尝试使用VertxUnitRunner和Vertx的RXified版本从Vertx WebClient单元测试http调用.

问题是我的单元测试总是因超时异常而失败.单元测试WebClient http调用有不同的方法吗?以下是我的代码:

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

由于120秒后超时,测试总是失败

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)

解决方法

因为你的async使用是错误的.它类似于java CountDownLatch.它在 docs中描述

正确的用法是:

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

您还可以阻止代码阻止异步测试:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读