简体   繁体   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?

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.

相关问题 如何在C#.NET核心中测试查询的执行时间 - How can I test execution time of a query in C# .NET core 如何让我的计算器进行多次计算? (C#) - How can I get my calculator to do more than one calculation? (C#) 如何获取我的C#和MySql从数据库返回多个条目? - How can I get my C# and MySql to return more than one entry from the database? ASP.NET 核心 - 如何从我的 C# class 到我的 Z558B5744CF685F39D34EZ906B 客户端应用程序中获取我的状态代码? - ASP.NET Core - How do I get my status code from my C# class to my TypeScript Client App? 为什么每次通过输入XML或通过C#插入21个以上用户时Excel都会损坏? - Why does my Excel gets corrupted every time I insert more than 21 users by typing XML or through C#? 如何使用 VS Code 在 my.Net Core 项目中手动安装包/引用 - How can I manually install a package/reference in my .Net Core project with VS Code 我的代码在整个 Unity 项目中运行了不止一次 - My code runs more than one time in the whole Unity project 如何在我的 New.Net Core 项目中修复 DbUpdateConcurrencyException? - How Can I Fix DbUpdateConcurrencyException In My New .Net Core Project? 如何将早期版本的开放XML添加到C#项目中? - How can I add an earlier version of open XML to my C# project? ASP.NET C#如何在代码中包含模型? - ASP.NET C# How can I include my models in my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM