Skip to content

Commit 54ac556

Browse files
authored
Only ignore OperationCanceledException if cancellationToken is cancelled (Azure#21250)
1 parent 7b2837d commit 54ac556

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Azure.Test.Perf;
5+
using System;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Azure.Sample.Perf
10+
{
11+
// The perf framework should ignore OperationCanceledException thrown by Run/RunAsync(), but only
12+
// if the test is being cancelled. This verifies that an OperationCanceledException thrown earlier
13+
// will cause the perf framework to fail fast.
14+
// https://github.com/Azure/azure-sdk-for-net/issues/21241
15+
16+
public class OperationCanceledTest : PerfTest<PerfOptions>
17+
{
18+
public OperationCanceledTest(PerfOptions options) : base(options)
19+
{
20+
}
21+
22+
public override void Run(CancellationToken cancellationToken)
23+
{
24+
throw new OperationCanceledException();
25+
}
26+
27+
public override Task RunAsync(CancellationToken cancellationToken)
28+
{
29+
throw new OperationCanceledException();
30+
}
31+
}
32+
}

common/Perf/Azure.Test.Perf/PerfProgram.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,16 @@ private static void RunLoop(IPerfTest test, int index, bool latency, Cancellatio
441441
_lastCompletionTimes[index] = sw.Elapsed;
442442
}
443443
}
444-
catch (OperationCanceledException)
444+
catch (Exception e)
445445
{
446+
if (cancellationToken.IsCancellationRequested && PerfStressUtilities.ContainsOperationCanceledException(e))
447+
{
448+
// If the test has been canceled, ignore if any part of the exception chain is OperationCanceledException.
449+
}
450+
else
451+
{
452+
throw;
453+
}
446454
}
447455
}
448456

@@ -484,8 +492,11 @@ private static async Task RunLoopAsync(IPerfTest test, int index, bool latency,
484492
}
485493
catch (Exception e)
486494
{
487-
// Ignore if any part of the exception chain is type OperationCanceledException
488-
if (!PerfStressUtilities.ContainsOperationCanceledException(e))
495+
if (cancellationToken.IsCancellationRequested && PerfStressUtilities.ContainsOperationCanceledException(e))
496+
{
497+
// If the test has been canceled, ignore if any part of the exception chain is OperationCanceledException.
498+
}
499+
else
489500
{
490501
throw;
491502
}

0 commit comments

Comments
 (0)