Skip to content

Commit

Permalink
Add test for ReconnectAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
pzajaczkowski committed Nov 28, 2024
1 parent 353b93d commit 9446d22
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/NATS.Client.Core.Tests/NatsConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,45 @@ public async Task OnSocketAvailableAsync_ShouldBeInvokedOnReconnection()
// Assert
Assert.Equal(2, invocationCount);
}

[Fact]
public async Task ReconnectOnOpenConnection_ShouldDisconnectAndOpenNewConnection()
{
// Arrange
await using var server = NatsServer.Start(_output, _transportType);
await using var connection = server.CreateClientConnection();
await connection.ConnectAsync(); // wait first connection open

var openedCount = 0;
var disconnectedCount = 0;

var openSignal = new WaitSignal();
var disconnectSignal = new WaitSignal();

connection.ConnectionOpened += (_, _) =>
{
Interlocked.Increment(ref openedCount);
openSignal.Pulse();
return default;
};
connection.ConnectionDisconnected += (_, _) =>
{
Interlocked.Increment(ref disconnectedCount);
disconnectSignal.Pulse();
return default;
};

// Act
await connection.ReconnectAsync();
await disconnectSignal;
await openSignal;

// Assert
// First connection is not taken into account, so one invocation of
// disconnected event and open connection event are expected
openedCount.ShouldBe(1);
disconnectedCount.ShouldBe(1);
}
}

[JsonSerializable(typeof(SampleClass))]
Expand Down

0 comments on commit 9446d22

Please sign in to comment.