11using Alba ;
2+ using JasperFx . CommandLine ;
3+ using JasperFx . Core . Reflection ;
24using Microsoft . AspNetCore . Builder ;
5+ using Microsoft . Extensions . Hosting ;
36using Shouldly ;
47using System . Reflection ;
8+ using Microsoft . Extensions . DependencyInjection ;
59using Wolverine . Runtime ;
610using Wolverine . Tracking ;
711
812namespace Wolverine . Http . Tests . Bugs ;
913
1014public class Bug_using_host_stop
1115{
16+ public enum HostType
17+ {
18+ WebApplicationBuilder ,
19+ AlbaHostWithWebApplicationBuilder ,
20+ AlbaHostWithFactory
21+ }
22+
23+ private class HostTypeData : TheoryData < HostType >
24+ {
25+ public HostTypeData ( ) => AddRange ( Enum . GetValues < HostType > ( ) ) ;
26+ }
27+
1228 [ Theory ]
13- [ InlineData ( true ) ]
14- [ InlineData ( false ) ]
15- public async Task stops_wolverine_runtime ( bool useWebApplicationBuilder )
29+ [ ClassData ( typeof ( HostTypeData ) ) ]
30+ public async Task wolverine_runtime_stops_when_host_is_stopped ( HostType type )
1631 {
17- await using var host = await CreateHostAsync ( useWebApplicationBuilder ) ;
32+ using var host = await CreateHostAsync ( type ) ;
1833 var wolverineRuntime = host . GetRuntime ( ) ;
19- var checkPoints = new bool [ 3 ] ;
34+ var checkPoints = new bool [ 2 ] ;
2035
2136 checkPoints [ 0 ] = IsRunning ( wolverineRuntime ) ;
2237 await host . StopAsync ( ) ;
2338 checkPoints [ 1 ] = IsRunning ( wolverineRuntime ) ;
24- await host . DisposeAsync ( ) ;
25- checkPoints [ 2 ] = IsRunning ( wolverineRuntime ) ;
2639
27- // Note WolverineRuntime is stopped when host.StopAsync() is called,
28- // which is expected as WolverineRuntime is IHostedService.
29- checkPoints . ShouldBe ( [ true , false , false ] ) ;
40+ checkPoints . ShouldBe ( [ true , false ] ) ;
3041 }
3142
3243 [ Theory ]
33- [ InlineData ( true ) ]
34- [ InlineData ( false ) ]
35- public async Task wolverine_runtime_can_be_stopped_explicitly ( bool useWebApplicationBuilder )
44+ [ ClassData ( typeof ( HostTypeData ) ) ]
45+ public async Task wolverine_runtime_stops_when_host_is_disposed ( HostType type )
3646 {
37- await using var host = await CreateHostAsync ( useWebApplicationBuilder ) ;
47+ using var host = await CreateHostAsync ( type ) ;
3848 var wolverineRuntime = host . GetRuntime ( ) ;
39- var checkPoints = new bool [ 3 ] ;
49+ var checkPoints = new bool [ 2 ] ;
4050
4151 checkPoints [ 0 ] = IsRunning ( wolverineRuntime ) ;
42- await wolverineRuntime . StopAsync ( default ) ; // can be stopped explicitly
43- await host . StopAsync ( ) ;
52+ await host . As < IAsyncDisposable > ( ) . DisposeAsync ( ) ;
4453 checkPoints [ 1 ] = IsRunning ( wolverineRuntime ) ;
45- await host . DisposeAsync ( ) ;
46- checkPoints [ 2 ] = IsRunning ( wolverineRuntime ) ;
4754
48- checkPoints . ShouldBe ( [ true , false , false ] ) ;
55+ checkPoints . ShouldBe ( [ true , false ] ) ;
4956 }
5057
5158 static bool IsRunning ( WolverineRuntime runtime )
5259 {
53- var field = typeof ( WolverineRuntime ) . GetField ( "_hasStopped" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
60+ var field = typeof ( WolverineRuntime ) . GetField ( "_hasStopped" ,
61+ BindingFlags . NonPublic | BindingFlags . Instance ) ;
5462 return ( bool ? ) field ? . GetValue ( runtime ) == false ;
5563 }
5664
57- static Task < IAlbaHost > CreateHostAsync ( bool useWebApplicationBuilder )
58- {
59- if ( useWebApplicationBuilder )
65+ private static async Task < IHost > CreateHostAsync ( HostType hostType ) =>
66+ hostType switch
6067 {
61- var builder = WebApplication . CreateBuilder ( [ ] ) ;
62- builder . Services . DisableAllWolverineMessagePersistence ( ) ;
63- builder . Services . DisableAllExternalWolverineTransports ( ) ;
64- builder . Services . AddWolverine ( _ => { } ) ;
68+ HostType . WebApplicationBuilder =>
69+ await CreateHostWithWebApplicationBuilder ( ) ,
70+
71+ HostType . AlbaHostWithWebApplicationBuilder =>
72+ await AlbaHost . For ( CreateWebApplicationBuilder ( ) , _ => { } ) ,
73+
74+ _ =>
75+ await CreateAlbaHostWithWithFactory ( )
76+ } ;
6577
66- return AlbaHost . For ( builder , _ => { } ) ;
67- }
78+ private static async Task < IHost > CreateAlbaHostWithWithFactory ( )
79+ {
80+ JasperFxEnvironment . AutoStartHost = true ; // to start the underlying host
81+
82+ return await AlbaHost . For < WolverineWebApi . Program > ( x =>
83+ x . ConfigureServices ( ConfigureWolverine ) ) ;
84+ }
85+
86+ private static async Task < IHost > CreateHostWithWebApplicationBuilder ( )
87+ {
88+ var builder = CreateWebApplicationBuilder ( ) ;
89+ var host = builder . Build ( ) ;
90+ await host . StartAsync ( ) ;
91+ return host ;
92+ }
6893
69- return AlbaHost . For < WolverineWebApi . Program > ( _ => { } ) ;
94+ private static WebApplicationBuilder CreateWebApplicationBuilder ( )
95+ {
96+ var builder = WebApplication . CreateBuilder ( [ ] ) ;
97+ ConfigureWolverine ( builder . Services ) ;
98+ builder . Services . AddWolverine ( _ => { } ) ;
99+ return builder ;
100+ }
101+
102+ private static void ConfigureWolverine ( IServiceCollection services )
103+ {
104+ services
105+ . RunWolverineInSoloMode ( )
106+ . DisableAllWolverineMessagePersistence ( )
107+ . DisableAllExternalWolverineTransports ( ) ;
70108 }
71- }
109+ }
0 commit comments