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
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ private async Task OnBeforeStartAsync(BeforeStartEvent @event, CancellationToken

var daprSidecar = daprAnnotation.Sidecar;


// Propagate WaitAnnotations from the original resource to the Dapr sidecar
if (resource.TryGetAnnotationsOfType<WaitAnnotation>(out var waitAnnotations))
{
foreach (var waitAnnotation in waitAnnotations)
{
daprSidecar.Annotations.Add(waitAnnotation);
}
}

var sidecarOptionsAnnotation = daprSidecar.Annotations.OfType<DaprSidecarOptionsAnnotation>().LastOrDefault();

var sidecarOptions = sidecarOptionsAnnotation?.Options;
Expand Down Expand Up @@ -205,6 +213,15 @@ private async Task OnBeforeStartAsync(BeforeStartEvent @event, CancellationToken
var daprCliResourceName = $"{daprSidecar.Name}-cli";
var daprCli = new ExecutableResource(daprCliResourceName, fileName, appHostDirectory);

// Propagate WaitAnnotations from the original resource to the Dapr CLI executable
if (resource.TryGetAnnotationsOfType<WaitAnnotation>(out var resourceWaitAnnotations))
{
foreach (var waitAnnotation in resourceWaitAnnotations)
{
daprCli.Annotations.Add(waitAnnotation);
}
}

// Make the Dapr CLI wait for the component resources it references
foreach (var componentRef in componentReferenceAnnotations)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;

namespace CommunityToolkit.Aspire.Hosting.Dapr.Tests;

Expand Down Expand Up @@ -111,4 +110,72 @@ public void DaprSidecarCanReferenceComponents()
Assert.Contains(referenceAnnotations, a => a.Component.Name == "statestore");
Assert.Contains(referenceAnnotations, a => a.Component.Name == "pubsub");
}

[Fact]
public void ResourceWithDaprSidecarAndNoWaitAnnotations_CreatesBasicSidecar()
{
var builder = DistributedApplication.CreateBuilder();

var app = builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Dapr_ServiceA>("test")
.WithDaprSidecar();

// Verify no wait annotations on the main resource
Assert.Empty(app.Resource.Annotations.OfType<WaitAnnotation>());

// Verify the sidecar resource exists
var sidecarResource = Assert.Single(builder.Resources.OfType<DaprSidecarResource>());

// Verify the sidecar is properly linked
var sidecarAnnotation = Assert.Single(app.Resource.Annotations.OfType<DaprSidecarAnnotation>());
Assert.Equal(sidecarResource, sidecarAnnotation.Sidecar);
}

[Fact]
public void ResourceWithWaitAnnotationAndDaprSidecar_SetsUpCorrectDependencies()
{
var builder = DistributedApplication.CreateBuilder();

var database = builder.AddContainer("db", "postgres");

var app = builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Dapr_ServiceA>("test")
.WaitFor(database)
.WithDaprSidecar();

// Verify the main resource has the wait annotation
var waitAnnotation = Assert.Single(app.Resource.Annotations.OfType<WaitAnnotation>());
Assert.Equal("db", waitAnnotation.Resource.Name);

// Verify the sidecar resource exists
var sidecarResource = Assert.Single(builder.Resources.OfType<DaprSidecarResource>());
Assert.NotNull(sidecarResource);

// The actual propagation happens in the lifecycle hook, but we can verify the setup is correct
var sidecarAnnotation = Assert.Single(app.Resource.Annotations.OfType<DaprSidecarAnnotation>());
Assert.Equal(sidecarResource, sidecarAnnotation.Sidecar);
}

[Fact]
public void ResourceWithMultipleWaitAnnotationsAndDaprSidecar_HasAllWaitDependencies()
{
var builder = DistributedApplication.CreateBuilder();

var database = builder.AddContainer("db", "postgres");
var redis = builder.AddContainer("cache", "redis");

var app = builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Dapr_ServiceA>("test")
.WaitFor(database)
.WaitFor(redis)
.WithDaprSidecar();

// Verify the main resource has both wait annotations
var waitAnnotations = app.Resource.Annotations.OfType<WaitAnnotation>().ToList();
Assert.Equal(2, waitAnnotations.Count);
Assert.Contains(waitAnnotations, w => w.Resource.Name == "db");
Assert.Contains(waitAnnotations, w => w.Resource.Name == "cache");

// Verify the sidecar resource exists and is properly linked
var sidecarResource = Assert.Single(builder.Resources.OfType<DaprSidecarResource>());
var sidecarAnnotation = Assert.Single(app.Resource.Annotations.OfType<DaprSidecarAnnotation>());
Assert.Equal(sidecarResource, sidecarAnnotation.Sidecar);
}
Comment on lines +114 to +180
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests verify the setup of WaitAnnotations on the main resource but do not verify that these annotations are actually propagated to the Dapr sidecar and CLI resources during lifecycle hook execution. Consider adding an integration test that calls app.Build() and app.StartAsync() to verify that the lifecycle hook properly propagates WaitAnnotations to the sidecar and CLI resources. This would ensure the propagation logic works as expected at runtime.

Copilot uses AI. Check for mistakes.
}
Loading