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

fix: Restore threading tests #899

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 0 additions & 2 deletions build/ci/stage-build-linux-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ jobs:
"$(build.sourcesdirectory)/src/Uno.Wasm.Threads/bin/Release/net9.0/publish/wwwroot" \
"$(build.sourcesdirectory)/src/Uno.Wasm.Threading.UITests" "http://localhost:8000/"
displayName: Threading UI Tests
condition: eq( variables['THREAD_TESTING'], 'true' )
env:
BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)"

Expand All @@ -82,7 +81,6 @@ jobs:
"$(build.sourcesdirectory)/src/Uno.Wasm.Threads.Aot/bin/Release/net9.0/publish/wwwroot" \
"$(build.sourcesdirectory)/src/Uno.Wasm.Threading.UITests" "http://localhost:8000/"
displayName: Threading AOT UI Tests
condition: eq( variables['THREAD_TESTING'], 'true' )
env:
BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)"

Expand Down
2 changes: 0 additions & 2 deletions build/ci/stage-build-windows-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ jobs:
"http://localhost:8000/"

displayName: Threading Tests
condition: eq( variables['THREAD_TESTING'], 'true' )
env:
BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)"

Expand All @@ -258,7 +257,6 @@ jobs:
"$(build.sourcesdirectory)\src\Uno.Wasm.Threading.UITests" `
"http://localhost:8000/"
displayName: Threading AOT Tests
condition: eq( variables['THREAD_TESTING'], 'true' )
env:
BUILD_SOURCESDIRECTORY: "$(build.sourcesdirectory)"

Expand Down
2 changes: 1 addition & 1 deletion src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
<WasmEnableThreads Condition=" '$(WasmShellEnableThreads)' == 'true' ">true</WasmEnableThreads>

<!-- https://github.com/dotnet/runtime/blob/1231a9addd0fb3cfb806c2a89cb45a3018c48bb2/src/mono/browser/build/BrowserWasmApp.targets#L125-L126 -->
<_WasmPThreadPoolInitialSize Condition=" '$(WasmShellPThreadsPoolSize)' == 'true' ">true</_WasmPThreadPoolInitialSize>
<_WasmPThreadPoolInitialSize Condition=" '$(WasmShellPThreadsPoolSize)' == '' ">2</_WasmPThreadPoolInitialSize>

<!-- Native build compatibility -->
<WasmBuildNative Condition="
Expand Down
14 changes: 11 additions & 3 deletions src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/Bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ namespace Uno.WebAssembly.Bootstrap {
.withRuntimeOptions(config.config.uno_runtime_options)
.withConfig({ loadAllSatelliteResources: config.config.uno_load_all_satellite_resources });

var features = config.config.environmentVariables['UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES'];

if (features && features.includes('threads')) {
// m.dotnet
// .withConfig({ jsThreadBlockingMode: "DangerousAllowBlockingWait" });
}

const dotnetRuntime = await m.default(
(context: DotnetPublicAPI) => {
bootstrapper.configure(context);
Expand Down Expand Up @@ -138,6 +145,7 @@ namespace Uno.WebAssembly.Bootstrap {
onConfigLoaded: this.onConfigLoaded,
onDotnetReady: this.onDotnetReady,
onAbort: this.onAbort,
pthreadPoolInitialSize: 2,
exports: ["IDBFS", "FS"].concat(this._unoConfig.emcc_exported_runtime_methods),
onDownloadResourceProgress: (resourcesLoaded: number, totalResources: number) => this.reportDownloadResourceProgress(resourcesLoaded, totalResources),
};
Expand Down Expand Up @@ -253,9 +261,9 @@ namespace Uno.WebAssembly.Bootstrap {

await this.setupHotReload();

if (this._hotReloadSupport) {
await this._hotReloadSupport.initializeHotReload();
}
//if (this._hotReloadSupport) {
// await this._hotReloadSupport.initializeHotReload();
//}

this._runMain(this._unoConfig.uno_main, []);

Expand Down
17 changes: 7 additions & 10 deletions src/Uno.Wasm.Bootstrap/ts/Uno/WebAssembly/HotReloadSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ namespace Uno.WebAssembly.Bootstrap {
(<any>this._context).config.environmentVariables['ASPNETCORE-BROWSER-TOOLS']
|| (<any>this._context).config.environmentVariables['__ASPNETCORE_BROWSER_TOOLS'];

if (!await HotReloadSupport._initializeMethod()) {
console.warn("The application was compiled with the IL linker enabled, hot reload is disabled. See https://aka.platform.uno/wasm-il-linker for more details.");
}
let capabilities = await HotReloadSupport._getApplyUpdateCapabilitiesMethod();

// Take the place of the internal .NET for WebAssembly APIs for metadata updates coming
// from the "BrowserLink" feature.
(function (Blazor) {
Blazor._internal = {
initialize: function () {
if (!HotReloadSupport._initializeMethod()) {
console.warn("The application was compiled with the IL linker enabled, hot reload is disabled. See https://aka.platform.uno/wasm-il-linker for more details.");
}
},

applyExisting: async function (): Promise<void> {

if (browserToolsVariable == "true")
Expand All @@ -62,13 +61,11 @@ namespace Uno.WebAssembly.Bootstrap {
},

getApplyUpdateCapabilities: function () {
Blazor._internal.initialize();
return HotReloadSupport._getApplyUpdateCapabilitiesMethod();
return capabilities;
},

applyHotReload: function (moduleId: any, metadataDelta: any, ilDelta: any, pdbDelta: any, updatedTypes: any) {
Blazor._internal.initialize();
return HotReloadSupport._applyHotReloadDeltaMethod(moduleId, metadataDelta, ilDelta, pdbDelta || "", updatedTypes || []);
HotReloadSupport._applyHotReloadDeltaMethod(moduleId, metadataDelta, ilDelta, pdbDelta || "", updatedTypes || []);
}
};
})((<any>window).Blazor || ((<any>window).Blazor = {}));
Expand Down
5 changes: 3 additions & 2 deletions src/Uno.Wasm.Tests.Shared/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices.JavaScript;
using System.Threading.Tasks;

namespace WebAssembly
{
Expand All @@ -11,7 +12,7 @@ internal sealed class Runtime
/// <summary>
/// Invokes Javascript code in the hosting environment
/// </summary>
internal static string InvokeJS(string str)
internal static Task<string> InvokeJS(string str)
{
Console.WriteLine($"Invoking {str}");
return Interop.InvokeJS(str);
Expand All @@ -21,6 +22,6 @@ internal static string InvokeJS(string str)
internal static partial class Interop
{
[JSImport("globalThis.Uno.WebAssembly.Bootstrap.Bootstrapper.invokeJS")]
public static partial string InvokeJS(string value);
public static partial Task<string> InvokeJS(string value);
}
}
23 changes: 10 additions & 13 deletions src/Uno.Wasm.Threads.Shared/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ public static class Program
private static List<string> _messages = new List<string>();
private static bool _mainThreadInvoked = false;
private static Timer _timer;
private static SynchronizationContext _jsContext = SynchronizationContext.Current;

static void Main()
{
var runtimeMode = Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_MODE");
Console.WriteLine($"Runtime Version: " + RuntimeInformation.FrameworkDescription);
Console.WriteLine($"Runtime Mode: " + runtimeMode);
Console.WriteLine($"TID: {Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine($"SynchronizationContext: {_jsContext}");

Runtime.InvokeJS("Interop.appendResult('Startup')");
_ = Runtime.InvokeJS("Interop.appendResult('Startup')");

Run();

Expand All @@ -55,7 +57,7 @@ private static void OnTick(object state)
if (_event.WaitOne(10) && _mainThreadInvoked)
{
var r = $"Done {_messages.Count} results (_mainThreadInvoked:{_mainThreadInvoked})";
Runtime.InvokeJS($"Interop.appendResult('{r}')");
_ = Runtime.InvokeJS($"Interop.appendResult('{r}')");
Console.WriteLine($"[tid:{Thread.CurrentThread.ManagedThreadId}]: {r}");
_timer.Dispose();
}
Expand Down Expand Up @@ -92,34 +94,29 @@ void DoWork(string name)

if ((i % 2000) == 0)
{
WebAssembly.JSInterop.InternalCalls.InvokeOnMainThread();
Console.WriteLine("InvokeOnMainThread1");
_jsContext.Post(_ => MainThreadCallback(), null);
}
}
}

#pragma warning disable CA1416 // Validate platform compatibility
new Thread(_ =>
{
Console.WriteLine($"Starting thread [tid:{Thread.CurrentThread.ManagedThreadId}]");

WebAssembly.JSInterop.InternalCalls.InvokeOnMainThread();
_jsContext.Post(_ => MainThreadCallback(), null);
Console.WriteLine("InvokeOnMainThread2");

DoWork("thread1");
tcs.TrySetResult(true);
Console.WriteLine($"Stopping thread [tid:{Thread.CurrentThread.ManagedThreadId}]");
}).Start();
#pragma warning restore CA1416 // Validate platform compatibility

await tcs.Task;

_event.Set();
}
}
}

namespace WebAssembly.JSInterop
{
public static class InternalCalls
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern void InvokeOnMainThread();
}
}
9 changes: 1 addition & 8 deletions src/Uno.Wasm.Threads.Shared/WasmScripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
});

var Interop = {
appendResult: function (str) {
appendResult: async function (str) {
var txt = document.createTextNode(str);
var parent = document.getElementById('results');
parent.appendChild(txt, parent.lastChild);
}
};

var DotNet = {
invokeOnMainThread: function (str) {
let getApplyUpdateCapabilitiesMethod = BINDING.bind_static_method("[Uno.Wasm.Threads] Uno.Wasm.Sample.Program:MainThreadCallback");
getApplyUpdateCapabilitiesMethod();
}
}
1 change: 1 addition & 0 deletions src/Uno.Wasm.Threads/Uno.Wasm.Threads.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<!--<WasmShellMonoRuntimeExecutionMode Condition="$([MSBuild]::IsOsPlatform('Linux'))">FullAOT</WasmShellMonoRuntimeExecutionMode>-->
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<WasmShellEnableThreads>true</WasmShellEnableThreads>
<!--<WasmBuildNative>true</WasmBuildNative>-->
<!--<WasmShellILLinkerEnabled>false</WasmShellILLinkerEnabled>-->
</PropertyGroup>

Expand Down
Loading