diff --git a/tests/Aspire.Hosting.Tests/Dcp/DcpExecutorTests.cs b/tests/Aspire.Hosting.Tests/Dcp/DcpExecutorTests.cs index 1be1f7931a..502c059b31 100644 --- a/tests/Aspire.Hosting.Tests/Dcp/DcpExecutorTests.cs +++ b/tests/Aspire.Hosting.Tests/Dcp/DcpExecutorTests.cs @@ -963,13 +963,24 @@ public async Task ErrorIfResourceNotDeletedBeforeRestart() var kubernetesService = new TestKubernetesService(); using var app = builder.Build(); var distributedAppModel = app.Services.GetRequiredService(); - var appExecutor = CreateAppExecutor(distributedAppModel, kubernetesService: kubernetesService); + var dcpEvents = new DcpExecutorEvents(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + dcpEvents.Subscribe(c => + { + tcs.SetResult(); + return Task.CompletedTask; + }); + + var appExecutor = CreateAppExecutor(distributedAppModel, kubernetesService: kubernetesService, events: dcpEvents); await appExecutor.RunApplicationAsync(); var dcpCtr = Assert.Single(kubernetesService.CreatedResources.OfType()); var ex = await Assert.ThrowsAsync(async () => await appExecutor.StartResourceAsync(dcpCtr.Metadata.Name, CancellationToken.None)); Assert.Equal($"Failed to delete '{dcpCtr.Metadata.Name}' successfully before restart.", ex.Message); + + // Verify failed to start event. + await tcs.Task.DefaultTimeout(); } [Fact] diff --git a/tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs b/tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs index d124bc279a..308fd72acc 100644 --- a/tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs +++ b/tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs @@ -3,9 +3,11 @@ using System.Globalization; using System.Text.RegularExpressions; +using System.Threading.Channels; using Aspire.Components.Common.Tests; using Aspire.Hosting.Dcp; using Aspire.Hosting.Dcp.Model; +using Aspire.Hosting.Eventing; using Aspire.Hosting.Lifecycle; using Aspire.Hosting.Orchestrator; using Aspire.Hosting.Testing; @@ -277,6 +279,13 @@ public async Task VerifyContainerStopStartWorks() await using var app = testProgram.Build(); + var events = app.Services.GetRequiredService(); + var beforeResourceStartedEvents = Channel.CreateUnbounded(); + events.Subscribe(async (e, ct) => + { + await beforeResourceStartedEvents.Writer.WriteAsync(e, ct); + }); + var kubernetes = app.Services.GetRequiredService(); var orchestrator = app.Services.GetRequiredService(); var suffix = app.Services.GetRequiredService>().Value.ResourceNameSuffix; @@ -290,6 +299,9 @@ public async Task VerifyContainerStopStartWorks() var redisContainer = await KubernetesHelper.GetResourceByNameMatchAsync(kubernetes, containerPattern, r => r.Status?.State == ContainerState.Running, token).DefaultTimeout(TestConstants.DefaultOrchestratorTestLongTimeout); Assert.NotNull(redisContainer); + // Initial startup event. + await beforeResourceStartedEvents.Reader.ReadAsync().DefaultTimeout(); + await orchestrator.StopResourceAsync(redisContainer.Metadata.Name, token).DefaultTimeout(TestConstants.DefaultOrchestratorTestTimeout); redisContainer = await KubernetesHelper.GetResourceByNameMatchAsync(kubernetes, containerPattern, r => r.Status?.State == ContainerState.Exited, token).DefaultTimeout(TestConstants.DefaultOrchestratorTestLongTimeout); @@ -297,6 +309,9 @@ public async Task VerifyContainerStopStartWorks() await orchestrator.StartResourceAsync(redisContainer.Metadata.Name, token); + // Restart event. + await beforeResourceStartedEvents.Reader.ReadAsync().DefaultTimeout(); + redisContainer = await KubernetesHelper.GetResourceByNameMatchAsync(kubernetes, containerPattern, r => r.Status?.State == ContainerState.Running, token); Assert.NotNull(redisContainer); diff --git a/tests/Aspire.Hosting.Tests/Orchestrator/ApplicationOrchestratorTests.cs b/tests/Aspire.Hosting.Tests/Orchestrator/ApplicationOrchestratorTests.cs index 3b60489bf3..aad53a9da6 100644 --- a/tests/Aspire.Hosting.Tests/Orchestrator/ApplicationOrchestratorTests.cs +++ b/tests/Aspire.Hosting.Tests/Orchestrator/ApplicationOrchestratorTests.cs @@ -29,7 +29,7 @@ public async Task ParentPropertySetOnChildResource() var events = new DcpExecutorEvents(); var resourceNotificationService = ResourceNotificationServiceTestHelpers.Create(); - var appOrchestrator = CreateOrchestrator(distributedAppModel, notificationService: resourceNotificationService, events: events); + var appOrchestrator = CreateOrchestrator(distributedAppModel, notificationService: resourceNotificationService, dcpEvents: events); await appOrchestrator.RunApplicationAsync(); string? parentResourceId = null; @@ -64,15 +64,16 @@ public async Task ParentPropertySetOnChildResource() private static ApplicationOrchestrator CreateOrchestrator( DistributedApplicationModel distributedAppModel, ResourceNotificationService notificationService, - DcpExecutorEvents? events = null) + DcpExecutorEvents? dcpEvents = null, + DistributedApplicationEventing? applicationEventing = null) { return new ApplicationOrchestrator( distributedAppModel, new TestDcpExecutor(), - events ?? new DcpExecutorEvents(), + dcpEvents ?? new DcpExecutorEvents(), Array.Empty(), notificationService, - new DistributedApplicationEventing(), + applicationEventing ?? new DistributedApplicationEventing(), new ServiceCollection().BuildServiceProvider() ); }