Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[browser][mt] fix WebWorkerTest.JSSynchronizationContext_Send_Post_Items_Cancellation #97775

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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