Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jan 23, 2025
1 parent 86b4240 commit 166d3d6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
13 changes: 12 additions & 1 deletion tests/Aspire.Hosting.Tests/Dcp/DcpExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -963,13 +963,24 @@ public async Task ErrorIfResourceNotDeletedBeforeRestart()
var kubernetesService = new TestKubernetesService();
using var app = builder.Build();
var distributedAppModel = app.Services.GetRequiredService<DistributedApplicationModel>();
var appExecutor = CreateAppExecutor(distributedAppModel, kubernetesService: kubernetesService);
var dcpEvents = new DcpExecutorEvents();
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
dcpEvents.Subscribe<OnResourceFailedToStartContext>(c =>
{
tcs.SetResult();
return Task.CompletedTask;
});

var appExecutor = CreateAppExecutor(distributedAppModel, kubernetesService: kubernetesService, events: dcpEvents);
await appExecutor.RunApplicationAsync();

var dcpCtr = Assert.Single(kubernetesService.CreatedResources.OfType<Container>());

var ex = await Assert.ThrowsAsync<DistributedApplicationException>(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]
Expand Down
15 changes: 15 additions & 0 deletions tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -277,6 +279,13 @@ public async Task VerifyContainerStopStartWorks()

await using var app = testProgram.Build();

var events = app.Services.GetRequiredService<IDistributedApplicationEventing>();
var beforeResourceStartedEvents = Channel.CreateUnbounded<BeforeResourceStartedEvent>();
events.Subscribe<BeforeResourceStartedEvent>(async (e, ct) =>
{
await beforeResourceStartedEvents.Writer.WriteAsync(e, ct);
});

var kubernetes = app.Services.GetRequiredService<IKubernetesService>();
var orchestrator = app.Services.GetRequiredService<ApplicationOrchestrator>();
var suffix = app.Services.GetRequiredService<IOptions<DcpOptions>>().Value.ResourceNameSuffix;
Expand All @@ -290,13 +299,19 @@ public async Task VerifyContainerStopStartWorks()
var redisContainer = await KubernetesHelper.GetResourceByNameMatchAsync<Container>(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<Container>(kubernetes, containerPattern, r => r.Status?.State == ContainerState.Exited, token).DefaultTimeout(TestConstants.DefaultOrchestratorTestLongTimeout);
Assert.NotNull(redisContainer);

await orchestrator.StartResourceAsync(redisContainer.Metadata.Name, token);

// Restart event.
await beforeResourceStartedEvents.Reader.ReadAsync().DefaultTimeout();

redisContainer = await KubernetesHelper.GetResourceByNameMatchAsync<Container>(kubernetes, containerPattern, r => r.Status?.State == ContainerState.Running, token);
Assert.NotNull(redisContainer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IDistributedApplicationLifecycleHook>(),
notificationService,
new DistributedApplicationEventing(),
applicationEventing ?? new DistributedApplicationEventing(),
new ServiceCollection().BuildServiceProvider()
);
}
Expand Down

0 comments on commit 166d3d6

Please sign in to comment.