diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs index 63dc4460754fa..ba7ece2913241 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs @@ -74,6 +74,7 @@ public async Task JSSynchronizationContext_Send_Post_Items_Cancellation() ManualResetEventSlim blocker = new ManualResetEventSlim(false); TaskCompletionSource never = new TaskCompletionSource(); + TaskCompletionSource canceled = new TaskCompletionSource(); SynchronizationContext capturedSynchronizationContext = null; TaskCompletionSource jswReady = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); TaskCompletionSource sendReady = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -95,39 +96,44 @@ public async Task JSSynchronizationContext_Send_Post_Items_Cancellation() var shouldNotHitSend = false; var shouldNotHitPost = false; var hitAfterPost = false; + var hitAfterSend = false; - var canceledSend = Task.Run(() => + var canceledSend = Task.Run(async () => { // this will be blocked until blocker.Set() sendReady.SetResult(); + await canceled.Task; capturedSynchronizationContext.Send(_ => { // then it should get canceled and not executed shouldNotHitSend = true; }, null); + hitAfterSend = true; return Task.CompletedTask; }); var canceledPost = Task.Run(() => { - postReady.SetResult(); capturedSynchronizationContext.Post(_ => { // then it should get canceled and not executed shouldNotHitPost = true; }, null); hitAfterPost = true; + postReady.SetResult(); return Task.CompletedTask; }); // make sure that jobs got the chance to enqueue await sendReady.Task; await postReady.Task; - await Task.Delay(100); // this could should be delivered immediately cts.Cancel(); + // now we release helpers to use capturedSynchronizationContext + canceled.SetResult(); + // this will unblock the current pending work item blocker.Set(); @@ -137,6 +143,7 @@ public async Task JSSynchronizationContext_Send_Post_Items_Cancellation() Assert.False(shouldNotHitSend); Assert.False(shouldNotHitPost); Assert.True(hitAfterPost); + Assert.False(hitAfterSend); } [Fact]