-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Reconnect backoff To help minimize the impact on the servers during failures. * Exponential backoff * Backoff negative check
- Loading branch information
Showing
3 changed files
with
179 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace NATS.Client.Core.Tests; | ||
|
||
public class ConnectionRetryTest | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public ConnectionRetryTest(ITestOutputHelper output) => _output = output; | ||
|
||
[Fact] | ||
public async Task Max_retry_reached_after_disconnect() | ||
{ | ||
await using var server = NatsServer.Start(); | ||
await using var nats = server.CreateClientConnection(new NatsOpts | ||
{ | ||
MaxReconnectRetry = 2, | ||
ReconnectWaitMax = TimeSpan.Zero, | ||
ReconnectWaitMin = TimeSpan.FromSeconds(.1), | ||
}); | ||
|
||
var signal = new WaitSignal(); | ||
nats.ReconnectFailed += (_, e) => signal.Pulse(); | ||
|
||
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); | ||
|
||
await server.StopAsync(); | ||
|
||
await signal; | ||
var exception = await Assert.ThrowsAsync<NatsException>(async () => await nats.PingAsync(cts.Token)); | ||
Assert.Equal("Max connect retry exceeded.", exception.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task Retry_and_connect_after_disconnected() | ||
{ | ||
await using var server = NatsServer.Start(); | ||
await using var nats = server.CreateClientConnection(new NatsOpts | ||
{ | ||
MaxReconnectRetry = 10, | ||
ReconnectWaitMax = TimeSpan.Zero, | ||
ReconnectWaitMin = TimeSpan.FromSeconds(2), | ||
}); | ||
|
||
var signal = new WaitSignal(); | ||
nats.ReconnectFailed += (_, e) => signal.Pulse(); | ||
|
||
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); | ||
|
||
await server.StopAsync(); | ||
|
||
await signal; | ||
|
||
await Task.Delay(TimeSpan.FromSeconds(5), cts.Token); | ||
|
||
server.StartServerProcess(); | ||
|
||
var rtt = await nats.PingAsync(cts.Token); | ||
Assert.True(rtt > TimeSpan.Zero); | ||
} | ||
} |