英文:
How to test a gRPC client in a parallel for with NUnit?
问题
I would like to test when many users access to the server at the same time, so I am trying this code:
[Test]
public void Agregar2RegistrosHorariosDeEntradaDe2EmpleadosAsync()
{
try
{
Parallel.For(0, 2,
async iterador =>
{
try
{
await miClienteLogin.LoginAsync(_nombreUsuario, _password);
}
catch(Exception ex)
{
throw;
}
});
}
catch(Exception ex)
{
Assert.Fail("No se debió producir ninguna excepción.");
}
}
But I don't get any exception and the log in the server is this:
[13:38:29 DBG] Connection id "0HMPPMB4T" accepted.
[13:38:29 DBG] Connection id "0HMPPMB4T" started.
[13:38:29 DBG] Connection 0HMPPMB4T established using the following protocol: Tls13
[13:38:29 DBG] Connection id "0HMPPMB4T" reset.
[13:38:29 DBG] Connection id "0HMPPMB4T" is closed. The last processed stream ID was 0.
[13:38:29 DBG] The connection queue processing loop for 0HMPPMB4TUI6R completed.
[13:38:29 DBG] Connection id "0HMPPMB4T" sending FIN because: "The client closed the connection."
[13:38:29 DBG] Connection id "0HMPPMB4T" stopped.
But if I run the same code outside the parallel for, it works as expected.
I guess it has to be a problem when I execute the client command inside a parallel for.
How could I run it inside the parallel for? Or how could I test when many users are requesting something to the server?
Thanks.
英文:
I would like to test when many users access to the server at the same time, so I am trying this code:
[Test]
public void Agregar2RegistrosHorariosDeEntradaDe2EmpleadosAsync()
{
try
{
Parallel.For(0, 2,
async iterador =>
{
try
{
await miClienteLogin.LoginAsync(_nombreUsuario, _password);
}
catch(Exception ex)
{
throw;
}
});
}
catch(Exception ex)
{
Assert.Fail("No se debió producir ninguna excepción.");
}
}
But I don't get any exception and the log in the server is this:
[13:38:29 DBG] Connection id "0HMPPMB4T" accepted.
[13:38:29 DBG] Connection id "0HMPPMB4T" started.
[13:38:29 DBG] Connection 0HMPPMB4T established using the following protocol: Tls13
[13:38:29 DBG] Connection id "0HMPPMB4T" reset.
[13:38:29 DBG] Connection id "0HMPPMB4T" is closed. The last processed stream ID was 0.
[13:38:29 DBG] The connection queue processing loop for 0HMPPMB4TUI6R completed.
[13:38:29 DBG] Connection id "0HMPPMB4T" sending FIN because: "The client closed the connection."
[13:38:29 DBG] Connection id "0HMPPMB4T" stopped.
But If I run the same code outside the parallel for, it works as expected.
I guess it has to be a problem when I execute the client command inside a parallel for.
How could I run it inside the parallel for? Or how could I test when many users are requesting something to the server?
Thanks.
答案1
得分: 1
Parallel.For 不会等待 LoginAsync 完成后再结束循环(它只会启动任务而已)。可以使用 Parallel.ForEachAsync 或 Task.WhenAll。示例代码如下:
[Test]
public async Task Agregar2RegistrosHorariosDeEntradaDe2EmpleadosAsync()
{
try
{
await Task.WhenAll(Enumerable.Range(0, 2)
.Select(_ => miClienteLogin.LoginAsync(_nombreUsuario, _password)));
}
catch (Exception ex)
{
Assert.Fail("No se debió producir ninguna excepción.");
}
}
P.S.
如果要断言是否会引发异常,可以使用 Assert.Throws/Assert.ThrowsAsync。
英文:
Parallel.For is not task-aware so it will not actually wait for LoginAsync to finish before ending the loop (it will just start tasks and that's it). Use Parallel.ForEachAsync or just Task.WhenAll. Something like the following:
[Test]
public async Task Agregar2RegistrosHorariosDeEntradaDe2EmpleadosAsync()
{
try
{
await Task.WhenAll(Enumerable.Range(0, 2)
.Select(_ => miClienteLogin.LoginAsync(_nombreUsuario, _password)));
}
catch (Exception ex)
{
Assert.Fail("No se debió producir ninguna excepción.");
}
}
P.S.
If you want to assert if something throws you can use Assert.Throws/Assert.ThrowsAsync.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论