Skip to content

Commit

Permalink
Issue992: Fix test
Browse files Browse the repository at this point in the history
This won't be fully accurate until #1947 fixed the PING routing, but getting a test fix into main ahead of time.
  • Loading branch information
NickCraver committed Jan 18, 2022
1 parent 51272b3 commit fa9e1c3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
43 changes: 43 additions & 0 deletions src/StackExchange.Redis/BacklogPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace StackExchange.Redis
{
/// <summary>
/// The backlog policy to use for commands. This policy comes into effect when a connection is unhealthy or unavailable.
/// The policy can choose to backlog commands and wait to try them (within their timeout) against a connection when it comes up,
/// or it could choose to fail fast and throw ASAP. Different apps desire different behaviors with backpressure and how to handle
/// large amounts of load, so this is configurable to optimize the happy path but avoid spiral-of-death queue scenarios for others.
/// </summary>
public class BacklogPolicy
{
/// <summary>
/// Backlog behavior matching StackExchange.Redis's 2.x line, failing fast and not attempting to queue
/// and retry when a connection is available again.
/// </summary>
public static BacklogPolicy FailFast { get; } = new()
{
QueueWhileDisconnected = false,
AbortPendingOnConnectionFailure = true,
};

/// <summary>
/// Default backlog policy which will allow commands to be issues against an endpoint and queue up.
/// Commands are still subject to their async timeout (which serves as a queue size check).
/// </summary>
public static BacklogPolicy Default { get; } = new()
{
QueueWhileDisconnected = true,
AbortPendingOnConnectionFailure = false,
};

/// <summary>
/// Whether to queue commands while disconnected.
/// True means queue for attempts up until their timeout.
/// False means to fail ASAP and queue nothing.
/// </summary>
public bool QueueWhileDisconnected { get; init; }

/// <summary>
/// Whether to immediately abandon (with an exception) all pending commands when a connection goes unhealthy.
/// </summary>
public bool AbortPendingOnConnectionFailure { get; init; }
}
}
12 changes: 8 additions & 4 deletions tests/StackExchange.Redis.Tests/ConnectingFailDetection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,16 @@ public async Task Issue922_ReconnectRaised()
Assert.Equal(0, Volatile.Read(ref restoreCount));

var server = muxer.GetServer(TestConfig.Current.MasterServerAndPort);
server.SimulateConnectionFailure(SimulatedFailureType.InteractiveInbound | SimulatedFailureType.InteractiveOutbound);
server.SimulateConnectionFailure(SimulatedFailureType.All);

await UntilCondition(TimeSpan.FromSeconds(10), () => Volatile.Read(ref failCount) >= 2 && Volatile.Read(ref restoreCount) >= 2);

await UntilCondition(TimeSpan.FromSeconds(10), () => Volatile.Read(ref failCount) + Volatile.Read(ref restoreCount) == 4);
// interactive+subscriber = 2
Assert.Equal(2, Volatile.Read(ref failCount));
Assert.Equal(2, Volatile.Read(ref restoreCount));
var failCountSnapshot = Volatile.Read(ref failCount);
Assert.True(failCountSnapshot >= 2, $"failCount {failCountSnapshot} >= 2");

var restoreCountSnapshot = Volatile.Read(ref restoreCount);
Assert.True(restoreCountSnapshot >= 2, $"restoreCount ({restoreCountSnapshot}) >= 2");
}
}

Expand Down

0 comments on commit fa9e1c3

Please sign in to comment.