Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Increase overall stability #2548

Merged
merged 8 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ConnectionMultiplexer GetWorkingBacklogConn() =>
{
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