简体   繁体   中英

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

I am doing data mining and in theory my code should do batches of 50 requests, run the specific batch in parallel but be blocking for the batch to finish.

I need to ensure that it is actually running as expected and not opening more than 50 outgoing connections - but I have no idea how to get access to that information, and continuously monitor it for me to write an automated test with.

I am not even sure that I am creating an outgoing request for any batch in my tests since I am mocking my http client like so:

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

Any advice would be appreciated - I am just going to spin up my own separate server on my local machine and return canned responses with 1 second delays to ensure i am not going over my open connection limit and throwing an exception.. but still this is manual testing.

Craig, I suppose you use moq library?

Give a try with .Callback(() => Interlocked.Increment(ref calls));

eg, in your class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM