1- using Microsoft . Extensions . Logging ;
2- using System . Runtime . CompilerServices ;
3- using Xunit . Abstractions ;
41using Aspire . Hosting ;
52using Microsoft . Extensions . Configuration ;
63using Microsoft . Extensions . Hosting ;
4+ using Microsoft . Extensions . Logging ;
5+ using System . Runtime . CompilerServices ;
6+ using System . Threading ;
7+ using Xunit . Abstractions ;
78
89namespace SWATestProject . Tests ;
910
1011public class SWAIntegrationTests ( ITestOutputHelper outputHelper )
1112{
1213 private void WriteFunctionName ( [ CallerMemberName ] string ? caller = null ) => outputHelper . WriteLine ( caller ) ;
13- private static readonly TimeSpan WaitForHealthyTimeout = TimeSpan . FromSeconds ( 90 ) ;
14+ private const int WaitForHealthyTimeoutSeconds = 90 ;
15+ private static readonly TimeSpan WaitForHealthyTimeout = TimeSpan . FromSeconds ( WaitForHealthyTimeoutSeconds ) ;
1416
1517 private async Task < IDistributedApplicationTestingBuilder > CreateAppHostAsync ( )
1618 {
@@ -26,6 +28,7 @@ private async Task<IDistributedApplicationTestingBuilder> CreateAppHostAsync()
2628
2729 //ConfigureOtlpOverHttp(host, outputHelper.WriteLine);
2830
31+ host . Configuration ! . AddUserSecrets < SWAIntegrationTests > ( ) ;
2932 //host.EnvironmentName = "Test";
3033 //dab.AssemblyName = this.GetType().Assembly.GetName().Name;
3134 } ) ;
@@ -43,12 +46,13 @@ private async Task<IDistributedApplicationTestingBuilder> CreateAppHostAsync()
4346
4447 appHost . Services . ConfigureHttpClientDefaults ( clientBuilder =>
4548 {
46-
49+ var timeout = TimeSpan . FromSeconds ( appHost . Configuration . GetValue < double > ( "HttpClientTimeout" , 30 ) ) ;
4750 clientBuilder
48- . AddStandardResilienceHandler ( res=> {
49- res . TotalRequestTimeout . Timeout = TimeSpan . FromSeconds ( 120 ) ;
50- res . CircuitBreaker . SamplingDuration = TimeSpan . FromSeconds ( 60 ) ;
51- res . AttemptTimeout . Timeout = TimeSpan . FromSeconds ( 30 ) ;
51+ . AddStandardResilienceHandler ( res =>
52+ {
53+ res . TotalRequestTimeout . Timeout = timeout * 4 ;
54+ res . CircuitBreaker . SamplingDuration = timeout * 2 ;
55+ res . AttemptTimeout . Timeout = timeout ;
5256 } ) ;
5357 clientBuilder
5458 . ConfigurePrimaryHttpMessageHandler ( ( ) =>
@@ -130,10 +134,11 @@ public async Task GetFrameworkRootReturnsOkStatusCode()
130134 await using Aspire . Hosting . DistributedApplication app = await ArrangeAppHostAsync ( ) ;
131135 var resourceNotificationService = app . Services . GetRequiredService < ResourceNotificationService > ( ) ;
132136 await app . StartAsync ( ) ;
137+ var timeout = TimeSpan . FromSeconds ( app . Services . GetRequiredService < IConfiguration > ( ) . GetValue < double > ( "WaitForHealthyTimeoutSeconds" , WaitForHealthyTimeoutSeconds ) ) ;
133138
134139 // Act
135140 var httpClient = app . CreateHttpClient ( "framework" , "http" ) ;
136- await resourceNotificationService . WaitForResourceAsync ( "framework" , KnownResourceStates . Running ) . WaitAsync ( WaitForHealthyTimeout ) ;
141+ await resourceNotificationService . WaitForResourceAsync ( "framework" , KnownResourceStates . Running ) . WaitAsync ( timeout ) ;
137142 var response = await httpClient . GetAsync ( "/" ) ;
138143
139144 // Assert
@@ -149,11 +154,13 @@ public async Task GetCoreRootReturnsOkStatusCode()
149154 await using Aspire . Hosting . DistributedApplication app = await ArrangeAppHostAsync ( ) ;
150155 var resourceNotificationService = app . Services . GetRequiredService < ResourceNotificationService > ( ) ;
151156 await app . StartAsync ( ) ;
157+ var timeout = TimeSpan . FromSeconds ( app . Services . GetRequiredService < IConfiguration > ( ) . GetValue < double > ( "WaitForHealthyTimeoutSeconds" , WaitForHealthyTimeoutSeconds ) ) ;
158+
152159
153160 // Act
154161 var httpClient = app . CreateHttpClient ( "core" , "https" ) ;
155- await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( TimeSpan . FromSeconds ( 90 ) ) ;
156- await resourceNotificationService . WaitForResourceHealthyAsync ( "core" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( TimeSpan . FromSeconds ( 90 ) ) ;
162+ await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( timeout ) ;
163+ await resourceNotificationService . WaitForResourceHealthyAsync ( "core" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( timeout ) ;
157164 var response = await httpClient . GetAsync ( "/" ) ;
158165
159166 // Assert
@@ -169,11 +176,12 @@ public async Task GetFrameworkSessionReturnsOkStatusCode()
169176 await using Aspire . Hosting . DistributedApplication app = await ArrangeAppHostAsync ( ) ;
170177 var resourceNotificationService = app . Services . GetRequiredService < ResourceNotificationService > ( ) ;
171178 await app . StartAsync ( ) ;
179+ var timeout = TimeSpan . FromSeconds ( app . Services . GetRequiredService < IConfiguration > ( ) . GetValue < double > ( "WaitForHealthyTimeoutSeconds" , WaitForHealthyTimeoutSeconds ) ) ;
172180
173181 // Act
174182 var httpClient = app . CreateHttpClient ( "framework" , "http" ) ;
175- await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( WaitForHealthyTimeout ) ;
176- await resourceNotificationService . WaitForResourceHealthyAsync ( "core" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( WaitForHealthyTimeout ) ;
183+ await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( timeout ) ;
184+ await resourceNotificationService . WaitForResourceHealthyAsync ( "core" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( timeout ) ;
177185 var response = await httpClient . GetAsync ( "/framework" ) ;
178186
179187 // Assert
@@ -190,15 +198,16 @@ public async Task GetFrameworkHttpsWorksSometimes()
190198 await using Aspire . Hosting . DistributedApplication app = await appHost . BuildAsync ( ) ;
191199 var resourceNotificationService = app . Services . GetRequiredService < ResourceNotificationService > ( ) ;
192200 await app . StartAsync ( ) ;
193-
201+ var timeout = TimeSpan . FromSeconds ( app . Services . GetRequiredService < IConfiguration > ( ) . GetValue < double > ( "WaitForHealthyTimeoutSeconds" , WaitForHealthyTimeoutSeconds ) ) ;
202+
194203 var httpClient = app . CreateHttpClient ( "framework" , "https" ) ;
195- await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( WaitForHealthyTimeout ) ;
204+ await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( timeout ) ;
196205
197206 try
198207 {
199208 // Act
200209 var response = await httpClient . GetAsync ( "/" ) ;
201-
210+
202211 // Assert
203212 Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
204213 }
@@ -207,7 +216,7 @@ public async Task GetFrameworkHttpsWorksSometimes()
207216 outputHelper . WriteLine ( ex . Message ) ;
208217
209218 // Fix
210- var resource = app . Services . GetRequiredService < DistributedApplicationModel > ( ) . Resources . Single ( r=> r . Name == "framework" ) as IResourceWithEndpoints ;
219+ var resource = app . Services . GetRequiredService < DistributedApplicationModel > ( ) . Resources . Single ( r => r . Name == "framework" ) as IResourceWithEndpoints ;
211220 Assert . NotNull ( resource ) ;
212221 await resource . ExecuteFixHttpsCommand ( app . Services ) ;
213222
@@ -227,11 +236,13 @@ public async Task GetProxiedFrameworkSessionReturnsOkStatusCode()
227236 await using Aspire . Hosting . DistributedApplication app = await ArrangeAppHostAsync ( ) ;
228237 var resourceNotificationService = app . Services . GetRequiredService < ResourceNotificationService > ( ) ;
229238 await app . StartAsync ( ) ;
239+ var timeout = TimeSpan . FromSeconds ( app . Services . GetRequiredService < IConfiguration > ( ) . GetValue < double > ( "WaitForHealthyTimeoutSeconds" , WaitForHealthyTimeoutSeconds ) ) ;
240+
230241
231242 // Act
232243 var httpClient = app . CreateHttpClient ( "core" , "https" ) ;
233- await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( WaitForHealthyTimeout ) ;
234- await resourceNotificationService . WaitForResourceHealthyAsync ( "core" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( WaitForHealthyTimeout ) ;
244+ await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( timeout ) ;
245+ await resourceNotificationService . WaitForResourceHealthyAsync ( "core" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( timeout ) ;
235246 var response = await httpClient . GetAsync ( "/framework" ) ;
236247
237248 // Assert
@@ -247,11 +258,13 @@ public async Task GetCoreSessionReturnsOkStatusCode()
247258 await using Aspire . Hosting . DistributedApplication app = await ArrangeAppHostAsync ( ) ;
248259 var resourceNotificationService = app . Services . GetRequiredService < ResourceNotificationService > ( ) ;
249260 await app . StartAsync ( ) ;
261+ var timeout = TimeSpan . FromSeconds ( app . Services . GetRequiredService < IConfiguration > ( ) . GetValue < double > ( "WaitForHealthyTimeoutSeconds" , WaitForHealthyTimeoutSeconds ) ) ;
262+
250263
251264 // Act
252265 var httpClient = app . CreateHttpClient ( "core" , "https" ) ;
253- await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( WaitForHealthyTimeout ) ;
254- await resourceNotificationService . WaitForResourceHealthyAsync ( "core" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( WaitForHealthyTimeout ) ;
266+ await resourceNotificationService . WaitForResourceHealthyAsync ( "framework" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( timeout ) ;
267+ await resourceNotificationService . WaitForResourceHealthyAsync ( "core" , WaitBehavior . StopOnResourceUnavailable ) . WaitAsync ( timeout ) ;
255268 var response = await httpClient . GetAsync ( "/core" ) ;
256269
257270 // Assert
0 commit comments