Skip to content

Commit 36ca267

Browse files
authored
[browser] test that Task.Yield yields to browser event loop (#113531)
1 parent 2a28518 commit 36ca267

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\JavaScript\JavaScriptTestHelper.cs" />
3333
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\JavaScript\JSImportTest.cs" />
3434
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\JavaScript\JSExportTest.cs" />
35+
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\JavaScript\YieldAwaitableTests.cs" />
3536
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\JavaScript\JSInteropTestBase.cs" />
3637
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\JavaScript\Utils.cs" />
3738

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,15 @@ public static JSObject EchoIJSObject([JSMarshalAs<JSType.Object>] JSObject arg1)
10321032
return arg1;
10331033
}
10341034

1035+
[JSImport("beforeYield", "JavaScriptTestHelper")]
1036+
public static partial void BeforeYield();
1037+
1038+
[JSImport("isSetTimeoutHit", "JavaScriptTestHelper")]
1039+
public static partial bool IsSetTimeoutHit();
1040+
1041+
[JSImport("isPromiseThenHit", "JavaScriptTestHelper")]
1042+
public static partial bool IsPromiseThenHit();
1043+
10351044
[JSImport("callJavaScriptLibrary", "JavaScriptTestHelper")]
10361045
public static partial Task<int> callJavaScriptLibrary(int a, int b);
10371046

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,28 @@ export function delay(ms) {
466466
export function reject(what) {
467467
return new Promise((_, reject) => globalThis.setTimeout(() => reject(what), 0));
468468
}
469+
470+
let setTimeoutHit = false;
471+
let promiseThenHit = false;
472+
export function beforeYield() {
473+
setTimeoutHit = false;
474+
promiseThenHit = false;
475+
setTimeout(() => {
476+
setTimeoutHit = true;
477+
}, 0);
478+
let res;
479+
new Promise((resolve) => {
480+
res = resolve;
481+
}).then(() => {
482+
promiseThenHit = true;
483+
});
484+
res();
485+
}
486+
487+
export function isSetTimeoutHit() {
488+
return setTimeoutHit;
489+
}
490+
491+
export function isPromiseThenHit() {
492+
return promiseThenHit;
493+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Runtime.CompilerServices;
8+
using System.Text;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
12+
namespace System.Runtime.InteropServices.JavaScript.Tests
13+
{
14+
public class YieldAwaitableTests : IAsyncLifetime
15+
{
16+
[Fact]
17+
public async Task TaskYieldsToBrowserLoop()
18+
{
19+
JavaScriptTestHelper.BeforeYield();
20+
await Task.Yield();
21+
Assert.True(JavaScriptTestHelper.IsSetTimeoutHit());
22+
Assert.True(JavaScriptTestHelper.IsPromiseThenHit());
23+
}
24+
25+
[Fact]
26+
public async Task TaskDelay0DoesNotYieldToBrowserLoop()
27+
{
28+
JavaScriptTestHelper.BeforeYield();
29+
await Task.Delay(0);
30+
Assert.False(JavaScriptTestHelper.IsSetTimeoutHit());
31+
Assert.False(JavaScriptTestHelper.IsPromiseThenHit());
32+
}
33+
34+
[Fact]
35+
public async Task TaskDelay1YieldsToBrowserLoop()
36+
{
37+
JavaScriptTestHelper.BeforeYield();
38+
await Task.Delay(1);
39+
Assert.True(JavaScriptTestHelper.IsSetTimeoutHit());
40+
Assert.True(JavaScriptTestHelper.IsPromiseThenHit());
41+
}
42+
43+
public async Task InitializeAsync()
44+
{
45+
await JavaScriptTestHelper.InitializeAsync();
46+
await Task.Delay(100);
47+
}
48+
49+
public async Task DisposeAsync()
50+
{
51+
await JavaScriptTestHelper.DisposeAsync();
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)