Skip to content

Commit f66e95c

Browse files
committed
Merge in 'release/6.0' changes
2 parents 5c543c7 + 7d82ac7 commit f66e95c

File tree

7 files changed

+424
-17
lines changed

7 files changed

+424
-17
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
<Compile Include="System\Runtime\InteropServices\JavaScript\HelperMarshal.cs" />
1818
<Compile Include="System\Runtime\InteropServices\JavaScript\Http\HttpRequestMessageTest.cs" />
1919
<Compile Include="System\Runtime\InteropServices\JavaScript\ParallelTests.cs" />
20+
21+
<!-- tests which are not working well in XHarness + XUnit -->
22+
<Compile Include="System\Runtime\InteropServices\JavaScript\Simple\TimerTests.cs" />
23+
<Compile Include="System\Runtime\InteropServices\JavaScript\Simple\SimpleTest.cs" />
24+
</ItemGroup>
25+
<ItemGroup>
26+
<WasmExtraFilesToDeploy Include="simple.html" />
27+
<WasmExtraFilesToDeploy Include="simple.js" />
2028
</ItemGroup>
2129
<ItemGroup>
2230
<!-- Part of the shared framework but not exposed. -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
7+
namespace System.Runtime.InteropServices.JavaScript.Tests
8+
{
9+
public static class SimpleTest
10+
{
11+
public static async Task<int> Test()
12+
{
13+
var tests = new List<Func<Task>>();
14+
tests.Add(TimerTests.T0_NoTimer);
15+
tests.Add(TimerTests.T1_OneTimer);
16+
tests.Add(TimerTests.T2_SecondTimerEarlier);
17+
tests.Add(TimerTests.T3_SecondTimerLater);
18+
tests.Add(TimerTests.T5_FiveTimers);
19+
20+
try
21+
{
22+
Console.WriteLine("SimpleMain start test!");
23+
var failures = 0;
24+
var failureNames = new List<string>();
25+
foreach (var test in tests)
26+
{
27+
var failed = await RunTest(test);
28+
if (failed != null)
29+
{
30+
failureNames.Add(failed);
31+
failures++;
32+
}
33+
}
34+
35+
foreach (var failure in failureNames)
36+
{
37+
Console.WriteLine(failure);
38+
}
39+
Console.WriteLine($"{Environment.NewLine}=== TEST EXECUTION SUMMARY ==={Environment.NewLine}Total: {tests.Count}, Failed: {failures}");
40+
return failures;
41+
}
42+
catch (Exception ex)
43+
{
44+
Console.WriteLine(ex.ToString());
45+
return -1;
46+
}
47+
}
48+
49+
private static async Task<string> RunTest(Func<Task> action)
50+
{
51+
try
52+
{
53+
Console.WriteLine("[STRT] " + action.Method.Name);
54+
await action();
55+
Console.WriteLine("[DONE] " + action.Method.Name);
56+
return null;
57+
}
58+
catch (Exception ex)
59+
{
60+
var message="[FAIL] "+action.Method.Name + " " + ex.Message;
61+
Console.WriteLine(message);
62+
return message;
63+
}
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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 System.Threading;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace System.Runtime.InteropServices.JavaScript.Tests
9+
{
10+
public static class TimerTests
11+
{
12+
static JSObject _timersHelper = (JSObject)Runtime.GetGlobalObject("timersHelper");
13+
static Function _installWrapper = (Function)_timersHelper.GetObjectProperty("install");
14+
static Function _getRegisterCount = (Function)_timersHelper.GetObjectProperty("getRegisterCount");
15+
static Function _getHitCount = (Function)_timersHelper.GetObjectProperty("getHitCount");
16+
static Function _cleanupWrapper = (Function)_timersHelper.GetObjectProperty("cleanup");
17+
18+
static public async Task T0_NoTimer()
19+
{
20+
try
21+
{
22+
_installWrapper.Call();
23+
24+
var setCounter = (int)_getRegisterCount.Call();
25+
Assert.Equal(0, setCounter);
26+
}
27+
finally
28+
{
29+
await WaitForCleanup();
30+
}
31+
}
32+
33+
static public async Task T1_OneTimer()
34+
{
35+
int wasCalled = 0;
36+
Timer? timer = null;
37+
try
38+
{
39+
_installWrapper.Call();
40+
41+
timer = new Timer((_) =>
42+
{
43+
Console.WriteLine("In timer");
44+
wasCalled++;
45+
}, null, 10, 0);
46+
47+
var setCounter = (int)_getRegisterCount.Call();
48+
Assert.True(0 == wasCalled, $"wasCalled: {wasCalled}");
49+
Assert.True(1 == setCounter, $"setCounter: {setCounter}");
50+
}
51+
finally
52+
{
53+
await WaitForCleanup();
54+
Assert.True(1 == wasCalled, $"wasCalled: {wasCalled}");
55+
timer?.Dispose();
56+
}
57+
}
58+
59+
static public async Task T2_SecondTimerEarlier()
60+
{
61+
int wasCalled = 0;
62+
Timer? timer1 = null;
63+
Timer? timer2 = null;
64+
try
65+
{
66+
_installWrapper.Call();
67+
68+
timer1 = new Timer((_) =>
69+
{
70+
Console.WriteLine("In timer1");
71+
wasCalled++;
72+
}, null, 10, 0);
73+
timer2 = new Timer((_) =>
74+
{
75+
Console.WriteLine("In timer2");
76+
wasCalled++;
77+
}, null, 5, 0);
78+
79+
var setCounter = (int)_getRegisterCount.Call();
80+
Assert.True(2 == setCounter, $"setCounter: {setCounter}");
81+
Assert.True(0 == wasCalled, $"wasCalled: {wasCalled}");
82+
83+
}
84+
finally
85+
{
86+
await WaitForCleanup();
87+
Assert.True(2 == wasCalled, $"wasCalled: {wasCalled}");
88+
timer1?.Dispose();
89+
timer2?.Dispose();
90+
}
91+
}
92+
93+
static public async Task T3_SecondTimerLater()
94+
{
95+
int wasCalled = 0;
96+
Timer? timer1 = null;
97+
Timer? timer2 = null;
98+
try
99+
{
100+
_installWrapper.Call();
101+
102+
timer1 = new Timer((_) =>
103+
{
104+
Console.WriteLine("In timer1");
105+
wasCalled++;
106+
}, null, 10, 0);
107+
timer2 = new Timer((_) =>
108+
{
109+
Console.WriteLine("In timer2");
110+
wasCalled++;
111+
}, null, 20, 0);
112+
113+
var setCounter = (int)_getRegisterCount.Call();
114+
Assert.True(0 == wasCalled, $"wasCalled: {wasCalled}");
115+
Assert.True(1 == setCounter, $"setCounter: {setCounter}");
116+
}
117+
finally
118+
{
119+
await WaitForCleanup();
120+
Assert.True(2 == wasCalled, $"wasCalled: {wasCalled}");
121+
timer1?.Dispose();
122+
timer2?.Dispose();
123+
}
124+
}
125+
126+
static public async Task T5_FiveTimers()
127+
{
128+
int wasCalled = 0;
129+
Timer? timer1 = null;
130+
Timer? timer2 = null;
131+
Timer? timer3 = null;
132+
Timer? timer4 = null;
133+
Timer? timer5 = null;
134+
try
135+
{
136+
_installWrapper.Call();
137+
138+
timer1 = new Timer((_) =>
139+
{
140+
Console.WriteLine("In timer1");
141+
wasCalled++;
142+
}, null, 800, 0);
143+
timer2 = new Timer((_) =>
144+
{
145+
Console.WriteLine("In timer2");
146+
wasCalled++;
147+
}, null, 600, 0);
148+
timer3 = new Timer((_) =>
149+
{
150+
Console.WriteLine("In timer3");
151+
wasCalled++;
152+
}, null, 400, 0);
153+
timer4 = new Timer((_) =>
154+
{
155+
Console.WriteLine("In timer4");
156+
wasCalled++;
157+
}, null, 200, 0);
158+
timer5 = new Timer((_) =>
159+
{
160+
Console.WriteLine("In timer5");
161+
wasCalled++;
162+
}, null, 000, 0);
163+
164+
var setCounter = (int)_getRegisterCount.Call();
165+
Assert.True(0 == wasCalled, $"wasCalled: {wasCalled}");
166+
Assert.True(5 == setCounter, $"setCounter: {setCounter}");
167+
}
168+
finally
169+
{
170+
await WaitForCleanup();
171+
var hitCounter = (int)_getHitCount.Call();
172+
var setCounter = (int)_getRegisterCount.Call();
173+
Assert.True(5 == wasCalled, $"wasCalled: {wasCalled}");
174+
Assert.True(8 == hitCounter, $"hitCounter: {hitCounter}");
175+
Assert.True(12 == setCounter, $"setCounter: {setCounter}");
176+
timer1?.Dispose();
177+
timer2?.Dispose();
178+
timer3?.Dispose();
179+
timer4?.Dispose();
180+
timer5?.Dispose();
181+
}
182+
}
183+
184+
static private async Task WaitForCleanup()
185+
{
186+
Console.WriteLine("wait for cleanup begin");
187+
await Task.Delay(1000);
188+
_cleanupWrapper.Call();
189+
Console.WriteLine("wait for cleanup end");
190+
}
191+
}
192+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<!-- Licensed to the .NET Foundation under one or more agreements. -->
3+
<!-- The .NET Foundation licenses this file to you under the MIT license. -->
4+
<html>
5+
<head>
6+
<title>TESTS</title>
7+
<meta charset="UTF-8">
8+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
9+
</head>
10+
<body>
11+
<script type="text/javascript" src="simple.js"></script>
12+
<script defer src="dotnet.js"></script>
13+
</body>
14+
</html>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
class TimersHelper {
2+
install() {
3+
const measuredCallbackName = "mono_wasm_set_timeout_exec";
4+
globalThis.registerCounter = 0;
5+
globalThis.hitCounter = 0;
6+
console.log("install")
7+
if (!globalThis.originalSetTimeout) {
8+
globalThis.originalSetTimeout = globalThis.setTimeout;
9+
}
10+
globalThis.setTimeout = (cb, time) => {
11+
var start = Date.now().valueOf();
12+
if (cb.name === measuredCallbackName) {
13+
globalThis.registerCounter++;
14+
console.log(`registerCounter: ${globalThis.registerCounter} now:${start} delay:${time}`)
15+
}
16+
return globalThis.originalSetTimeout(() => {
17+
if (cb.name === measuredCallbackName) {
18+
var hit = Date.now().valueOf();
19+
globalThis.hitCounter++;
20+
var delta = hit - start;
21+
console.log(`hitCounter: ${globalThis.hitCounter} now:${hit} delay:${time} delta:${delta}`)
22+
}
23+
cb();
24+
}, time);
25+
};
26+
}
27+
28+
getRegisterCount() {
29+
console.log(`registerCounter: ${globalThis.registerCounter} `)
30+
return globalThis.registerCounter;
31+
}
32+
33+
getHitCount() {
34+
console.log(`hitCounter: ${globalThis.hitCounter} `)
35+
return globalThis.hitCounter;
36+
}
37+
38+
cleanup() {
39+
console.log(`cleanup registerCounter: ${globalThis.registerCounter} hitCounter: ${globalThis.hitCounter} `)
40+
globalThis.setTimeout = globalThis.originalSetTimeout;
41+
}
42+
}
43+
44+
globalThis.timersHelper = new TimersHelper();
45+
46+
var Module = {
47+
48+
config: null,
49+
50+
preInit: async function() {
51+
await MONO.mono_wasm_load_config("./mono-config.json"); // sets Module.config implicitly
52+
},
53+
54+
// Called when the runtime is initialized and wasm is ready
55+
onRuntimeInitialized: function () {
56+
if (!Module.config || Module.config.error) {
57+
console.log("No config found");
58+
return;
59+
}
60+
61+
Module.config.loaded_cb = function () {
62+
try {
63+
BINDING.call_static_method("[System.Private.Runtime.InteropServices.JavaScript.Tests] System.Runtime.InteropServices.JavaScript.Tests.SimpleTest:Test", []);
64+
} catch (error) {
65+
throw (error);
66+
}
67+
};
68+
Module.config.fetch_file_cb = function (asset) {
69+
return fetch (asset, { credentials: 'same-origin' });
70+
}
71+
72+
try
73+
{
74+
MONO.mono_load_runtime_and_bcl_args (Module.config);
75+
} catch (error) {
76+
throw(error);
77+
}
78+
},
79+
};

0 commit comments

Comments
 (0)