Skip to content

Commit

Permalink
[browser][mt] fix WebWorkerTest.JSSynchronizationContext_Send_Post_It…
Browse files Browse the repository at this point in the history
…ems_Cancellation (dotnet#97775)
  • Loading branch information
pavelsavara committed Feb 1, 2024
1 parent b6e20b8 commit e5cebac
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();

Expand All @@ -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]
Expand Down

0 comments on commit e5cebac

Please sign in to comment.