Skip to content

Commit

Permalink
Add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
johannespfeiffer committed Oct 24, 2024
1 parent f2545b2 commit 2fa95e6
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/NATS.Client.Core.Tests/NatsConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,60 @@ public void NewInboxVeryLongPrefixReturnsPrefixWithNuid()

Assert.Matches("A{512}\\.[A-z0-9]{22}", inbox);
}

[Fact]
public async Task OnSocketAvailableAsync_ShouldBeInvokedOnInitialConnection()
{
// Arrange
await using var server = NatsServer.Start();
var clientOpts = server.ClientOpts(NatsOpts.Default);

var wasInvoked = false;
var nats = new NatsConnection(clientOpts);
nats.OnSocketAvailableAsync = async socket =>
{
wasInvoked = true;
await Task.Delay(10);
return socket;
};

// Act
await nats.ConnectAsync();

// Assert
Assert.True(wasInvoked, "OnSocketAvailableAsync should be invoked on initial connection.");
}

[Fact]
public async Task OnSocketAvailableAsync_ShouldBeInvokedOnReconnection()
{
// Arrange
await using var server = NatsServer.Start();
var clientOpts = server.ClientOpts(NatsOpts.Default);

var invocationCount = 0;
var nats = new NatsConnection(clientOpts);
nats.OnSocketAvailableAsync = async socket =>
{
invocationCount++;
await Task.Delay(10);
return socket;
};

// Simulate initial connection
await nats.ConnectAsync();

// Simulate disconnection
await server.StopAsync();

// Act
// Simulate reconnection
server.StartServerProcess();
await nats.ConnectAsync();

// Assert
Assert.Equal(2, invocationCount);
}
}

[JsonSerializable(typeof(SampleClass))]
Expand Down Expand Up @@ -476,3 +530,4 @@ public override string ToString()
return $"{Id}-{Name}";
}
}

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / check

File is required to end with a single newline character

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / check

File is required to end with a single newline character

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (v2.9)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (v2.9)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (v2.9)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (v2.9)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (v2.9)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (v2.9)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (v2.9)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (v2.9)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (latest)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (latest)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (latest)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (latest)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (latest)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (latest)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (latest)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (latest)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (main)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (main)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (main)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Linux (main)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (main)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (main)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (main)

Check warning on line 532 in tests/NATS.Client.Core.Tests/NatsConnectionTest.cs

View workflow job for this annotation

GitHub Actions / Windows (main)


0 comments on commit 2fa95e6

Please sign in to comment.