如何使用NUnit在并行循环中测试gRPC客户端?

huangapple go评论64阅读模式
英文:

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.ForEachAsyncTask.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.

huangapple
  • 本文由 发表于 2023年4月10日 19:44:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定