[英]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. 我正在进行数据挖掘,理论上我的代码应该执行50个请求的批处理,并行运行特定的批处理,但是会阻止该批处理完成。
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. 我需要确保它实际上按预期运行,并且不会打开50个以上的传出连接-但我不知道如何访问该信息,并持续监视它以编写自动测试。
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: 我什至不确定我要在测试中为任何批次创建外发请求,因为我正在像这样模拟我的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)
});
}
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. 任何建议都将不胜感激-我将在本地计算机上旋转自己的独立服务器,并以1秒的延迟返回固定响应,以确保我不会超过开放连接限制并引发异常。.但这仍然是手动测试。
Craig, I suppose you use moq
library? 克雷格,我想你用的是
moq
库?
Give a try with .Callback(() => Interlocked.Increment(ref calls));
试试看
.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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.