Skip to content

Commit c05179f

Browse files
authored
Tests: Increase overall stability (#2548)
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.
1 parent 8f7e040 commit c05179f

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

tests/StackExchange.Redis.Tests/AbortOnConnectFailTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using StackExchange.Redis.Tests.Helpers;
2+
using System;
23
using System.Threading.Tasks;
34
using Xunit;
45
using Xunit.Abstractions;
@@ -92,9 +93,9 @@ private ConnectionMultiplexer GetWorkingBacklogConn() =>
9293
{
9394
AbortOnConnectFail = false,
9495
BacklogPolicy = policy,
95-
ConnectTimeout = 50,
96+
ConnectTimeout = 500,
9697
SyncTimeout = 400,
9798
KeepAlive = 400,
9899
AllowAdmin = true,
99-
};
100+
}.WithoutSubscriptions();
100101
}

tests/StackExchange.Redis.Tests/Helpers/Extensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Runtime.InteropServices;
34
using Xunit.Abstractions;
45

@@ -26,4 +27,10 @@ static Extensions()
2627
}
2728

2829
public static void WriteFrameworkVersion(this ITestOutputHelper output) => output.WriteLine(VersionInfo);
30+
31+
public static ConfigurationOptions WithoutSubscriptions(this ConfigurationOptions options)
32+
{
33+
options.CommandMap = CommandMap.Create(new HashSet<string>() { nameof(RedisCommand.SUBSCRIBE) }, available: false);
34+
return options;
35+
}
2936
}

tests/StackExchange.Redis.Tests/RoleTests.cs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Xunit;
1+
using System.Linq;
2+
using Xunit;
23
using Xunit.Abstractions;
34

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

12+
protected override string GetConfiguration() => TestConfig.Current.PrimaryServerAndPort + "," + TestConfig.Current.ReplicaServerAndPort;
13+
1114
[Theory]
1215
[InlineData(true)]
1316
[InlineData(false)]
1417
public void PrimaryRole(bool allowAdmin) // should work with or without admin now
1518
{
1619
using var conn = Create(allowAdmin: allowAdmin);
17-
var server = conn.GetServer(TestConfig.Current.PrimaryServerAndPort);
18-
20+
var servers = conn.GetServers();
21+
Log("Server list:");
22+
foreach (var s in servers)
23+
{
24+
Log($" Server: {s.EndPoint} (isConnected: {s.IsConnected}, isReplica: {s.IsReplica})");
25+
}
26+
var server = servers.First(conn => !conn.IsReplica);
1927
var role = server.Role();
28+
Log($"Chosen primary: {server.EndPoint} (role: {role})");
29+
if (allowAdmin)
30+
{
31+
Log($"Info (Replication) dump for {server.EndPoint}:");
32+
Log(server.InfoRaw("Replication"));
33+
Log("");
34+
35+
foreach (var s in servers)
36+
{
37+
if (s.IsReplica)
38+
{
39+
Log($"Info (Replication) dump for {s.EndPoint}:");
40+
Log(s.InfoRaw("Replication"));
41+
Log("");
42+
}
43+
}
44+
}
2045
Assert.NotNull(role);
2146
Assert.Equal(role.Value, RedisLiterals.master);
2247
var primary = role as Role.Master;
2348
Assert.NotNull(primary);
2449
Assert.NotNull(primary.Replicas);
25-
Log($"Searching for: {TestConfig.Current.ReplicaServer}:{TestConfig.Current.ReplicaPort}");
26-
Log($"Replica count: {primary.Replicas.Count}");
27-
Assert.NotEmpty(primary.Replicas);
28-
foreach (var replica in primary.Replicas)
50+
51+
// Only do this check for Redis > 4 (to exclude Redis 3.x on Windows).
52+
// Unrelated to this test, the replica isn't connecting and we'll revisit swapping the server out.
53+
// TODO: MemuraiDeveloper check
54+
if (server.Version > RedisFeatures.v4_0_0)
2955
{
30-
Log($" Replica: {replica.Ip}:{replica.Port} (offset: {replica.ReplicationOffset})");
31-
Log(replica.ToString());
56+
Log($"Searching for: {TestConfig.Current.ReplicaServer}:{TestConfig.Current.ReplicaPort}");
57+
Log($"Replica count: {primary.Replicas.Count}");
58+
59+
Assert.NotEmpty(primary.Replicas);
60+
foreach (var replica in primary.Replicas)
61+
{
62+
Log($" Replica: {replica.Ip}:{replica.Port} (offset: {replica.ReplicationOffset})");
63+
Log(replica.ToString());
64+
}
65+
Assert.Contains(primary.Replicas, r =>
66+
r.Ip == TestConfig.Current.ReplicaServer &&
67+
r.Port == TestConfig.Current.ReplicaPort);
3268
}
33-
Assert.Contains(primary.Replicas, r =>
34-
r.Ip == TestConfig.Current.ReplicaServer &&
35-
r.Port == TestConfig.Current.ReplicaPort);
3669
}
3770

3871
[Fact]
3972
public void ReplicaRole()
4073
{
4174
using var conn = ConnectionMultiplexer.Connect($"{TestConfig.Current.ReplicaServerAndPort},allowAdmin=true");
42-
var server = conn.GetServer(TestConfig.Current.ReplicaServerAndPort);
75+
var server = conn.GetServers().First(conn => conn.IsReplica);
4376

4477
var role = server.Role();
4578
Assert.NotNull(role);

0 commit comments

Comments
 (0)