Skip to content

Commit a29edfd

Browse files
committed
Update WaitForResourceHealthyAsync to use DefaultWaitBehavior
1 parent a1136f8 commit a29edfd

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
@@ -233,7 +233,7 @@ public async Task<ResourceEvent> WaitForResourceHealthyAsync(string resourceName
233233
{
234234
return await WaitForResourceHealthyAsync(
235235
resourceName,
236-
WaitBehavior.WaitOnResourceUnavailable, // Retain default behavior.
236+
DefaultWaitBehavior,
237237
cancellationToken).ConfigureAwait(false);
238238
}
239239

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)