-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Add E2E tests for webworker template #65405
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
Open
ilonatommy
wants to merge
8
commits into
dotnet:main
Choose a base branch
from
ilonatommy:webworker-e2e-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+481
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce0554c
Add WebWorker E2E tests.
ilonatommy f69d202
Remove redundant comments.
ilonatommy 8eae37e
Cleanup.
ilonatommy 309b1ce
Fix build.
ilonatommy daf9f9e
Fix build errors.
ilonatommy 9467871
Tests inside one class run sequentially, no need for locking.
ilonatommy cd20f80
Fix: move exports to blazor app.
ilonatommy 4c9cb67
Attempt to fix timeout.
ilonatommy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/ProjectTemplates/test/Templates.Blazor.Tests/TestAssets/WebWorker/TestWorkerMethods.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Runtime.InteropServices.JavaScript; | ||
| using System.Runtime.Versioning; | ||
| using System.Text.Json; | ||
|
|
||
| namespace TestWorkerExports; | ||
|
|
||
| [SupportedOSPlatform("browser")] | ||
| public static partial class TestWorkerMethods | ||
| { | ||
| [JSExport] | ||
| public static int Add(int a, int b) => a + b; | ||
|
|
||
| [JSExport] | ||
| public static string Echo(string input) => input; | ||
|
|
||
| [JSExport] | ||
| public static string GetPersonJson() | ||
| => JsonSerializer.Serialize(new { Name = "Alice", Age = 30 }); | ||
|
|
||
| [JSExport] | ||
| public static string ThrowError() | ||
| => throw new InvalidOperationException("Test exception from worker"); | ||
| } | ||
125 changes: 125 additions & 0 deletions
125
src/ProjectTemplates/test/Templates.Blazor.Tests/TestAssets/WebWorker/WebWorkerTest.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| @* Licensed to the .NET Foundation under one or more agreements. *@ | ||
| @* The .NET Foundation licenses this file to you under the MIT license. *@ | ||
|
|
||
| @page "/webworker-test" | ||
|
|
||
| @using WorkerLib | ||
| @inject IJSRuntime JSRuntime | ||
| @implements IAsyncDisposable | ||
|
|
||
| <PageTitle>WebWorker Test</PageTitle> | ||
|
|
||
| <div id="webworker-test"> | ||
| <h3>WebWorker E2E Test</h3> | ||
|
|
||
| <div> | ||
| <button id="btn-init" @onclick="InitWorker">Initialize Worker</button> | ||
| <span id="init-status">@InitStatus</span> | ||
| </div> | ||
|
|
||
| <div> | ||
| <button id="btn-add" @onclick="TestAdd" disabled="@(!IsReady)">Test Add</button> | ||
| <span id="add-result">@AddResult</span> | ||
| </div> | ||
|
|
||
| <div> | ||
| <button id="btn-echo" @onclick="TestEcho" disabled="@(!IsReady)">Test Echo</button> | ||
| <span id="echo-result">@EchoResult</span> | ||
| </div> | ||
|
|
||
| <div> | ||
| <button id="btn-error" @onclick="TestError" disabled="@(!IsReady)">Test Error</button> | ||
| <span id="error-result">@ErrorResult</span> | ||
| </div> | ||
|
|
||
| <div> | ||
| <button id="btn-dispose" @onclick="DisposeWorker" disabled="@(!IsReady)">Dispose Worker</button> | ||
| <span id="dispose-status">@DisposeStatus</span> | ||
| </div> | ||
| </div> | ||
|
|
||
| @code { | ||
| private WebWorkerClient? Worker; | ||
| private string InitStatus = "Not initialized"; | ||
| private string AddResult = ""; | ||
| private string EchoResult = ""; | ||
| private string ErrorResult = ""; | ||
| private string DisposeStatus = ""; | ||
|
|
||
| private bool IsReady => Worker != null && DisposeStatus != "Disposed"; | ||
|
|
||
| private async Task InitWorker() | ||
| { | ||
| try | ||
| { | ||
| InitStatus = "Initializing..."; | ||
| StateHasChanged(); | ||
| await Task.Yield(); | ||
|
|
||
| Worker = await WebWorkerClient.CreateAsync(JSRuntime); | ||
| InitStatus = "Ready"; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| InitStatus = $"Error: {ex.Message}"; | ||
| Console.WriteLine($"[WebWorkerTest] Init error: {ex}"); | ||
| } | ||
| } | ||
|
|
||
| private async Task TestAdd() | ||
| { | ||
| try | ||
| { | ||
| var result = await Worker!.InvokeAsync<int>("TestWorkerExports.TestWorkerMethods.Add", [5, 3]); | ||
| AddResult = result == 8 ? "8" : $"Unexpected: {result}"; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| AddResult = $"Error: {ex.Message}"; | ||
| } | ||
| } | ||
|
|
||
| private async Task TestEcho() | ||
| { | ||
| try | ||
| { | ||
| var result = await Worker!.InvokeAsync<string>("TestWorkerExports.TestWorkerMethods.Echo", ["Hello Worker"]); | ||
| EchoResult = result == "Hello Worker" ? "Hello Worker" : $"Unexpected: {result}"; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| EchoResult = $"Error: {ex.Message}"; | ||
| } | ||
| } | ||
|
|
||
| private async Task TestError() | ||
| { | ||
| try | ||
| { | ||
| await Worker!.InvokeAsync<string>("TestWorkerExports.TestWorkerMethods.ThrowError", []); | ||
| ErrorResult = "No error thrown"; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| ErrorResult = ex.Message.Contains("Test exception") ? "Caught expected error" : $"Wrong error: {ex.Message}"; | ||
| } | ||
| } | ||
|
|
||
| private async Task DisposeWorker() | ||
| { | ||
| if (Worker != null) | ||
| { | ||
| await Worker.DisposeAsync(); | ||
| Worker = null; | ||
| DisposeStatus = "Disposed"; | ||
| } | ||
| } | ||
|
|
||
| public async ValueTask DisposeAsync() | ||
| { | ||
| if (Worker != null) | ||
| { | ||
| await Worker.DisposeAsync(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.