簡體   English   中英

如何在c#.net核心項目中測試我的代碼一次打開的請求連接數不超過50個?

[英]How can I test that my code does not open more than 50 request connections at a time in my c# .net core project?

我正在進行數據挖掘,理論上我的代碼應該執行50個請求的批處理,並行運行特定的批處理,但是會阻止該批處理完成。

我需要確保它實際上按預期運行,並且不會打開50個以上的傳出連接-但我不知道如何訪問該信息,並持續監視它以編寫自動測試。

我什至不確定我要在測試中為任何批次創建外發請求,因為我正在像這樣模擬我的http客戶端:

protected HttpClient httpClient;
protected Mock<HttpMessageHandlerFake> fakeHttpMessageHandler = new Mock<HttpMessageHandlerFake> { CallBase = true };
httpClient = new HttpClient(fakeHttpMessageHandler.Object);



protected void MockHttpResponse(string url, string mockedResponse)
{
    fakeHttpMessageHandler.Setup(f => f.Send(It.Is<HttpRequestMessage>(x => x.RequestUri.AbsoluteUri == url))).Returns(new HttpResponseMessage
    {
         StatusCode = HttpStatusCode.OK,
         Content = new StringContent(mockedResponse)
    });
}

任何建議都將不勝感激-我將在本地計算機上旋轉自己的獨立服務器,並以1秒的延遲返回固定響應,以確保我不會超過開放連接限制並引發異常。.但這仍然是手動測試。

克雷格,我想你用的是moq庫?

試試看.Callback(() => Interlocked.Increment(ref calls));

例如,在您的課堂上:

private int calls = 0;
public int Calls {get {return Interlocked.Read(ref calls);}}

protected void MockHttpResponse(string url, string mockedResponse)
{
        fakeHttpMessageHandler.Setup(f => f.Send(It.Is<HttpRequestMessage>(x => x.RequestUri.AbsoluteUri == url))).Returns(new HttpResponseMessage
        {
             StatusCode = HttpStatusCode.OK,
             Content = new StringContent(mockedResponse)
        }).Callback(() => Interlocked.Increment(ref calls));
    }

暫無
暫無

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

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