Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 8 additions & 31 deletions tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,43 +116,20 @@ public void RegisteredLifecycleHookIsExecutedWhenRunSynchronously()
}

[Fact]
public async Task TryAddWillNotAddTheSameLifecycleHook()
public void TryAddWillNotAddTheSameLifecycleHook()
{
var exceptionMessage = "Exception from lifecycle hook to prove it ran!";

var signal = (FirstHookExecuted: false, SecondHookExecuted: false);

var testProgram = CreateTestProgram();

// Lifecycle hook 1
testProgram.AppBuilder.Services.TryAddLifecycleHook((sp) =>
{
return new CallbackLifecycleHook((app, cancellationToken) =>
{
signal.FirstHookExecuted = true;
return Task.CompletedTask;
});
});
var callback1 = (IServiceProvider sp) => new DummyLifecycleHook();
testProgram.AppBuilder.Services.TryAddLifecycleHook(callback1);

// Lifecycle hook 2
testProgram.AppBuilder.Services.TryAddLifecycleHook((sp) =>
{
return new CallbackLifecycleHook((app, cancellationToken) =>
{
signal.SecondHookExecuted = true;

// We still want to throw on the second one to block startup.
throw new DistributedApplicationException(exceptionMessage);
});
});
var callback2 = (IServiceProvider sp) => new DummyLifecycleHook();
testProgram.AppBuilder.Services.TryAddLifecycleHook(callback2);

using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMinutes(1));
await using var app = testProgram.Build();
await app.StartAsync(cts.Token);
var lifecycleHookDescriptors = testProgram.AppBuilder.Services.Where(sd => sd.ServiceType == typeof(IDistributedApplicationLifecycleHook));

Assert.True(signal.FirstHookExecuted);
Assert.False(signal.SecondHookExecuted);
Assert.Single(lifecycleHookDescriptors.Where(sd => sd.ImplementationFactory == callback1));
Assert.Empty(lifecycleHookDescriptors.Where(sd => sd.ImplementationFactory == callback2));
}

[LocalOnlyFact]
Expand Down
10 changes: 10 additions & 0 deletions tests/Aspire.Hosting.Tests/Helpers/DummyLifecycleHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.Lifecycle;

namespace Aspire.Hosting.Tests.Helpers;

internal sealed class DummyLifecycleHook : IDistributedApplicationLifecycleHook
{
}