diff --git a/Directory.Packages.props b/Directory.Packages.props index acfa622fd..b332c9503 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,7 +3,7 @@ true - + diff --git a/src/Http/Wolverine.Http.Tests/Bugs/Bug_using_host_stop.cs b/src/Http/Wolverine.Http.Tests/Bugs/Bug_using_host_stop.cs index e025f2f45..055d4df6b 100644 --- a/src/Http/Wolverine.Http.Tests/Bugs/Bug_using_host_stop.cs +++ b/src/Http/Wolverine.Http.Tests/Bugs/Bug_using_host_stop.cs @@ -1,7 +1,11 @@ using Alba; +using JasperFx.CommandLine; +using JasperFx.Core.Reflection; using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Hosting; using Shouldly; using System.Reflection; +using Microsoft.Extensions.DependencyInjection; using Wolverine.Runtime; using Wolverine.Tracking; @@ -9,74 +13,97 @@ namespace Wolverine.Http.Tests.Bugs; public class Bug_using_host_stop { - [Fact] - public async Task stops_wolverine_runtime_when_created_via_host_builder() + public enum HostType { - var builder = WebApplication.CreateBuilder([]); - builder.Services.DisableAllWolverineMessagePersistence(); - builder.Services.DisableAllExternalWolverineTransports(); - builder.Services.AddWolverine(_ => { }); - var host = await AlbaHost.For(builder, _ => { }); - var runtime = host.GetRuntime(); - var checkPoints = new bool[3]; - - checkPoints[0] = IsRunning(runtime); - await host.StopAsync(); - checkPoints[1] = IsRunning(runtime); - await host.DisposeAsync(); - checkPoints[2] = IsRunning(runtime); + WebApplicationBuilder, + AlbaHostWithWebApplicationBuilder, + AlbaHostWithFactory + } - // Note WolverineRuntime is stopped when host.StopAsync() is called, - // which is expected as WolverineRuntime is IHostedService. - checkPoints.ShouldBe([true, false, false]); + private class HostTypeData : TheoryData + { + public HostTypeData() => AddRange(Enum.GetValues()); } - [Fact] - public async Task does_not_stop_wolverine_runtime_when_created_via_web_factory() + [Theory] + [ClassData(typeof(HostTypeData))] + public async Task wolverine_runtime_stops_when_host_is_stopped(HostType type) { - var builder = WebApplication.CreateBuilder([]); - builder.Services.DisableAllWolverineMessagePersistence(); - builder.Services.DisableAllExternalWolverineTransports(); - builder.Services.AddWolverine(_ => { }); - var host = await AlbaHost.For(_ => { }); + using var host = await CreateHostAsync(type); var wolverineRuntime = host.GetRuntime(); - var checkPoints = new bool[3]; + var checkPoints = new bool[2]; checkPoints[0] = IsRunning(wolverineRuntime); await host.StopAsync(); checkPoints[1] = IsRunning(wolverineRuntime); - await host.DisposeAsync(); - checkPoints[2] = IsRunning(wolverineRuntime); - // If you expect host.StopAsync() to stop WolverineRuntime - - // [true, false, false] - it's not the case here. - checkPoints.ShouldBe([true, true, false]); + checkPoints.ShouldBe([true, false]); } - [Fact] - public async Task wolverine_runtime_can_be_stopped_explicitly_when_created_via_web_factory() + [Theory] + [ClassData(typeof(HostTypeData))] + public async Task wolverine_runtime_stops_when_host_is_disposed(HostType type) { - var builder = WebApplication.CreateBuilder([]); - builder.Services.DisableAllWolverineMessagePersistence(); - builder.Services.DisableAllExternalWolverineTransports(); - builder.Services.AddWolverine(_ => { }); - var host = await AlbaHost.For(_ => { }); + using var host = await CreateHostAsync(type); var wolverineRuntime = host.GetRuntime(); - var checkPoints = new bool[3]; + var checkPoints = new bool[2]; checkPoints[0] = IsRunning(wolverineRuntime); - await host.GetRuntime().StopAsync(default); // Can be stopped explicitly. - await host.StopAsync(); + await host.As().DisposeAsync(); checkPoints[1] = IsRunning(wolverineRuntime); - await host.DisposeAsync(); - checkPoints[2] = IsRunning(wolverineRuntime); - checkPoints.ShouldBe([true, false, false]); + checkPoints.ShouldBe([true, false]); } static bool IsRunning(WolverineRuntime runtime) { - var field = typeof(WolverineRuntime).GetField("_hasStopped", BindingFlags.NonPublic | BindingFlags.Instance); + var field = typeof(WolverineRuntime).GetField("_hasStopped", + BindingFlags.NonPublic | BindingFlags.Instance); return (bool?)field?.GetValue(runtime) == false; } -} + + private static async Task CreateHostAsync(HostType hostType) => + hostType switch + { + HostType.WebApplicationBuilder => + await CreateHostWithWebApplicationBuilder(), + + HostType.AlbaHostWithWebApplicationBuilder => + await AlbaHost.For(CreateWebApplicationBuilder(), _ => { }), + + _ => + await CreateAlbaHostWithWithFactory() + }; + + private static async Task CreateAlbaHostWithWithFactory() + { + JasperFxEnvironment.AutoStartHost = true; // to start the underlying host + + return await AlbaHost.For(x => + x.ConfigureServices(ConfigureWolverine)); + } + + private static async Task CreateHostWithWebApplicationBuilder() + { + var builder = CreateWebApplicationBuilder(); + var host = builder.Build(); + await host.StartAsync(); + return host; + } + + private static WebApplicationBuilder CreateWebApplicationBuilder() + { + var builder = WebApplication.CreateBuilder([]); + ConfigureWolverine(builder.Services); + builder.Services.AddWolverine(_ => { }); + return builder; + } + + private static void ConfigureWolverine(IServiceCollection services) + { + services + .RunWolverineInSoloMode() + .DisableAllWolverineMessagePersistence() + .DisableAllExternalWolverineTransports(); + } +} \ No newline at end of file diff --git a/src/Http/Wolverine.Http.Tests/IntegrationContext.cs b/src/Http/Wolverine.Http.Tests/IntegrationContext.cs index 09a44bb44..f8e6ade20 100644 --- a/src/Http/Wolverine.Http.Tests/IntegrationContext.cs +++ b/src/Http/Wolverine.Http.Tests/IntegrationContext.cs @@ -56,7 +56,6 @@ public async Task DisposeAsync() if (Host is null) return; - await Host.GetRuntime().StopAsync(default); await Host.StopAsync(); await Host.DisposeAsync();