Skip to content
Merged
Show file tree
Hide file tree
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 @@ -17,6 +17,14 @@
<Compile Include="System\Runtime\InteropServices\JavaScript\HelperMarshal.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Http\HttpRequestMessageTest.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\ParallelTests.cs" />

<!-- tests which are not working well in XHarness + XUnit -->
<Compile Include="System\Runtime\InteropServices\JavaScript\Simple\TimerTests.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Simple\SimpleTest.cs" />
</ItemGroup>
<ItemGroup>
<WasmExtraFilesToDeploy Include="simple.html" />
<WasmExtraFilesToDeploy Include="simple.js" />
</ItemGroup>
<ItemGroup>
<!-- Part of the shared framework but not exposed. -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Threading.Tasks;

namespace System.Runtime.InteropServices.JavaScript.Tests
{
public static class SimpleTest
{
public static async Task<int> Test()
{
var tests = new List<Func<Task>>();
tests.Add(TimerTests.T0_NoTimer);
tests.Add(TimerTests.T1_OneTimer);
tests.Add(TimerTests.T2_SecondTimerEarlier);
tests.Add(TimerTests.T3_SecondTimerLater);
tests.Add(TimerTests.T5_FiveTimers);

try
{
Console.WriteLine("SimpleMain start test!");
var failures = 0;
var failureNames = new List<string>();
foreach (var test in tests)
{
var failed = await RunTest(test);
if (failed != null)
{
failureNames.Add(failed);
failures++;
}
}

foreach (var failure in failureNames)
{
Console.WriteLine(failure);
}
Console.WriteLine($"{Environment.NewLine}=== TEST EXECUTION SUMMARY ==={Environment.NewLine}Total: {tests.Count}, Failed: {failures}");
return failures;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return -1;
}
}

private static async Task<string> RunTest(Func<Task> action)
{
try
{
Console.WriteLine("[STRT] " + action.Method.Name);
await action();
Console.WriteLine("[DONE] " + action.Method.Name);
return null;
}
catch (Exception ex)
{
var message="[FAIL] "+action.Method.Name + " " + ex.Message;
Console.WriteLine(message);
return message;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace System.Runtime.InteropServices.JavaScript.Tests
{
public static class TimerTests
{
static JSObject _timersHelper = (JSObject)Runtime.GetGlobalObject("timersHelper");
static Function _installWrapper = (Function)_timersHelper.GetObjectProperty("install");
static Function _getRegisterCount = (Function)_timersHelper.GetObjectProperty("getRegisterCount");
static Function _getHitCount = (Function)_timersHelper.GetObjectProperty("getHitCount");
static Function _cleanupWrapper = (Function)_timersHelper.GetObjectProperty("cleanup");

static public async Task T0_NoTimer()
{
try
{
_installWrapper.Call();

var setCounter = (int)_getRegisterCount.Call();
Assert.Equal(0, setCounter);
}
finally
{
await WaitForCleanup();
}
}

static public async Task T1_OneTimer()
{
int wasCalled = 0;
Timer? timer = null;
try
{
_installWrapper.Call();

timer = new Timer((_) =>
{
Console.WriteLine("In timer");
wasCalled++;
}, null, 10, 0);

var setCounter = (int)_getRegisterCount.Call();
Assert.True(0 == wasCalled, $"wasCalled: {wasCalled}");
Assert.True(1 == setCounter, $"setCounter: {setCounter}");
}
finally
{
await WaitForCleanup();
Assert.True(1 == wasCalled, $"wasCalled: {wasCalled}");
timer?.Dispose();
}
}

static public async Task T2_SecondTimerEarlier()
{
int wasCalled = 0;
Timer? timer1 = null;
Timer? timer2 = null;
try
{
_installWrapper.Call();

timer1 = new Timer((_) =>
{
Console.WriteLine("In timer1");
wasCalled++;
}, null, 10, 0);
timer2 = new Timer((_) =>
{
Console.WriteLine("In timer2");
wasCalled++;
}, null, 5, 0);

var setCounter = (int)_getRegisterCount.Call();
Assert.True(2 == setCounter, $"setCounter: {setCounter}");
Assert.True(0 == wasCalled, $"wasCalled: {wasCalled}");

}
finally
{
await WaitForCleanup();
Assert.True(2 == wasCalled, $"wasCalled: {wasCalled}");
timer1?.Dispose();
timer2?.Dispose();
}
}

static public async Task T3_SecondTimerLater()
{
int wasCalled = 0;
Timer? timer1 = null;
Timer? timer2 = null;
try
{
_installWrapper.Call();

timer1 = new Timer((_) =>
{
Console.WriteLine("In timer1");
wasCalled++;
}, null, 10, 0);
timer2 = new Timer((_) =>
{
Console.WriteLine("In timer2");
wasCalled++;
}, null, 20, 0);

var setCounter = (int)_getRegisterCount.Call();
Assert.True(0 == wasCalled, $"wasCalled: {wasCalled}");
Assert.True(1 == setCounter, $"setCounter: {setCounter}");
}
finally
{
await WaitForCleanup();
Assert.True(2 == wasCalled, $"wasCalled: {wasCalled}");
timer1?.Dispose();
timer2?.Dispose();
}
}

static public async Task T5_FiveTimers()
{
int wasCalled = 0;
Timer? timer1 = null;
Timer? timer2 = null;
Timer? timer3 = null;
Timer? timer4 = null;
Timer? timer5 = null;
try
{
_installWrapper.Call();

timer1 = new Timer((_) =>
{
Console.WriteLine("In timer1");
wasCalled++;
}, null, 800, 0);
timer2 = new Timer((_) =>
{
Console.WriteLine("In timer2");
wasCalled++;
}, null, 600, 0);
timer3 = new Timer((_) =>
{
Console.WriteLine("In timer3");
wasCalled++;
}, null, 400, 0);
timer4 = new Timer((_) =>
{
Console.WriteLine("In timer4");
wasCalled++;
}, null, 200, 0);
timer5 = new Timer((_) =>
{
Console.WriteLine("In timer5");
wasCalled++;
}, null, 000, 0);

var setCounter = (int)_getRegisterCount.Call();
Assert.True(0 == wasCalled, $"wasCalled: {wasCalled}");
Assert.True(5 == setCounter, $"setCounter: {setCounter}");
}
finally
{
await WaitForCleanup();
var hitCounter = (int)_getHitCount.Call();
var setCounter = (int)_getRegisterCount.Call();
Assert.True(5 == wasCalled, $"wasCalled: {wasCalled}");
Assert.True(8 == hitCounter, $"hitCounter: {hitCounter}");
Assert.True(12 == setCounter, $"setCounter: {setCounter}");
timer1?.Dispose();
timer2?.Dispose();
timer3?.Dispose();
timer4?.Dispose();
timer5?.Dispose();
}
}

static private async Task WaitForCleanup()
{
Console.WriteLine("wait for cleanup begin");
await Task.Delay(1000);
_cleanupWrapper.Call();
Console.WriteLine("wait for cleanup end");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<!-- Licensed to the .NET Foundation under one or more agreements. -->
<!-- The .NET Foundation licenses this file to you under the MIT license. -->
<html>
<head>
<title>TESTS</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script type="text/javascript" src="simple.js"></script>
<script defer src="dotnet.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class TimersHelper {
install() {
const measuredCallbackName = "mono_wasm_set_timeout_exec";
globalThis.registerCounter = 0;
globalThis.hitCounter = 0;
console.log("install")
if (!globalThis.originalSetTimeout) {
globalThis.originalSetTimeout = globalThis.setTimeout;
}
globalThis.setTimeout = (cb, time) => {
var start = Date.now().valueOf();
if (cb.name === measuredCallbackName) {
globalThis.registerCounter++;
console.log(`registerCounter: ${globalThis.registerCounter} now:${start} delay:${time}`)
}
return globalThis.originalSetTimeout(() => {
if (cb.name === measuredCallbackName) {
var hit = Date.now().valueOf();
globalThis.hitCounter++;
var delta = hit - start;
console.log(`hitCounter: ${globalThis.hitCounter} now:${hit} delay:${time} delta:${delta}`)
}
cb();
}, time);
};
}

getRegisterCount() {
console.log(`registerCounter: ${globalThis.registerCounter} `)
return globalThis.registerCounter;
}

getHitCount() {
console.log(`hitCounter: ${globalThis.hitCounter} `)
return globalThis.hitCounter;
}

cleanup() {
console.log(`cleanup registerCounter: ${globalThis.registerCounter} hitCounter: ${globalThis.hitCounter} `)
globalThis.setTimeout = globalThis.originalSetTimeout;
}
}

globalThis.timersHelper = new TimersHelper();

var Module = {

config: null,

preInit: async function() {
await MONO.mono_wasm_load_config("./mono-config.json"); // sets Module.config implicitly
},

// Called when the runtime is initialized and wasm is ready
onRuntimeInitialized: function () {
if (!Module.config || Module.config.error) {
console.log("No config found");
return;
}

Module.config.loaded_cb = function () {
try {
BINDING.call_static_method("[System.Private.Runtime.InteropServices.JavaScript.Tests] System.Runtime.InteropServices.JavaScript.Tests.SimpleTest:Test", []);
} catch (error) {
throw (error);
}
};
Module.config.fetch_file_cb = function (asset) {
return fetch (asset, { credentials: 'same-origin' });
}

try
{
MONO.mono_load_runtime_and_bcl_args (Module.config);
} catch (error) {
throw(error);
}
},
};
Loading