From 55e95329643e242d1efa2e81f05a59d54a254a26 Mon Sep 17 00:00:00 2001 From: Ziya Suzen Date: Wed, 10 Jan 2024 19:20:04 +0000 Subject: [PATCH] Format and test fixes (#317) --- sandbox/Example.Core.PublishModel/Program.cs | 3 ++- src/NATS.Client.Core/NatsConnection.cs | 8 ++++---- tests/NATS.Client.Core.Tests/CancellationTest.cs | 8 ++++++-- tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs | 4 ++-- tests/NATS.Client.Core.Tests/NatsConnectionTest.cs | 8 +++++++- tests/NATS.Client.KeyValueStore.Tests/GetKeysTest.cs | 2 +- .../ServicesSerializationTest.cs | 2 +- 7 files changed, 23 insertions(+), 12 deletions(-) diff --git a/sandbox/Example.Core.PublishModel/Program.cs b/sandbox/Example.Core.PublishModel/Program.cs index 663fcecce..d9ca83cbf 100644 --- a/sandbox/Example.Core.PublishModel/Program.cs +++ b/sandbox/Example.Core.PublishModel/Program.cs @@ -4,7 +4,8 @@ using NATS.Client.Serializers.Json; var subject = "bar.xyz"; -var options = NatsOpts.Default with { +var options = NatsOpts.Default with +{ LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()), SerializerRegistry = NatsJsonSerializerRegistry.Default, }; diff --git a/src/NATS.Client.Core/NatsConnection.cs b/src/NATS.Client.Core/NatsConnection.cs index d77334dc4..4eaecd915 100644 --- a/src/NATS.Client.Core/NatsConnection.cs +++ b/src/NATS.Client.Core/NatsConnection.cs @@ -150,8 +150,6 @@ public async ValueTask ConnectAsync() } } - internal NatsStats GetStats() => Counter.ToStats(); - public virtual async ValueTask DisposeAsync() { if (!IsDisposed) @@ -184,6 +182,8 @@ public virtual async ValueTask DisposeAsync() } } + internal NatsStats GetStats() => Counter.ToStats(); + internal void EnqueuePing(AsyncPingCommand pingCommand) { // Enqueue Ping Command to current working reader. @@ -538,7 +538,7 @@ private async void ReconnectLoop() _currentConnectUri = null; var urlEnumerator = urls.AsEnumerable().GetEnumerator(); NatsUri? url = null; - CONNECT_AGAIN: + CONNECT_AGAIN: try { if (urlEnumerator.MoveNext()) @@ -748,7 +748,7 @@ private void EnqueueCommandSync(ICommand command) private async ValueTask EnqueueCommandAsync(ICommand command) { - RETRY: + RETRY: if (_commandWriter.TryWrite(command)) { Interlocked.Increment(ref Counter.PendingMessages); diff --git a/tests/NATS.Client.Core.Tests/CancellationTest.cs b/tests/NATS.Client.Core.Tests/CancellationTest.cs index 36b862552..e6ee72ef3 100644 --- a/tests/NATS.Client.Core.Tests/CancellationTest.cs +++ b/tests/NATS.Client.Core.Tests/CancellationTest.cs @@ -19,16 +19,20 @@ public async Task CommandTimeoutTest() await using var subConnection = server.CreateClientConnection(NatsOpts.Default with { CommandTimeout = TimeSpan.FromSeconds(1) }); await using var pubConnection = server.CreateClientConnection(NatsOpts.Default with { CommandTimeout = TimeSpan.FromSeconds(1) }); + + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + var cancellationToken = cts.Token; + await pubConnection.ConnectAsync(); - await subConnection.SubscribeCoreAsync("foo"); + await subConnection.SubscribeCoreAsync("foo", cancellationToken: cancellationToken); var cmd = new SleepWriteCommand("PUB foo 5\r\naiueo", TimeSpan.FromSeconds(10)); pubConnection.PostDirectWrite(cmd); var timeoutException = await Assert.ThrowsAsync(async () => { - await pubConnection.PublishAsync("foo", "aiueo", opts: new NatsPubOpts { WaitUntilSent = true }); + await pubConnection.PublishAsync("foo", "aiueo", opts: new NatsPubOpts { WaitUntilSent = true }, cancellationToken: cancellationToken); }); timeoutException.Message.Should().Contain("1 seconds elapsing"); diff --git a/tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs b/tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs index eec70a25d..9065c8614 100644 --- a/tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs +++ b/tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs @@ -14,7 +14,7 @@ public async Task ConnectionsShouldBeNonDisposable() } // Assert - var con2 = (NatsConnection) pool.GetConnection(); + var con2 = (NatsConnection)pool.GetConnection(); con2.IsDisposed.Should().BeFalse(); } @@ -24,7 +24,7 @@ public async Task ConnectionsShouldBeDisposedWhenPoolIsDisposed() // Arrange NatsConnectionPool pool = new(1); - var con = (NatsConnection) pool.GetConnection(); + var con = (NatsConnection)pool.GetConnection(); // Act await pool.DisposeAsync(); diff --git a/tests/NATS.Client.Core.Tests/NatsConnectionTest.cs b/tests/NATS.Client.Core.Tests/NatsConnectionTest.cs index 971304d3d..528f8110e 100644 --- a/tests/NATS.Client.Core.Tests/NatsConnectionTest.cs +++ b/tests/NATS.Client.Core.Tests/NatsConnectionTest.cs @@ -2,6 +2,7 @@ using System.Reflection; using System.Text; using System.Text.Json.Serialization; +using System.Text.RegularExpressions; using System.Threading.Channels; using Xunit.Sdk; @@ -378,6 +379,11 @@ public void InterfaceShouldHaveSamePublicPropertiesEventsAndMethodAsClass() foreach (var classInfo in classMethods) { var name = classInfo.Name; + + // TODO: enable this check when we have events pulled up to the interface + if (Regex.IsMatch(name, @"add_|remove_")) + continue; + interfaceMethods.Select(m => m.Name).Should().Contain(name); } } @@ -432,7 +438,7 @@ public override bool Equals(object? obj) return false; } - return Equals((SampleClass) obj); + return Equals((SampleClass)obj); } public override int GetHashCode() diff --git a/tests/NATS.Client.KeyValueStore.Tests/GetKeysTest.cs b/tests/NATS.Client.KeyValueStore.Tests/GetKeysTest.cs index db4874f4e..b60799981 100644 --- a/tests/NATS.Client.KeyValueStore.Tests/GetKeysTest.cs +++ b/tests/NATS.Client.KeyValueStore.Tests/GetKeysTest.cs @@ -1,4 +1,4 @@ -using NATS.Client.Core.Tests; +using NATS.Client.Core.Tests; namespace NATS.Client.KeyValueStore.Tests; diff --git a/tests/NATS.Client.Services.Tests/ServicesSerializationTest.cs b/tests/NATS.Client.Services.Tests/ServicesSerializationTest.cs index 5143597bb..6844ab9c2 100644 --- a/tests/NATS.Client.Services.Tests/ServicesSerializationTest.cs +++ b/tests/NATS.Client.Services.Tests/ServicesSerializationTest.cs @@ -1,4 +1,4 @@ -using System.Buffers; +using System.Buffers; using NATS.Client.Core.Tests; using NATS.Client.Serializers.Json; using NATS.Client.Services.Internal;