|
3 | 3 |
|
4 | 4 | using System.Net.Sockets; |
5 | 5 | using Aspire.Hosting.ApplicationModel; |
| 6 | +using Aspire.Hosting.Eventing; |
6 | 7 | using Aspire.Hosting.Utils; |
7 | 8 | using Microsoft.Extensions.DependencyInjection; |
8 | 9 |
|
@@ -176,4 +177,55 @@ public void WithKafkaUIAddsAnUniqueContainerSetsItsNameAndInvokesConfigurationCa |
176 | 177 | Assert.Equal(8080, kafkaUiEndpoint.TargetPort); |
177 | 178 | Assert.Equal(port, kafkaUiEndpoint.Port); |
178 | 179 | } |
| 180 | + |
| 181 | + [Fact] |
| 182 | + public async Task KafkaEnvironmentCallbackIsIdempotent() |
| 183 | + { |
| 184 | + using var appBuilder = TestDistributedApplicationBuilder.Create(); |
| 185 | + |
| 186 | + var kafka = appBuilder.AddKafka("kafka") |
| 187 | + .WithEndpoint("tcp", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 27017)); |
| 188 | + |
| 189 | + // Call GetEnvironmentVariableValuesAsync multiple times to ensure callbacks are idempotent |
| 190 | + var config1 = await kafka.Resource.GetEnvironmentVariableValuesAsync(); |
| 191 | + var config2 = await kafka.Resource.GetEnvironmentVariableValuesAsync(); |
| 192 | + |
| 193 | + // Both calls should succeed and return the same values |
| 194 | + Assert.Equal(config1.Count, config2.Count); |
| 195 | + Assert.Contains(config1, kvp => kvp.Key == "KAFKA_LISTENERS"); |
| 196 | + Assert.Contains(config2, kvp => kvp.Key == "KAFKA_LISTENERS"); |
| 197 | + Assert.Equal( |
| 198 | + config1.First(kvp => kvp.Key == "KAFKA_LISTENERS").Value, |
| 199 | + config2.First(kvp => kvp.Key == "KAFKA_LISTENERS").Value); |
| 200 | + } |
| 201 | + |
| 202 | + [Fact] |
| 203 | + public async Task KafkaUIEnvironmentCallbackIsIdempotent() |
| 204 | + { |
| 205 | + using var appBuilder = TestDistributedApplicationBuilder.Create(); |
| 206 | + |
| 207 | + var kafka = appBuilder.AddKafka("kafka1") |
| 208 | + .WithEndpoint("tcp", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 27017)) |
| 209 | + .WithKafkaUI(); |
| 210 | + |
| 211 | + using var app = appBuilder.Build(); |
| 212 | + var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); |
| 213 | + var kafkaUiResource = Assert.Single(appModel.Resources.OfType<KafkaUIContainerResource>()); |
| 214 | + |
| 215 | + // Trigger the BeforeResourceStartedEvent to add environment callbacks |
| 216 | + await appBuilder.Eventing.PublishAsync( |
| 217 | + new BeforeResourceStartedEvent(kafkaUiResource, app.Services), |
| 218 | + EventDispatchBehavior.BlockingSequential); |
| 219 | + |
| 220 | + // Call GetEnvironmentVariableValuesAsync multiple times to ensure callbacks are idempotent |
| 221 | + var config1 = await kafkaUiResource.GetEnvironmentVariableValuesAsync(); |
| 222 | + var config2 = await kafkaUiResource.GetEnvironmentVariableValuesAsync(); |
| 223 | + |
| 224 | + // Both calls should succeed and return the same values |
| 225 | + Assert.Equal(config1.Count, config2.Count); |
| 226 | + Assert.Contains(config1, kvp => kvp.Key == "KAFKA_CLUSTERS_0_NAME"); |
| 227 | + Assert.Contains(config2, kvp => kvp.Key == "KAFKA_CLUSTERS_0_NAME"); |
| 228 | + Assert.Equal("kafka1", config1.First(kvp => kvp.Key == "KAFKA_CLUSTERS_0_NAME").Value); |
| 229 | + Assert.Equal("kafka1", config2.First(kvp => kvp.Key == "KAFKA_CLUSTERS_0_NAME").Value); |
| 230 | + } |
179 | 231 | } |
0 commit comments