Skip to content

Commit

Permalink
chore: fix benchmark cancellation for ServerStreaming Scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
guitarrapc committed Nov 2, 2024
1 parent 973e5e7 commit f81c513
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Grpc.Core;
using Grpc.Net.Client;
using MagicOnion.Client;
using PerformanceTest.Shared;
Expand All @@ -19,13 +20,21 @@ public ValueTask PrepareAsync(GrpcChannel channel)
// So most times MoveNext won't wait at all, and it may wait occasionally.
public async ValueTask RunAsync(int connectionId, PerformanceTestRunningContext ctx, CancellationToken cancellationToken)
{
var stream = await client.ServerStreamingAsync();
while(!cancellationToken.IsCancellationRequested)
using var stream = await client.ServerStreamingAsync();
while (!cancellationToken.IsCancellationRequested)
{
ctx.Increment();
var begin = timeProvider.GetTimestamp();
await stream.ResponseStream.MoveNext(cancellationToken);
ctx.Latency(connectionId, timeProvider.GetElapsedTime(begin));
try
{
await stream.ResponseStream.MoveNext(cancellationToken);
ctx.Latency(connectionId, timeProvider.GetElapsedTime(begin));
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled && cancellationToken.IsCancellationRequested)
{
// canceling call is expected behavior.
break;
}
}
}

Expand Down

0 comments on commit f81c513

Please sign in to comment.