Skip to content

Commit

Permalink
Merge pull request #243 from Lombiq/issue/OSOE-464-remove-sync-over-a…
Browse files Browse the repository at this point in the history
…sync

OSOE-464: Attempt to fix dotnet test randomly getting stuck due to sync-over-async and shorter/safer monkey testing
  • Loading branch information
dministro authored Dec 13, 2022
2 parents 6025b42 9a9983c commit 29df808
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Lombiq.Tests.UI.Samples/Tests/MonkeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 65,6 @@ public Task TestAdminPagesAsMonkeyRecursivelyShouldWorkWithAdminUser(Browser bro
context.TestAdminAsMonkeyRecursivelyAsync(CreateMonkeyTestingOptions()),
browser,
configuration =>
// This is necessary to work around this bug: https://github.com/OrchardCMS/OrchardCore/issues/11420.
configuration.AssertBrowserLog = logEntries => logEntries.ShouldNotContain(
logEntry => IsValidAdminBrowserLogEntry(logEntry),
logEntries.Where(IsValidAdminBrowserLogEntry).ToFormattedString()));
Expand Down Expand Up @@ -105,13 104,17 @@ public Task TestAdminBackgroundTasksAsMonkeyRecursivelyShouldWorkWithAdminUser(B
private static MonkeyTestingOptions CreateMonkeyTestingOptions() =>
new()
{
PageTestTime = TimeSpan.FromSeconds(10),
PageTestTime = TimeSpan.FromSeconds(5),
};

private static bool IsValidAdminBrowserLogEntry(LogEntry logEntry) =>
OrchardCoreUITestExecutorConfiguration.IsValidBrowserLogEntry(logEntry) &&
// This is necessary to work around this bug: https://github.com/OrchardCMS/OrchardCore/issues/11420.
!logEntry.Message.ContainsOrdinalIgnoreCase(
"Blocked attempt to show a 'beforeunload' confirmation panel for a frame that never had a user gesture since its load.");
"Blocked attempt to show a 'beforeunload' confirmation panel for a frame that never had a user gesture since its load.") &&
// Requests to /api/graphql without further parameters will fail with HTTP 400, but that's OK, since some
// parameters are required.
!logEntry.Message.ContainsOrdinalIgnoreCase("/api/graphql - Failed to load resource: the server responded with a status of 400");
}

// END OF TRAINING SECTION: Monkey tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 49,23 @@ protected override IHost CreateHost(IHostBuilder builder)
// to store cached types when initializing the default logger instance.
lock (OrchardApplicationFactoryCounter.CreateHostLock)
{
return base.CreateHost(builder);
// Moving host startup out of the xUnit synchronization context to a new thread, to avoid potential
// deadlocks and thus dotnet test getting randomly stuck due to sync-over-async code in
// WebApplicationFactory. See ASP.NET Core issue: https://github.com/dotnet/aspnetcore/issues/43353. See our
// issue for more details about the whole topic: https://github.com/Lombiq/UI-Testing-Toolbox/issues/228.
// Solution taken from:
// https://www.strathweb.com/2021/05/the-curious-case-of-asp-net-core-integration-test-deadlock/.

// The original CreateHost() is just the following:
////var host = builder.Build();
////host.Start();
////return host;
// See https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs for
// the latest source.

var host = builder.Build();
Task.Run(() => host.StartAsync()).GetAwaiter().GetResult();
return host;
}
}

Expand Down

0 comments on commit 29df808

Please sign in to comment.