Skip to content

Commit

Permalink
Tests: Increase overall stability (#2548)
Browse files Browse the repository at this point in the history
Fixes:
- `DisconnectAndNoReconnectThrowsConnectionExceptionAsync`: Under load, we're not connecting in 50ms for the initial thing, and stability > speed, so give a little on this test to help out. Average runtime will be higher, but that's way better than sporadically failing.
- `Roles`: Don't assume we're in the initial primary/replica setup (we may have failed over) and make it work either way.
  • Loading branch information
NickCraver committed Oct 31, 2023
1 parent 8f7e040 commit c05179f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
7 changes: 4 additions & 3 deletions tests/StackExchange.Redis.Tests/AbortOnConnectFailTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using StackExchange.Redis.Tests.Helpers;
using System;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -92,9 +93,9 @@ private ConfigurationOptions GetOptions(BacklogPolicy policy) => new Configurati
{
AbortOnConnectFail = false,
BacklogPolicy = policy,
ConnectTimeout = 50,
ConnectTimeout = 500,
SyncTimeout = 400,
KeepAlive = 400,
AllowAdmin = true,
};
}.WithoutSubscriptions();
}
7 changes: 7 additions & 0 deletions tests/StackExchange.Redis.Tests/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Xunit.Abstractions;

Expand Down Expand Up @@ -26,4 +27,10 @@ static Extensions()
}

public static void WriteFrameworkVersion(this ITestOutputHelper output) => output.WriteLine(VersionInfo);

public static ConfigurationOptions WithoutSubscriptions(this ConfigurationOptions options)
{
options.CommandMap = CommandMap.Create(new HashSet<string>() { nameof(RedisCommand.SUBSCRIBE) }, available: false);
return options;
}
}
59 changes: 46 additions & 13 deletions tests/StackExchange.Redis.Tests/RoleTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

namespace StackExchange.Redis.Tests;
Expand All @@ -8,38 +9,70 @@ public class Roles : TestBase
{
public Roles(ITestOutputHelper output, SharedConnectionFixture fixture) : base(output, fixture) { }

protected override string GetConfiguration() => TestConfig.Current.PrimaryServerAndPort + "," + TestConfig.Current.ReplicaServerAndPort;

[Theory]
[InlineData(true)]
[InlineData(false)]
public void PrimaryRole(bool allowAdmin) // should work with or without admin now
{
using var conn = Create(allowAdmin: allowAdmin);
var server = conn.GetServer(TestConfig.Current.PrimaryServerAndPort);

var servers = conn.GetServers();
Log("Server list:");
foreach (var s in servers)
{
Log($" Server: {s.EndPoint} (isConnected: {s.IsConnected}, isReplica: {s.IsReplica})");
}
var server = servers.First(conn => !conn.IsReplica);
var role = server.Role();
Log($"Chosen primary: {server.EndPoint} (role: {role})");
if (allowAdmin)
{
Log($"Info (Replication) dump for {server.EndPoint}:");
Log(server.InfoRaw("Replication"));
Log("");

foreach (var s in servers)
{
if (s.IsReplica)
{
Log($"Info (Replication) dump for {s.EndPoint}:");
Log(s.InfoRaw("Replication"));
Log("");
}
}
}
Assert.NotNull(role);
Assert.Equal(role.Value, RedisLiterals.master);
var primary = role as Role.Master;
Assert.NotNull(primary);
Assert.NotNull(primary.Replicas);
Log($"Searching for: {TestConfig.Current.ReplicaServer}:{TestConfig.Current.ReplicaPort}");
Log($"Replica count: {primary.Replicas.Count}");
Assert.NotEmpty(primary.Replicas);
foreach (var replica in primary.Replicas)

// Only do this check for Redis > 4 (to exclude Redis 3.x on Windows).
// Unrelated to this test, the replica isn't connecting and we'll revisit swapping the server out.
// TODO: MemuraiDeveloper check
if (server.Version > RedisFeatures.v4_0_0)
{
Log($" Replica: {replica.Ip}:{replica.Port} (offset: {replica.ReplicationOffset})");
Log(replica.ToString());
Log($"Searching for: {TestConfig.Current.ReplicaServer}:{TestConfig.Current.ReplicaPort}");
Log($"Replica count: {primary.Replicas.Count}");

Assert.NotEmpty(primary.Replicas);
foreach (var replica in primary.Replicas)
{
Log($" Replica: {replica.Ip}:{replica.Port} (offset: {replica.ReplicationOffset})");
Log(replica.ToString());
}
Assert.Contains(primary.Replicas, r =>
r.Ip == TestConfig.Current.ReplicaServer &&
r.Port == TestConfig.Current.ReplicaPort);
}
Assert.Contains(primary.Replicas, r =>
r.Ip == TestConfig.Current.ReplicaServer &&
r.Port == TestConfig.Current.ReplicaPort);
}

[Fact]
public void ReplicaRole()
{
using var conn = ConnectionMultiplexer.Connect($"{TestConfig.Current.ReplicaServerAndPort},allowAdmin=true");
var server = conn.GetServer(TestConfig.Current.ReplicaServerAndPort);
var server = conn.GetServers().First(conn => conn.IsReplica);

var role = server.Role();
Assert.NotNull(role);
Expand Down

0 comments on commit c05179f

Please sign in to comment.