Skip to content

Commit e5cebac

Browse files
authored
[browser][mt] fix WebWorkerTest.JSSynchronizationContext_Send_Post_Items_Cancellation (#97775)
1 parent b6e20b8 commit e5cebac

File tree

1 file changed

+10
-3
lines changed
  • src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript

1 file changed

+10
-3
lines changed

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public async Task JSSynchronizationContext_Send_Post_Items_Cancellation()
7474

7575
ManualResetEventSlim blocker = new ManualResetEventSlim(false);
7676
TaskCompletionSource never = new TaskCompletionSource();
77+
TaskCompletionSource canceled = new TaskCompletionSource();
7778
SynchronizationContext capturedSynchronizationContext = null;
7879
TaskCompletionSource jswReady = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
7980
TaskCompletionSource sendReady = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
@@ -95,39 +96,44 @@ public async Task JSSynchronizationContext_Send_Post_Items_Cancellation()
9596
var shouldNotHitSend = false;
9697
var shouldNotHitPost = false;
9798
var hitAfterPost = false;
99+
var hitAfterSend = false;
98100

99-
var canceledSend = Task.Run(() =>
101+
var canceledSend = Task.Run(async () =>
100102
{
101103
// this will be blocked until blocker.Set()
102104
sendReady.SetResult();
105+
await canceled.Task;
103106
capturedSynchronizationContext.Send(_ =>
104107
{
105108
// then it should get canceled and not executed
106109
shouldNotHitSend = true;
107110
}, null);
111+
hitAfterSend = true;
108112
return Task.CompletedTask;
109113
});
110114

111115
var canceledPost = Task.Run(() =>
112116
{
113-
postReady.SetResult();
114117
capturedSynchronizationContext.Post(_ =>
115118
{
116119
// then it should get canceled and not executed
117120
shouldNotHitPost = true;
118121
}, null);
119122
hitAfterPost = true;
123+
postReady.SetResult();
120124
return Task.CompletedTask;
121125
});
122126

123127
// make sure that jobs got the chance to enqueue
124128
await sendReady.Task;
125129
await postReady.Task;
126-
await Task.Delay(100);
127130

128131
// this could should be delivered immediately
129132
cts.Cancel();
130133

134+
// now we release helpers to use capturedSynchronizationContext
135+
canceled.SetResult();
136+
131137
// this will unblock the current pending work item
132138
blocker.Set();
133139

@@ -137,6 +143,7 @@ public async Task JSSynchronizationContext_Send_Post_Items_Cancellation()
137143
Assert.False(shouldNotHitSend);
138144
Assert.False(shouldNotHitPost);
139145
Assert.True(hitAfterPost);
146+
Assert.False(hitAfterSend);
140147
}
141148

142149
[Fact]

0 commit comments

Comments
 (0)