簡體   English   中英

使用CountDownLatch等待http響應從vertx WebClient返回

[英]Waiting for an http response to return from vertx WebClient using CountDownLatch

我正在為某些Rest API編寫一些測試,我有一個調度程序,可以將我的Rest請求從vertx分離到WebClient,在某些情況下,我想等待Rest API的響應返回,然后再繼續我的斷言,分派請求的代碼包裝在其他類中,所以我沒有直接從測試中發出這些請求,我從請求分派器中實現了2種實現,一個實現用於生產,一個實現用於測試,測試分派器如下所示:

public class TestRequestDispatcher extends AbstractRequestDispatcher {

    @Override
    protected void dispatchRequest(ServerRequest request, ServerRequestEventFactory requestEventFactory) {

        request.getSender()
                .send(request,
                        new ServerRequestCallBack() {

                            @Override
                            public <T> void onSuccess(T response) {
                                requestEventFactory.makeSuccess(request, response).fire();
                            }

                            @Override
                            public void onFailure(FailedResponseBean failedResponse) {
                                requestEventFactory.makeFailed(request, failedResponse).fire();
                            }
                        });
    }
}

然后應調用一些代碼來構建WebClient並調用其send方法將請求發送到服務器。

為了等待響應,我決定使用CountDownLatch並將我的代碼修改為以下內容

public class TestRequestDispatcher extends AbstractRequestDispatcher {

    @Override
    protected void dispatchRequest(ServerRequest request, ServerRequestEventFactory requestEventFactory) {

        CountDownLatch requestWait = new CountDownLatch(1);

        request.getSender()
                .send(request,
                        new ServerRequestCallBack() {

                            @Override
                            public <T> void onSuccess(T response) {
                                requestWait.countDown();
                                requestEventFactory.makeSuccess(request, response).fire();
                            }

                            @Override
                            public void onFailure(FailedResponseBean failedResponse) {
                                requestWait.countDown();
                                requestEventFactory.makeFailed(request, failedResponse).fire();
                            }
                        });

        try {
            requestWait.await(20, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

我在此處使用較大的超時來確保響應應在超時結束之前返回,所以發生的事情是我可以斷點並查看正在調用的WebCLient.send方法,然后在requestWait.wait(...)但直到CountDownLatch超時結束才調用回調。 當我期望WebClient發送請求時,無論何時返回響應,它都會調用回調,而回調將倒計時並中斷timwout啟動之前的等待。

用正常的線程進行測試似乎可以正常工作,我創建了一個具有一些睡眠時間的可運行類..除了CountDownTime閂鎖。 像下面這樣

public class SenderWorker implements Runnable {

    private CountDownLatch countDownLatch;

    public SenderWorker(CountDownLatch countDownLatch) {

        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(5000L);
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

然后在調度程序中:

public class TestRequestDispatcher extends AbstractRequestDispatcher {

    @Override
    protected void dispatchRequest(ServerRequest request, ServerRequestEventFactory requestEventFactory) {

        CountDownLatch requestWait = new CountDownLatch(1);
        new Thread(new SenderWorker(requestWait))
                .start();

        try {
            requestWait.await(20, TimeUnit.SECONDS);
            System.out.println("i am here");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

這有效..它調用run方法..sleeps,然后調用requestWait.wait(..)並在5秒鍾后退出等待狀態。

我試圖執行在executeBlocking內調用WebClient的代碼,還嘗試了runOnContext甚至試圖在線程內運行它,就像我對SenderWorker所做的SenderWorker但結果仍然相同.. WebClient被阻塞,直到超時起來。

知道我在這里做錯了什么以及如何使它可行。?!

您可能需要考慮使用vertx-unitvertx-junit5來測試Vert.x的異步代碼。

除此之外,異步操作應該組成,而不是旋轉線程並等待倒計時鎖存器。 Vert.x提供了幾種方法可以做到這一點:

  • 回調鏈
  • 未來組成
  • RxJava(1和2)
  • Kotlin協程
  • 類星體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM