Skip to content

Commit 5873c75

Browse files
Update WaitForResourceHealthyAsync to use DefaultWaitBehavior (#7738)
Co-authored-by: afscrome <[email protected]>
1 parent f209b76 commit 5873c75

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public async Task<ResourceEvent> WaitForResourceHealthyAsync(string resourceName
242242
{
243243
return await WaitForResourceHealthyAsync(
244244
resourceName,
245-
WaitBehavior.WaitOnResourceUnavailable, // Retain default behavior.
245+
DefaultWaitBehavior,
246246
cancellationToken).ConfigureAwait(false);
247247
}
248248

tests/Aspire.Hosting.Tests/WaitForTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,34 @@ await app.ResourceNotifications.WaitForResourceHealthyAsync(
242242
Assert.Equal("The operation has timed out.", ex.Message);
243243
}
244244

245+
[Theory]
246+
[InlineData(WaitBehavior.WaitOnResourceUnavailable, typeof(TimeoutException), "The operation has timed out.")]
247+
[InlineData(WaitBehavior.StopOnResourceUnavailable, typeof(DistributedApplicationException), "Stopped waiting for resource 'redis' to become healthy because it failed to start.")]
248+
public async Task WhenWaitBehaviorIsMissingWaitForResourceHealthyAsyncShouldUseDefaultWaitBehavior(WaitBehavior defaultWaitBehavior, Type exceptionType, string exceptionMessage)
249+
{
250+
using var builder = TestDistributedApplicationBuilder.Create().WithTestAndResourceLogging(testOutputHelper);
251+
252+
builder.Services.Configure<ResourceNotificationServiceOptions>(o =>
253+
{
254+
o.DefaultWaitBehavior = defaultWaitBehavior;
255+
});
256+
257+
var failToStart = builder.AddExecutable("failToStart", "does-not-exist", ".");
258+
var dependency = builder.AddContainer("redis", "redis");
259+
260+
dependency.WaitFor(failToStart, WaitBehavior.StopOnResourceUnavailable);
261+
262+
using var app = builder.Build();
263+
await app.StartAsync();
264+
265+
var ex = await Assert.ThrowsAsync(exceptionType, async () => {
266+
await app.ResourceNotifications.WaitForResourceHealthyAsync(dependency.Resource.Name)
267+
.WaitAsync(TimeSpan.FromSeconds(15));
268+
});
269+
270+
Assert.Equal(exceptionMessage, ex.Message);
271+
}
272+
245273
[Theory]
246274
[InlineData(nameof(KnownResourceStates.Exited))]
247275
[InlineData(nameof(KnownResourceStates.FailedToStart))]

0 commit comments

Comments
 (0)