Skip to content

Commit

Permalink
[browser][MT] Add new test for multi-threading SignalR in Blazor WBT (#…
Browse files Browse the repository at this point in the history
…98343)

* Add new test asset app - blazor hosted with SignalR.

* Interactive version of tests.

* Revert removal of `--ignore-certificate-errors` -  it is needed for codespaces.

* Cleanup + better logs + Host.cshtml removal.

* Apply @maraf's fix for static files.

* Update comment + merge testOutputs into one buffer + add ToString on the output.

* Initial reduction of setup code.

* Minor refactor.

* Major refactor.
  • Loading branch information
ilonatommy authored Mar 1, 2024
1 parent 357525a commit 50d6e5d
Show file tree
Hide file tree
Showing 26 changed files with 553 additions and 43 deletions.
3 changes: 2 additions & 1 deletion eng/testing/scenarios/BuildWasmAppsJobsList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ Wasm.Build.Tests.TestAppScenarios.AppSettingsTests
Wasm.Build.Tests.TestAppScenarios.LazyLoadingTests
Wasm.Build.Tests.TestAppScenarios.LibraryInitializerTests
Wasm.Build.Tests.TestAppScenarios.SatelliteLoadingTests
Wasm.Build.Tests.TestAppScenarios.DownloadResourceProgressTests
Wasm.Build.Tests.TestAppScenarios.SignalRClientTests
Wasm.Build.Tests.WasmBuildAppTest
Wasm.Build.Tests.WasmNativeDefaultsTests
Wasm.Build.Tests.WasmRunOutOfAppBundleTests
Wasm.Build.Tests.WasmSIMDTests
Wasm.Build.Tests.WasmTemplateTests
Wasm.Build.Tests.WorkloadTests
Wasm.Build.Tests.TestAppScenarios.DownloadResourceProgressTests
Wasm.Build.Tests.MT.Blazor.SimpleMultiThreadedTests
Wasm.Build.Tests.TestAppScenarios.DebugLevelTests
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<PropertyGroup>
<_WasmNativeAssetFileNames>;@(WasmNativeAsset->'%(FileName)%(Extension)');</_WasmNativeAssetFileNames>
</PropertyGroup>

<ItemGroup>
<_WasmConfigFileCandidates Include="@(StaticWebAsset)" Condition="'%(SourceType)' == 'Discovered'" />

Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/Wasm.Build.Tests/Blazor/AppsettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var builder
await BlazorRunForBuildWithDotnetRun(new BlazorRunOptions()
{
Config = "debug",
OnConsoleMessage = msg =>
OnConsoleMessage = (_, msg) =>
{
if (msg.Text.Contains("appSettings Exists 'True'"))
existsChecked = true;
Expand Down
7 changes: 6 additions & 1 deletion src/mono/wasm/Wasm.Build.Tests/Blazor/BlazorRunOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

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

Expand All @@ -13,11 +14,15 @@ public record BlazorRunOptions
BlazorRunHost Host = BlazorRunHost.DotnetRun,
bool DetectRuntimeFailures = true,
bool CheckCounter = true,
Dictionary<string, string>? ServerEnvironment = null,
Func<IPage, Task>? Test = null,
Action<IConsoleMessage>? OnConsoleMessage = null,
Action<IPage>? OnPageLoaded = null,
Action<IPage, IConsoleMessage>? OnConsoleMessage = null,
Action<string>? OnServerMessage = null,
Action<string>? OnErrorMessage = null,
string Config = "Debug",
string? ExtraArgs = null,
string BrowserPath = "",
string QueryString = ""
);

Expand Down
27 changes: 22 additions & 5 deletions src/mono/wasm/Wasm.Build.Tests/Blazor/BlazorWasmTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,22 @@ public async Task BlazorRunTest(string runArgs,
{
if (!string.IsNullOrEmpty(runOptions.ExtraArgs))
runArgs += $" {runOptions.ExtraArgs}";

runOptions.ServerEnvironment?.ToList().ForEach(
kv => s_buildEnv.EnvVars[kv.Key] = kv.Value);

using var runCommand = new RunCommand(s_buildEnv, _testOutput)
.WithWorkingDirectory(workingDirectory);

await using var runner = new BrowserRunner(_testOutput);
var page = await runner.RunAsync(runCommand, runArgs, onConsoleMessage: OnConsoleMessage, onError: OnErrorMessage, modifyBrowserUrl: browserUrl => browserUrl + runOptions.QueryString);
var page = await runner.RunAsync(
runCommand,
runArgs,
onPageLoaded: runOptions.OnPageLoaded,
onConsoleMessage: OnConsoleMessage,
onServerMessage: runOptions.OnServerMessage,
onError: OnErrorMessage,
modifyBrowserUrl: browserUrl => browserUrl + runOptions.BrowserPath + runOptions.QueryString);

_testOutput.WriteLine("Waiting for page to load");
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded, new () { Timeout = 1 * 60 * 1000 });
Expand All @@ -217,11 +228,11 @@ public async Task BlazorRunTest(string runArgs,
_testOutput.WriteLine($"Waiting for additional 10secs to see if any errors are reported");
await Task.Delay(10_000);

void OnConsoleMessage(IConsoleMessage msg)
void OnConsoleMessage(IPage page, IConsoleMessage msg)
{
_testOutput.WriteLine($"[{msg.Type}] {msg.Text}");

runOptions.OnConsoleMessage?.Invoke(msg);
runOptions.OnConsoleMessage?.Invoke(page, msg);

if (runOptions.DetectRuntimeFailures)
{
Expand All @@ -237,6 +248,12 @@ void OnErrorMessage(string msg)
}
}

public string FindBlazorBinFrameworkDir(string config, bool forPublish, string framework = DefaultTargetFrameworkForBlazor)
=> _provider.FindBinFrameworkDir(config: config, forPublish: forPublish, framework: framework);
public string FindBlazorBinFrameworkDir(string config, bool forPublish, string framework = DefaultTargetFrameworkForBlazor, string? projectDir = null)
=> _provider.FindBinFrameworkDir(config: config, forPublish: forPublish, framework: framework, projectDir: projectDir);

public string FindBlazorHostedBinFrameworkDir(string config, bool forPublish, string clientDirRelativeToProjectDir, string framework = DefaultTargetFrameworkForBlazor)
{
string? clientProjectDir = _projectDir == null ? null : Path.Combine(_projectDir, clientDirRelativeToProjectDir);
return _provider.FindBinFrameworkDir(config: config, forPublish: forPublish, framework: framework, projectDir: clientProjectDir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ await BlazorRunForPublishWithWebServer(
runOptions: new BlazorRunOptions(
Config: config,
ExtraArgs: "--web-server-use-cors --web-server-use-cop",
OnConsoleMessage: (message) =>
OnConsoleMessage: (_, message) =>
{
if (message.Text.Contains("WasmEnableThreads=true"))
hasEmittedWasmEnableThreads = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ await BlazorRunTest(new BlazorRunOptions()
{
Config = config,
Host = publish ? BlazorRunHost.WebServer : BlazorRunHost.DotnetRun,
OnConsoleMessage = msg =>
OnConsoleMessage = (_, msg) =>
{
sbOutput.AppendLine(msg.Text);
}
Expand Down
26 changes: 19 additions & 7 deletions src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ internal class BrowserRunner : IAsyncDisposable

public async Task<string> StartServerAndGetUrlAsync(
ToolCommand cmd,
string args
string args,
Action<string>? onServerMessage = null
) {
TaskCompletionSource<string> urlAvailable = new();
Action<string?> outputHandler = msg =>
{
if (string.IsNullOrEmpty(msg))
return;

onServerMessage?.Invoke(msg);

lock (OutputLines)
{
OutputLines.Add(msg);
}

Match m = s_appHostUrlRegex.Match(msg);
if (!m.Success)
Expand Down Expand Up @@ -91,7 +96,8 @@ public async Task<IBrowser> SpawnBrowserAsync(
) {
var url = new Uri(browserUrl);
Playwright = await Microsoft.Playwright.Playwright.CreateAsync();
string[] chromeArgs = new[] { $"--explicitly-allowed-ports={url.Port}" };
// codespaces: ignore certificate error -> Microsoft.Playwright.PlaywrightException : net::ERR_CERT_AUTHORITY_INVALID
string[] chromeArgs = new[] { $"--explicitly-allowed-ports={url.Port}", "--ignore-certificate-errors" };
_testOutput.WriteLine($"Launching chrome ('{s_chromePath.Value}') via playwright with args = {string.Join(',', chromeArgs)}");
return Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions{
ExecutablePath = s_chromePath.Value,
Expand All @@ -105,21 +111,24 @@ public async Task<IPage> RunAsync(
ToolCommand cmd,
string args,
bool headless = true,
Action<IConsoleMessage>? onConsoleMessage = null,
Action<IPage, IConsoleMessage>? onConsoleMessage = null,
Action<IPage>? onPageLoaded = null,
Action<string>? onServerMessage = null,
Action<string>? onError = null,
Func<string, string>? modifyBrowserUrl = null)
{
var urlString = await StartServerAndGetUrlAsync(cmd, args);
var urlString = await StartServerAndGetUrlAsync(cmd, args, onServerMessage);
var browser = await SpawnBrowserAsync(urlString, headless);
var context = await browser.NewContextAsync();
return await RunAsync(context, urlString, headless, onConsoleMessage, onError, modifyBrowserUrl);
return await RunAsync(context, urlString, headless, onPageLoaded, onConsoleMessage, onError, modifyBrowserUrl);
}

public async Task<IPage> RunAsync(
IBrowserContext context,
string browserUrl,
bool headless = true,
Action<IConsoleMessage>? onConsoleMessage = null,
Action<IPage>? onPageLoaded = null,
Action<IPage, IConsoleMessage>? onConsoleMessage = null,
Action<string>? onError = null,
Func<string, string>? modifyBrowserUrl = null,
bool resetExitedState = false
Expand All @@ -131,8 +140,11 @@ public async Task<IPage> RunAsync(
browserUrl = modifyBrowserUrl(browserUrl);

IPage page = await context.NewPageAsync();
if (onPageLoaded is not null)
page.Load += (_, _) => onPageLoaded(page);

if (onConsoleMessage is not null)
page.Console += (_, msg) => onConsoleMessage(msg);
page.Console += (_, msg) => onConsoleMessage(page, msg);

onError ??= _testOutput.WriteLine;
if (onError is not null)
Expand Down
7 changes: 7 additions & 0 deletions src/mono/wasm/Wasm.Build.Tests/Common/TestOutputWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Text;
using Xunit.Abstractions;

#nullable enable
Expand All @@ -10,17 +11,23 @@ namespace Wasm.Build.Tests;

public class TestOutputWrapper(ITestOutputHelper baseOutput) : ITestOutputHelper
{
private readonly StringBuilder _outputBuffer = new StringBuilder();

public void WriteLine(string message)
{
baseOutput.WriteLine(message);
_outputBuffer.AppendLine(message);
if (EnvironmentVariables.ShowBuildOutput)
Console.WriteLine(message);
}

public void WriteLine(string format, params object[] args)
{
baseOutput.WriteLine(format, args);
_outputBuffer.AppendFormat(format, args).AppendLine();
if (EnvironmentVariables.ShowBuildOutput)
Console.WriteLine(format, args);
}

public override string ToString() => _outputBuffer.ToString();
}
4 changes: 2 additions & 2 deletions src/mono/wasm/Wasm.Build.Tests/ProjectProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,10 @@ private void AssertFileNames(IEnumerable<string> expected, IEnumerable<string> a
Assert.Equal(expected, actualFileNames);
}

public virtual string FindBinFrameworkDir(string config, bool forPublish, string framework, string? bundleDirName = null)
public virtual string FindBinFrameworkDir(string config, bool forPublish, string framework, string? bundleDirName = null, string? projectDir = null)
{
EnsureProjectDirIsSet();
string basePath = Path.Combine(ProjectDir!, "bin", config, framework);
string basePath = Path.Combine(projectDir ?? ProjectDir!, "bin", config, framework);
if (forPublish)
basePath = FindSubDirIgnoringCase(basePath, "publish");

Expand Down
Loading

0 comments on commit 50d6e5d

Please sign in to comment.