@@ -27,19 +27,27 @@ public HostingTests(ITestOutputHelper output) : base(output)
2727 public async Task WillRunSyncStartupAction ( )
2828 {
2929 var resetEvent = new AsyncManualResetEvent ( false ) ;
30- var builder = new WebHostBuilder ( )
31- . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
32- . ConfigureServices ( s =>
30+ using var host = new HostBuilder ( )
31+ . ConfigureWebHost ( webHostBuilder =>
3332 {
34- s . AddStartupAction ( "Hey" , ( ) => resetEvent . Set ( ) ) ;
35- s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
33+ webHostBuilder
34+ . UseTestServer ( )
35+ . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
36+ . ConfigureServices ( s =>
37+ {
38+ s . AddStartupAction ( "Hey" , ( ) => resetEvent . Set ( ) ) ;
39+ s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
40+ } )
41+ . Configure ( app =>
42+ {
43+ app . UseReadyHealthChecks ( "Critical" ) ;
44+ } ) ;
3645 } )
37- . Configure ( app =>
38- {
39- app . UseReadyHealthChecks ( "Critical" ) ;
40- } ) ;
46+ . Build ( ) ;
47+
48+ await host . StartAsync ( TestCancellationToken ) ;
4149
42- var server = new TestServer ( builder ) ;
50+ var server = host . GetTestServer ( ) ;
4351
4452 await server . WaitForReadyAsync ( cancellationToken : TestCancellationToken ) ;
4553 await resetEvent . WaitAsync ( TestCancellationToken ) ;
@@ -54,23 +62,31 @@ public async Task WillRunSyncStartupAction()
5462 public async Task WillRunAsyncStartupAction ( )
5563 {
5664 var resetEvent = new AsyncManualResetEvent ( false ) ;
57- var builder = new WebHostBuilder ( )
58- . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
59- . ConfigureServices ( s =>
65+ using var host = new HostBuilder ( )
66+ . ConfigureWebHost ( webHostBuilder =>
6067 {
61- s . AddStartupAction ( "Hey" , ( ) =>
62- {
63- resetEvent . Set ( ) ;
64- return Task . CompletedTask ;
65- } ) ;
66- s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
68+ webHostBuilder
69+ . UseTestServer ( )
70+ . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
71+ . ConfigureServices ( s =>
72+ {
73+ s . AddStartupAction ( "Hey" , ( ) =>
74+ {
75+ resetEvent . Set ( ) ;
76+ return Task . CompletedTask ;
77+ } ) ;
78+ s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
79+ } )
80+ . Configure ( app =>
81+ {
82+ app . UseReadyHealthChecks ( "Critical" ) ;
83+ } ) ;
6784 } )
68- . Configure ( app =>
69- {
70- app . UseReadyHealthChecks ( "Critical" ) ;
71- } ) ;
85+ . Build ( ) ;
7286
73- var server = new TestServer ( builder ) ;
87+ await host . StartAsync ( TestCancellationToken ) ;
88+
89+ var server = host . GetTestServer ( ) ;
7490
7591 await server . WaitForReadyAsync ( cancellationToken : TestCancellationToken ) ;
7692 await resetEvent . WaitAsync ( TestCancellationToken ) ;
@@ -84,19 +100,27 @@ public async Task WillRunAsyncStartupAction()
84100 [ Fact ]
85101 public async Task WillRunClassStartupAction ( )
86102 {
87- var builder = new WebHostBuilder ( )
88- . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
89- . ConfigureServices ( s =>
103+ using var host = new HostBuilder ( )
104+ . ConfigureWebHost ( webHostBuilder =>
90105 {
91- s . AddStartupAction < TestStartupAction > ( "Hey" ) ;
92- s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
106+ webHostBuilder
107+ . UseTestServer ( )
108+ . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
109+ . ConfigureServices ( s =>
110+ {
111+ s . AddStartupAction < TestStartupAction > ( "Hey" ) ;
112+ s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
113+ } )
114+ . Configure ( app =>
115+ {
116+ app . UseReadyHealthChecks ( "Critical" ) ;
117+ } ) ;
93118 } )
94- . Configure ( app =>
95- {
96- app . UseReadyHealthChecks ( "Critical" ) ;
97- } ) ;
119+ . Build ( ) ;
98120
99- var server = new TestServer ( builder ) ;
121+ await host . StartAsync ( TestCancellationToken ) ;
122+
123+ var server = host . GetTestServer ( ) ;
100124
101125 await server . WaitForReadyAsync ( cancellationToken : TestCancellationToken ) ;
102126 Assert . True ( TestStartupAction . HasRun ) ;
@@ -110,20 +134,28 @@ public async Task WillRunClassStartupAction()
110134 [ Fact ]
111135 public async Task WillStopWaitingWhenStartupActionFails ( )
112136 {
113- var builder = new WebHostBuilder ( )
114- . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
115- . CaptureStartupErrors ( true )
116- . ConfigureServices ( s =>
137+ using var host = new HostBuilder ( )
138+ . ConfigureWebHost ( webHostBuilder =>
117139 {
118- s . AddStartupAction ( "Boom" , ( ) => throw new ApplicationException ( "Boom" ) ) ;
119- s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
140+ webHostBuilder
141+ . UseTestServer ( )
142+ . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
143+ . CaptureStartupErrors ( true )
144+ . ConfigureServices ( s =>
145+ {
146+ s . AddStartupAction ( "Boom" , ( ) => throw new ApplicationException ( "Boom" ) ) ;
147+ s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
148+ } )
149+ . Configure ( app =>
150+ {
151+ app . UseReadyHealthChecks ( "Critical" ) ;
152+ } ) ;
120153 } )
121- . Configure ( app =>
122- {
123- app . UseReadyHealthChecks ( "Critical" ) ;
124- } ) ;
154+ . Build ( ) ;
155+
156+ await host . StartAsync ( TestCancellationToken ) ;
125157
126- var server = new TestServer ( builder ) ;
158+ var server = host . GetTestServer ( ) ;
127159
128160 var sw = Stopwatch . StartNew ( ) ;
129161 await Assert . ThrowsAsync < OperationCanceledException > ( ( ) => server . WaitForReadyAsync ( cancellationToken : TestCancellationToken ) ) ;
@@ -135,21 +167,29 @@ public async Task WillStopWaitingWhenStartupActionFails()
135167 [ Fact ]
136168 public async Task WillHandleNoRegisteredStartupActions ( )
137169 {
138- var builder = new WebHostBuilder ( )
139- . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
140- . UseEnvironment ( Environments . Development )
141- . CaptureStartupErrors ( true )
142- . ConfigureServices ( s =>
170+ using var host = new HostBuilder ( )
171+ . ConfigureWebHost ( webHostBuilder =>
143172 {
144- s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
173+ webHostBuilder
174+ . UseTestServer ( )
175+ . ConfigureLogging ( l => l . AddTestLogger ( _output ) )
176+ . UseEnvironment ( Environments . Development )
177+ . CaptureStartupErrors ( true )
178+ . ConfigureServices ( s =>
179+ {
180+ s . AddHealthChecks ( ) . AddCheckForStartupActions ( "Critical" ) ;
181+ } )
182+ . Configure ( app =>
183+ {
184+ app . UseReadyHealthChecks ( "Critical" ) ;
185+ app . UseWaitForStartupActionsBeforeServingRequests ( ) ;
186+ } ) ;
145187 } )
146- . Configure ( app =>
147- {
148- app . UseReadyHealthChecks ( "Critical" ) ;
149- app . UseWaitForStartupActionsBeforeServingRequests ( ) ;
150- } ) ;
188+ . Build ( ) ;
189+
190+ await host . StartAsync ( TestCancellationToken ) ;
151191
152- var server = new TestServer ( builder ) ;
192+ var server = host . GetTestServer ( ) ;
153193
154194 var sw = Stopwatch . StartNew ( ) ;
155195 await server . WaitForReadyAsync ( cancellationToken : TestCancellationToken ) ;
0 commit comments