Skip to content

Commit

Permalink
Add NATS server fixture and test improvements
Browse files Browse the repository at this point in the history
Introduced a base NATS server fixture for test classes and updated test cases to utilize the fixture. This simplifies server management and ensures consistent URL handling. Also, enhanced tests to use unique prefixes for identifiers to improve reliability.
  • Loading branch information
mtmk committed Sep 26, 2024
1 parent 8ef9c55 commit f9a2960
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 32 deletions.
15 changes: 15 additions & 0 deletions tests/NATS.Client.Platform.Windows.Tests/BaseNatsServerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NATS.Client.Platform.Windows.Tests;

public class BaseNatsServerFixture : IDisposable
{
private readonly NatsServerProcess _server;
private int _next;

protected BaseNatsServerFixture(string? config = default) => _server = NatsServerProcess.Start(config: config);

public string Url => _server.Url;

public string GetNextId() => $"test{Interlocked.Increment(ref _next)}";

public void Dispose() => _server.Dispose();
}
56 changes: 33 additions & 23 deletions tests/NATS.Client.Platform.Windows.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,49 @@

namespace NATS.Client.Platform.Windows.Tests;

public class BasicTests
public class BasicTests : IClassFixture<BasicTestsNatsServerFixture>
{
private readonly ITestOutputHelper _output;
private readonly BasicTestsNatsServerFixture _server;

public BasicTests(ITestOutputHelper output) => _output = output;
public BasicTests(ITestOutputHelper output, BasicTestsNatsServerFixture server)
{
_output = output;
_server = server;
}

[Fact]
public async Task Core()
{
await using var server = await NatsServerProcess.StartAsync();
await using var nats = new NatsConnection(new NatsOpts { Url = server.Url, SerializerRegistry = NatsJsonSerializerRegistry.Default });
await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url, SerializerRegistry = NatsJsonSerializerRegistry.Default });
var prefix = _server.GetNextId();

await nats.PingAsync();

await using var sub = await nats.SubscribeCoreAsync<TestData>("foo");
await using var sub = await nats.SubscribeCoreAsync<TestData>($"{prefix}.foo");
for (var i = 0; i < 16; i++)
{
await nats.PublishAsync("foo", new TestData { Id = i });
await nats.PublishAsync($"{prefix}.foo", new TestData { Id = i });
Assert.Equal(i, (await sub.Msgs.ReadAsync()).Data!.Id);
}
}

[Fact]
public async Task JetStream()
{
await using var server = await NatsServerProcess.StartAsync();
await using var nats = new NatsConnection(new NatsOpts { Url = server.Url });
await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url });
var prefix = _server.GetNextId();
var js = new NatsJSContext(nats);

var stream = await js.CreateStreamAsync(new StreamConfig("s1", ["s1.>"]));
var stream = await js.CreateStreamAsync(new StreamConfig($"{prefix}s1", [$"{prefix}s1.>"]));

for (var i = 0; i < 16; i++)
{
var ack = await js.PublishAsync("s1.foo", $"bar{i}");
var ack = await js.PublishAsync($"{prefix}s1.foo", $"bar{i}");
ack.EnsureSuccess();
}

var consumer = await stream.CreateOrUpdateConsumerAsync(new ConsumerConfig("c1"));
var consumer = await stream.CreateOrUpdateConsumerAsync(new ConsumerConfig($"{prefix}c1"));

var count = 0;
await foreach (var msg in consumer.ConsumeAsync<string>())
Expand All @@ -59,7 +64,7 @@ public async Task JetStream()
}
}

var orderedConsumer = await js.CreateOrderedConsumerAsync("s1");
var orderedConsumer = await js.CreateOrderedConsumerAsync($"{prefix}s1");
count = 0;
await foreach (var msg in orderedConsumer.ConsumeAsync<string>())
{
Expand All @@ -74,12 +79,13 @@ public async Task JetStream()
[Fact]
public async Task KV()
{
await using var server = await NatsServerProcess.StartAsync();
await using var nats = new NatsConnection(new NatsOpts { Url = server.Url });
await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url });
var prefix = _server.GetNextId();

var js = new NatsJSContext(nats);
var kv = new NatsKVContext(js);

var store = await kv.CreateStoreAsync("b1");
var store = await kv.CreateStoreAsync($"{prefix}b1");

for (var i = 0; i < 16; i++)
{
Expand All @@ -96,12 +102,13 @@ public async Task KV()
[Fact]
public async Task ObjectStore()
{
await using var server = await NatsServerProcess.StartAsync();
await using var nats = new NatsConnection(new NatsOpts { Url = server.Url });
await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url });
var prefix = _server.GetNextId();

var js = new NatsJSContext(nats);
var obj = new NatsObjContext(js);

var store = await obj.CreateObjectStoreAsync("b1");
var store = await obj.CreateObjectStoreAsync($"{prefix}b1");

for (var i = 0; i < 16; i++)
{
Expand All @@ -118,22 +125,23 @@ public async Task ObjectStore()
[Fact]
public async Task Services()
{
await using var server = await NatsServerProcess.StartAsync();
await using var nats = new NatsConnection(new NatsOpts { Url = server.Url });
await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url });
var prefix = _server.GetNextId();

var svc = new NatsSvcContext(nats);

var s1 = await svc.AddServiceAsync("s1", "1.0.0");
var s1 = await svc.AddServiceAsync($"{prefix}s1", "1.0.0");

await s1.AddEndpointAsync<int>(
async msg =>
{
await msg.ReplyAsync(msg.Data * 2);
},
"multiply");
$"{prefix}.multiply");

for (var i = 0; i < 16; i++)
{
var reply = await nats.RequestAsync<int, int>("multiply", i);
var reply = await nats.RequestAsync<int, int>($"{prefix}.multiply", i);
Assert.Equal(i * 2, reply.Data);
}
}
Expand All @@ -143,3 +151,5 @@ private class TestData
public int Id { get; set; }
}
}

public class BasicTestsNatsServerFixture : BaseNatsServerFixture;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net481</TargetFramework>
<TargetFrameworks>net6.0;net8.0;net481</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);CS8002;VSTHRD200;VSTHRD111</NoWarn>
Expand Down
22 changes: 14 additions & 8 deletions tests/NATS.Client.Platform.Windows.Tests/TlsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@

namespace NATS.Client.Platform.Windows.Tests;

public class TlsTests
public class TlsTests : IClassFixture<TlsTestsNatsServerFixture>
{
private readonly ITestOutputHelper _output;
private readonly TlsTestsNatsServerFixture _server;

public TlsTests(ITestOutputHelper output) => _output = output;
public TlsTests(ITestOutputHelper output, TlsTestsNatsServerFixture server)
{
_output = output;
_server = server;
}

[Fact]
public async Task Tls_fails_without_certificates()
{
await using var server = await NatsServerProcess.StartAsync(config: "resources/configs/tls.conf");
await using var nats = new NatsConnection(new NatsOpts { Url = server.Url });
await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url });

var exception = await Assert.ThrowsAsync<NatsException>(async () => await nats.ConnectAsync());
Assert.Contains("TLS authentication failed", exception.InnerException?.Message);
Expand All @@ -26,23 +30,25 @@ public async Task Tls_with_certificates()
{
const string caCertFile = "resources/certs/ca-cert.pem";
const string clientCertBundleFile = "resources/certs/client-cert-bundle.pfx";
var prefix = _server.GetNextId();

var tlsOpts = new NatsTlsOpts
{
CaFile = caCertFile,
CertBundleFile = clientCertBundleFile,
};

await using var server = await NatsServerProcess.StartAsync(config: "resources/configs/tls.conf");
await using var nats = new NatsConnection(new NatsOpts { Url = server.Url, TlsOpts = tlsOpts });
await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url, TlsOpts = tlsOpts });

await nats.PingAsync();

await using var sub = await nats.SubscribeCoreAsync<int>("foo");
await using var sub = await nats.SubscribeCoreAsync<int>($"{prefix}.foo");
for (var i = 0; i < 64; i++)
{
await nats.PublishAsync("foo", i);
await nats.PublishAsync($"{prefix}.foo", i);
Assert.Equal(i, (await sub.Msgs.ReadAsync()).Data);
}
}
}

public class TlsTestsNatsServerFixture() : BaseNatsServerFixture("resources/configs/tls.conf");

0 comments on commit f9a2960

Please sign in to comment.