|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Aspire.Components.Common.Tests; |
| 5 | +using Aspire.Hosting.Utils; |
| 6 | +using Confluent.Kafka; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Hosting; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Xunit; |
| 12 | +using Xunit.Abstractions; |
| 13 | + |
| 14 | +namespace Aspire.Hosting.Kafka.Tests; |
| 15 | + |
| 16 | +public class KafkaFunctionalTests(ITestOutputHelper testOutputHelper) |
| 17 | +{ |
| 18 | + [Fact] |
| 19 | + [RequiresDocker] |
| 20 | + public async Task VerifyKafkaResource() |
| 21 | + { |
| 22 | + var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); |
| 23 | + |
| 24 | + var builder = CreateDistributedApplicationBuilder(); |
| 25 | + |
| 26 | + var kafka = builder.AddKafka("kafka"); |
| 27 | + |
| 28 | + using var app = builder.Build(); |
| 29 | + |
| 30 | + await app.StartAsync(); |
| 31 | + |
| 32 | + var hb = Host.CreateApplicationBuilder(); |
| 33 | + |
| 34 | + hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> |
| 35 | + { |
| 36 | + [$"ConnectionStrings:{kafka.Resource.Name}"] = await kafka.Resource.ConnectionStringExpression.GetValueAsync(default) |
| 37 | + }); |
| 38 | + |
| 39 | + hb.AddKafkaProducer<string, string>("kafka"); |
| 40 | + hb.AddKafkaConsumer<string, string>("kafka", consumerBuilder => |
| 41 | + { |
| 42 | + consumerBuilder.Config.GroupId = "aspire-consumer-group"; |
| 43 | + consumerBuilder.Config.AutoOffsetReset = AutoOffsetReset.Earliest; |
| 44 | + }); |
| 45 | + |
| 46 | + using var host = hb.Build(); |
| 47 | + |
| 48 | + await host.StartAsync(); |
| 49 | + |
| 50 | + var topic = "test-topic"; |
| 51 | + var producer = host.Services.GetRequiredService<IProducer<string, string>>(); |
| 52 | + for (var i = 0; i < 10; i++) |
| 53 | + { |
| 54 | + await producer.ProduceAsync(topic, new Message<string, string> { Key = "test-key", Value = $"test-value{i}" }); |
| 55 | + } |
| 56 | + |
| 57 | + var consumer = host.Services.GetRequiredService<IConsumer<string, string>>(); |
| 58 | + consumer.Subscribe(topic); |
| 59 | + for (var i = 0; i < 10; i++) |
| 60 | + { |
| 61 | + var result = consumer.Consume(cts.Token); |
| 62 | + |
| 63 | + Assert.Equal($"test-key", result.Message.Key); |
| 64 | + Assert.Equal($"test-value{i}", result.Message.Value); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + [Theory] |
| 69 | + [ActiveIssue("https://github.com/dotnet/aspire/issues/4909")] |
| 70 | + [InlineData(true)] |
| 71 | + [InlineData(false)] |
| 72 | + [RequiresDocker] |
| 73 | + public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume) |
| 74 | + { |
| 75 | + var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); |
| 76 | + var topic = "test-topic"; |
| 77 | + string? volumeName = null; |
| 78 | + string? bindMountPath = null; |
| 79 | + |
| 80 | + try |
| 81 | + { |
| 82 | + var builder1 = CreateDistributedApplicationBuilder(); |
| 83 | + var kafka1 = builder1.AddKafka("kafka"); |
| 84 | + |
| 85 | + if (useVolume) |
| 86 | + { |
| 87 | + // Use a deterministic volume name to prevent them from exhausting the machines if deletion fails |
| 88 | + volumeName = VolumeNameGenerator.CreateVolumeName(kafka1, nameof(WithDataShouldPersistStateBetweenUsages)); |
| 89 | + |
| 90 | + // if the volume already exists (because of a crashing previous run), try to delete it |
| 91 | + DockerUtils.AttemptDeleteDockerVolume(volumeName); |
| 92 | + kafka1.WithDataVolume(volumeName); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + bindMountPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 97 | + kafka1.WithDataBindMount(bindMountPath); |
| 98 | + } |
| 99 | + |
| 100 | + using (var app = builder1.Build()) |
| 101 | + { |
| 102 | + await app.StartAsync(); |
| 103 | + try |
| 104 | + { |
| 105 | + var hb = Host.CreateApplicationBuilder(); |
| 106 | + |
| 107 | + hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> |
| 108 | + { |
| 109 | + [$"ConnectionStrings:{kafka1.Resource.Name}"] = await kafka1.Resource.ConnectionStringExpression.GetValueAsync(default) |
| 110 | + }); |
| 111 | + |
| 112 | + hb.AddKafkaProducer<string, string>("kafka"); |
| 113 | + |
| 114 | + using (var host = hb.Build()) |
| 115 | + { |
| 116 | + await host.StartAsync(); |
| 117 | + |
| 118 | + var producer = host.Services.GetRequiredService<IProducer<string, string>>(); |
| 119 | + for (var i = 0; i < 10; i++) |
| 120 | + { |
| 121 | + await producer.ProduceAsync(topic, new Message<string, string> { Key = "test-key", Value = $"test-value{i}" }); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + finally |
| 126 | + { |
| 127 | + // Stops the container, or the Volume/mount would still be in use |
| 128 | + await app.StopAsync(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + var builder2 = CreateDistributedApplicationBuilder(); |
| 133 | + var kafka2 = builder2.AddKafka("kafka"); |
| 134 | + |
| 135 | + if (useVolume) |
| 136 | + { |
| 137 | + kafka2.WithDataVolume(volumeName); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + kafka2.WithDataBindMount(bindMountPath!); |
| 142 | + } |
| 143 | + |
| 144 | + using (var app = builder2.Build()) |
| 145 | + { |
| 146 | + await app.StartAsync(); |
| 147 | + try |
| 148 | + { |
| 149 | + var hb = Host.CreateApplicationBuilder(); |
| 150 | + |
| 151 | + hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> |
| 152 | + { |
| 153 | + [$"ConnectionStrings:{kafka2.Resource.Name}"] = await kafka2.Resource.ConnectionStringExpression.GetValueAsync(default) |
| 154 | + }); |
| 155 | + |
| 156 | + hb.AddKafkaConsumer<string, string>("kafka", consumerBuilder => |
| 157 | + { |
| 158 | + consumerBuilder.Config.GroupId = "aspire-consumer-group"; |
| 159 | + consumerBuilder.Config.AutoOffsetReset = AutoOffsetReset.Earliest; |
| 160 | + }); |
| 161 | + |
| 162 | + using (var host = hb.Build()) |
| 163 | + { |
| 164 | + await host.StartAsync(); |
| 165 | + |
| 166 | + var consumer = host.Services.GetRequiredService<IConsumer<string, string>>(); |
| 167 | + consumer.Subscribe(topic); |
| 168 | + for (var i = 0; i < 10; i++) |
| 169 | + { |
| 170 | + var result = consumer.Consume(cts.Token); |
| 171 | + |
| 172 | + Assert.Equal($"test-key", result.Message.Key); |
| 173 | + Assert.Equal($"test-value{i}", result.Message.Value); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + finally |
| 178 | + { |
| 179 | + // Stops the container, or the Volume/mount would still be in use |
| 180 | + await app.StopAsync(); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + finally |
| 185 | + { |
| 186 | + if (volumeName is not null) |
| 187 | + { |
| 188 | + DockerUtils.AttemptDeleteDockerVolume(volumeName); |
| 189 | + } |
| 190 | + |
| 191 | + if (bindMountPath is not null) |
| 192 | + { |
| 193 | + try |
| 194 | + { |
| 195 | + File.Delete(bindMountPath); |
| 196 | + } |
| 197 | + catch |
| 198 | + { |
| 199 | + // Don't fail test if we can't clean the temporary folder |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private TestDistributedApplicationBuilder CreateDistributedApplicationBuilder() |
| 206 | + { |
| 207 | + var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry(); |
| 208 | + builder.Services.AddXunitLogging(testOutputHelper); |
| 209 | + return builder; |
| 210 | + } |
| 211 | +} |
0 commit comments