Skip to content

Commit a4557f5

Browse files
committed
Avoid delays in async and cancellation tests
1 parent 4a74ec9 commit a4557f5

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/DocumentTransformerTests.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,14 @@ public async Task DocumentTransformer_RespectsOperationCancellation()
249249
var options = new OpenApiOptions();
250250
var transformerCalled = false;
251251
var exceptionThrown = false;
252+
var tcs = new TaskCompletionSource();
252253

253254
options.AddDocumentTransformer(async (document, context, cancellationToken) =>
254255
{
255256
transformerCalled = true;
256257
try
257258
{
258-
await Task.Delay(5000, cancellationToken);
259+
await tcs.Task.WaitAsync(cancellationToken);
259260
document.Info.Description = "Should not be set";
260261
}
261262
catch (OperationCanceledException)
@@ -266,7 +267,7 @@ public async Task DocumentTransformer_RespectsOperationCancellation()
266267
});
267268

268269
using var cts = new CancellationTokenSource();
269-
cts.CancelAfter(100);
270+
cts.CancelAfter(1);
270271

271272
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
272273
{
@@ -287,7 +288,6 @@ public async Task DocumentTransformer_ExecutesAsynchronously()
287288
var transformerOrder = new List<int>();
288289
var tcs1 = new TaskCompletionSource();
289290
var tcs2 = new TaskCompletionSource();
290-
var tcs2Called = false;
291291

292292
options.AddDocumentTransformer(async (document, context, cancellationToken) =>
293293
{
@@ -300,11 +300,7 @@ public async Task DocumentTransformer_ExecutesAsynchronously()
300300
{
301301
transformerOrder.Add(2);
302302
document.Info.Title += " Second";
303-
if (!tcs2Called)
304-
{
305-
tcs2Called = true;
306-
tcs2.TrySetResult();
307-
}
303+
tcs2.TrySetResult();
308304
return Task.CompletedTask;
309305
});
310306

@@ -320,7 +316,7 @@ public async Task DocumentTransformer_ExecutesAsynchronously()
320316
Assert.Equal("First Second Third", document.Info.Title);
321317
});
322318

323-
await Task.Delay(100);
319+
await Task.Yield();
324320
tcs1.TrySetResult();
325321

326322
await documentTask;

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/OperationTransformerTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,13 +580,14 @@ public async Task OperationTransformer_RespectsOperationCancellation()
580580
var options = new OpenApiOptions();
581581
var transformerCalled = false;
582582
var exceptionThrown = false;
583+
var tcs = new TaskCompletionSource();
583584

584585
options.AddOperationTransformer(async (operation, context, cancellationToken) =>
585586
{
586587
transformerCalled = true;
587588
try
588589
{
589-
await Task.Delay(5000, cancellationToken);
590+
await tcs.Task.WaitAsync(cancellationToken);
590591
operation.Description = "Should not be set";
591592
}
592593
catch (OperationCanceledException)
@@ -597,7 +598,7 @@ public async Task OperationTransformer_RespectsOperationCancellation()
597598
});
598599

599600
using var cts = new CancellationTokenSource();
600-
cts.CancelAfter(100);
601+
cts.CancelAfter(1);
601602

602603
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
603604
{
@@ -647,7 +648,7 @@ public async Task OperationTransformer_ExecutesAsynchronously()
647648
Assert.Equal("First Second Third", operation.Description);
648649
});
649650

650-
await Task.Delay(100);
651+
await Task.Yield();
651652
tcs1.TrySetResult();
652653

653654
await documentTask;

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/SchemaTransformerTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,13 +857,15 @@ public async Task SchemaTransformer_RespectsOperationCancellation()
857857
var options = new OpenApiOptions();
858858
var transformerCalled = false;
859859
var exceptionThrown = false;
860+
var tcs = new TaskCompletionSource();
860861

862+
//Assert that transformers wait for completion signal from sibling tasks before running
861863
options.AddSchemaTransformer(async (schema, context, cancellationToken) =>
862864
{
863865
transformerCalled = true;
864866
try
865867
{
866-
await Task.Delay(5000, cancellationToken);
868+
await tcs.Task.WaitAsync(cancellationToken);
867869
schema.Description = "Should not be set";
868870
}
869871
catch (OperationCanceledException)
@@ -874,7 +876,7 @@ public async Task SchemaTransformer_RespectsOperationCancellation()
874876
});
875877

876878
using var cts = new CancellationTokenSource();
877-
cts.CancelAfter(100);
879+
cts.CancelAfter(1);
878880

879881
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
880882
{
@@ -896,7 +898,6 @@ public async Task SchemaTransformer_ExecutesAsynchronously()
896898
var tcs1 = new TaskCompletionSource();
897899
var tcs2 = new TaskCompletionSource();
898900

899-
// Assert that transformers wait for completion signal from sibling tasks before running
900901
options.AddSchemaTransformer(async (schema, context, cancellationToken) =>
901902
{
902903
await tcs1.Task;
@@ -926,7 +927,7 @@ public async Task SchemaTransformer_ExecutesAsynchronously()
926927
Assert.Equal("First Second Third", schema.Description);
927928
});
928929

929-
await Task.Delay(100);
930+
await Task.Yield();
930931
tcs1.TrySetResult();
931932

932933
await documentTask;

0 commit comments

Comments
 (0)