|
| 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 Xunit; |
| 7 | +using Xunit.Abstractions; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using Polly; |
| 10 | +using Microsoft.Extensions.Hosting; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using NATS.Client.Core; |
| 13 | +using NATS.Client.JetStream; |
| 14 | +using NATS.Client.JetStream.Models; |
| 15 | +namespace Aspire.Hosting.Nats.Tests; |
| 16 | + |
| 17 | +public class NatsFunctionalTests(ITestOutputHelper testOutputHelper) |
| 18 | +{ |
| 19 | + private const string StreamName = "test-stream"; |
| 20 | + private const string SubjectName = "test-subject"; |
| 21 | + |
| 22 | + [Fact] |
| 23 | + [RequiresDocker] |
| 24 | + public async Task VerifyNatsResource() |
| 25 | + { |
| 26 | + var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5)); |
| 27 | + var pipeline = new ResiliencePipelineBuilder() |
| 28 | + .AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(1), ShouldHandle = new PredicateBuilder().Handle<NatsException>() }) |
| 29 | + .Build(); |
| 30 | + |
| 31 | + var builder = CreateDistributedApplicationBuilder(); |
| 32 | + |
| 33 | + var nats = builder.AddNats("nats") |
| 34 | + .WithJetStream(); |
| 35 | + |
| 36 | + using var app = builder.Build(); |
| 37 | + |
| 38 | + await app.StartAsync(); |
| 39 | + |
| 40 | + var hb = Host.CreateApplicationBuilder(); |
| 41 | + |
| 42 | + hb.Configuration[$"ConnectionStrings:{nats.Resource.Name}"] = await nats.Resource.ConnectionStringExpression.GetValueAsync(default); |
| 43 | + |
| 44 | + hb.AddNatsClient("nats", configureOptions: opts => |
| 45 | + { |
| 46 | + var jsonRegistry = new NatsJsonContextSerializerRegistry(AppJsonContext.Default); |
| 47 | + return opts with { SerializerRegistry = jsonRegistry }; |
| 48 | + }); |
| 49 | + |
| 50 | + hb.AddNatsJetStream(); |
| 51 | + |
| 52 | + using var host = hb.Build(); |
| 53 | + |
| 54 | + await host.StartAsync(); |
| 55 | + |
| 56 | + await pipeline.ExecuteAsync(async token => |
| 57 | + { |
| 58 | + var jetStream = host.Services.GetRequiredService<INatsJSContext>(); |
| 59 | + |
| 60 | + await CreateTestData(jetStream, token); |
| 61 | + await ConsumeTestData(jetStream, token); |
| 62 | + }, cts.Token); |
| 63 | + } |
| 64 | + |
| 65 | + [Theory] |
| 66 | + [InlineData(true)] |
| 67 | + [InlineData(false)] |
| 68 | + [RequiresDocker] |
| 69 | + public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume) |
| 70 | + { |
| 71 | + var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); |
| 72 | + var pipeline = new ResiliencePipelineBuilder() |
| 73 | + .AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(1), ShouldHandle = new PredicateBuilder().Handle<NatsException>() }) |
| 74 | + .Build(); |
| 75 | + string? volumeName = null; |
| 76 | + string? bindMountPath = null; |
| 77 | + |
| 78 | + try |
| 79 | + { |
| 80 | + var builder1 = CreateDistributedApplicationBuilder(); |
| 81 | + var nats1 = builder1.AddNats("nats") |
| 82 | + .WithJetStream(); |
| 83 | + |
| 84 | + if (useVolume) |
| 85 | + { |
| 86 | + // Use a deterministic volume name to prevent them from exhausting the machines if deletion fails |
| 87 | + volumeName = VolumeNameGenerator.CreateVolumeName(nats1, nameof(WithDataShouldPersistStateBetweenUsages)); |
| 88 | + |
| 89 | + // if the volume already exists (because of a crashing previous run), try to delete it |
| 90 | + DockerUtils.AttemptDeleteDockerVolume(volumeName); |
| 91 | + nats1.WithDataVolume(volumeName); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + bindMountPath = Directory.CreateTempSubdirectory().FullName; |
| 96 | + nats1.WithDataBindMount(bindMountPath); |
| 97 | + } |
| 98 | + |
| 99 | + using (var app = builder1.Build()) |
| 100 | + { |
| 101 | + await app.StartAsync(); |
| 102 | + try |
| 103 | + { |
| 104 | + var hb = Host.CreateApplicationBuilder(); |
| 105 | + |
| 106 | + hb.Configuration[$"ConnectionStrings:{nats1.Resource.Name}"] = await nats1.Resource.ConnectionStringExpression.GetValueAsync(default); |
| 107 | + |
| 108 | + hb.AddNatsClient("nats", configureOptions: opts => |
| 109 | + { |
| 110 | + var jsonRegistry = new NatsJsonContextSerializerRegistry(AppJsonContext.Default); |
| 111 | + return opts with { SerializerRegistry = jsonRegistry }; |
| 112 | + }); |
| 113 | + |
| 114 | + hb.AddNatsJetStream(); |
| 115 | + |
| 116 | + using (var host = hb.Build()) |
| 117 | + { |
| 118 | + await host.StartAsync(); |
| 119 | + |
| 120 | + await pipeline.ExecuteAsync(async token => |
| 121 | + { |
| 122 | + var jetStream = host.Services.GetRequiredService<INatsJSContext>(); |
| 123 | + await CreateTestData(jetStream, token); |
| 124 | + await ConsumeTestData(jetStream, token); |
| 125 | + |
| 126 | + }, cts.Token); |
| 127 | + } |
| 128 | + } |
| 129 | + finally |
| 130 | + { |
| 131 | + // Stops the container, or the Volume/mount would still be in use |
| 132 | + await app.StopAsync(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + var builder2 = CreateDistributedApplicationBuilder(); |
| 137 | + var nats2 = builder2.AddNats("nats") |
| 138 | + .WithJetStream(); |
| 139 | + |
| 140 | + if (useVolume) |
| 141 | + { |
| 142 | + nats2.WithDataVolume(volumeName); |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + nats2.WithDataBindMount(bindMountPath!); |
| 147 | + } |
| 148 | + |
| 149 | + using (var app = builder2.Build()) |
| 150 | + { |
| 151 | + await app.StartAsync(); |
| 152 | + try |
| 153 | + { |
| 154 | + var hb = Host.CreateApplicationBuilder(); |
| 155 | + |
| 156 | + hb.Configuration[$"ConnectionStrings:{nats2.Resource.Name}"] = await nats2.Resource.ConnectionStringExpression.GetValueAsync(default); |
| 157 | + hb.AddNatsClient("nats", configureOptions: opts => |
| 158 | + { |
| 159 | + var jsonRegistry = new NatsJsonContextSerializerRegistry(AppJsonContext.Default); |
| 160 | + return opts with { SerializerRegistry = jsonRegistry }; |
| 161 | + }); |
| 162 | + |
| 163 | + hb.AddNatsJetStream(); |
| 164 | + |
| 165 | + using (var host = hb.Build()) |
| 166 | + { |
| 167 | + await host.StartAsync(); |
| 168 | + |
| 169 | + await pipeline.ExecuteAsync(async token => |
| 170 | + { |
| 171 | + var jetStream = host.Services.GetRequiredService<INatsJSContext>(); |
| 172 | + await ConsumeTestData(jetStream, token); |
| 173 | + }); |
| 174 | + } |
| 175 | + } |
| 176 | + finally |
| 177 | + { |
| 178 | + // Stops the container, or the Volume/mount would still be in use |
| 179 | + await app.StopAsync(); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + finally |
| 184 | + { |
| 185 | + if (volumeName is not null) |
| 186 | + { |
| 187 | + DockerUtils.AttemptDeleteDockerVolume(volumeName); |
| 188 | + } |
| 189 | + |
| 190 | + if (bindMountPath is not null) |
| 191 | + { |
| 192 | + try |
| 193 | + { |
| 194 | + Directory.Delete(bindMountPath, recursive: true); |
| 195 | + } |
| 196 | + catch |
| 197 | + { |
| 198 | + // Don't fail test if we can't clean the temporary folder |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + private static async Task ConsumeTestData(INatsJSContext jetStream, CancellationToken token) |
| 205 | + { |
| 206 | + var stream = await jetStream.GetStreamAsync(StreamName, cancellationToken: token); |
| 207 | + var consumer = await stream.CreateOrderedConsumerAsync(cancellationToken: token); |
| 208 | + |
| 209 | + var events = new List<AppEvent>(); |
| 210 | + await foreach (var msg in consumer.ConsumeAsync<AppEvent>(cancellationToken: token)) |
| 211 | + { |
| 212 | + events.Add(msg.Data!); |
| 213 | + await msg.AckAsync(cancellationToken: token); |
| 214 | + if (msg.Metadata?.NumPending == 0) |
| 215 | + { |
| 216 | + break; |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + for (var i = 0; i < 10; i++) |
| 221 | + { |
| 222 | + var @event = events[i]; |
| 223 | + Assert.Equal($"test-event-{i}", @event.Name); |
| 224 | + Assert.Equal($"test-event-description-{i}", @event.Description); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + private static async Task CreateTestData(INatsJSContext jetStream, CancellationToken token) |
| 229 | + { |
| 230 | + var stream = await jetStream.CreateStreamAsync(new StreamConfig(StreamName, [SubjectName]), cancellationToken: token); |
| 231 | + Assert.Equal(StreamName, stream.Info.Config.Name); |
| 232 | + |
| 233 | + for (var i = 0; i < 10; i++) |
| 234 | + { |
| 235 | + var appEvent = new AppEvent(SubjectName, $"test-event-{i}", $"test-event-description-{i}", i); |
| 236 | + var ack = await jetStream.PublishAsync(appEvent.Subject, appEvent, cancellationToken: token); |
| 237 | + ack.EnsureSuccess(); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + private TestDistributedApplicationBuilder CreateDistributedApplicationBuilder() |
| 242 | + { |
| 243 | + var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry(); |
| 244 | + builder.Services.AddXunitLogging(testOutputHelper); |
| 245 | + return builder; |
| 246 | + } |
| 247 | +} |
0 commit comments