|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace StackExchange.Redis.Tests; |
| 7 | + |
| 8 | +public abstract class GetServerTestsBase(ITestOutputHelper output, SharedConnectionFixture fixture) |
| 9 | + : TestBase(output, fixture) |
| 10 | +{ |
| 11 | + protected abstract bool IsCluster { get; } |
| 12 | + |
| 13 | + [Fact] |
| 14 | + public async Task GetServersMemoization() |
| 15 | + { |
| 16 | + await using var conn = Create(); |
| 17 | + |
| 18 | + var servers0 = conn.GetServers(); |
| 19 | + var servers1 = conn.GetServers(); |
| 20 | + |
| 21 | + // different array, exact same contents |
| 22 | + Assert.NotSame(servers0, servers1); |
| 23 | + Assert.NotEmpty(servers0); |
| 24 | + Assert.NotNull(servers0); |
| 25 | + Assert.NotNull(servers1); |
| 26 | + Assert.Equal(servers0.Length, servers1.Length); |
| 27 | + for (int i = 0; i < servers0.Length; i++) |
| 28 | + { |
| 29 | + Assert.Same(servers0[i], servers1[i]); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task GetServerByEndpointMemoization() |
| 35 | + { |
| 36 | + await using var conn = Create(); |
| 37 | + var ep = conn.GetEndPoints().First(); |
| 38 | + |
| 39 | + IServer x = conn.GetServer(ep), y = conn.GetServer(ep); |
| 40 | + Assert.Same(x, y); |
| 41 | + |
| 42 | + object asyncState = "whatever"; |
| 43 | + x = conn.GetServer(ep, asyncState); |
| 44 | + y = conn.GetServer(ep, asyncState); |
| 45 | + Assert.NotSame(x, y); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public async Task GetServerByKeyMemoization() |
| 50 | + { |
| 51 | + await using var conn = Create(); |
| 52 | + RedisKey key = Me(); |
| 53 | + string value = $"{key}:value"; |
| 54 | + await conn.GetDatabase().StringSetAsync(key, value); |
| 55 | + |
| 56 | + IServer x = conn.GetServer(key), y = conn.GetServer(key); |
| 57 | + Assert.False(y.IsReplica, "IsReplica"); |
| 58 | + Assert.Same(x, y); |
| 59 | + |
| 60 | + y = conn.GetServer(key, flags: CommandFlags.DemandMaster); |
| 61 | + Assert.Same(x, y); |
| 62 | + |
| 63 | + // async state demands separate instance |
| 64 | + y = conn.GetServer(key, "async state", flags: CommandFlags.DemandMaster); |
| 65 | + Assert.NotSame(x, y); |
| 66 | + |
| 67 | + // primary and replica should be different |
| 68 | + y = conn.GetServer(key, flags: CommandFlags.DemandReplica); |
| 69 | + Assert.NotSame(x, y); |
| 70 | + Assert.True(y.IsReplica, "IsReplica"); |
| 71 | + |
| 72 | + // replica again: same |
| 73 | + var z = conn.GetServer(key, flags: CommandFlags.DemandReplica); |
| 74 | + Assert.Same(y, z); |
| 75 | + |
| 76 | + // check routed correctly |
| 77 | + var actual = (string?)await x.ExecuteAsync(null, "get", [key], CommandFlags.NoRedirect); |
| 78 | + Assert.Equal(value, actual); // check value against primary |
| 79 | + |
| 80 | + // for replica, don't check the value, because of replication delay - just: no error |
| 81 | + _ = y.ExecuteAsync(null, "get", [key], CommandFlags.NoRedirect); |
| 82 | + } |
| 83 | + |
| 84 | + [Theory] |
| 85 | + [InlineData(true)] |
| 86 | + [InlineData(false)] |
| 87 | + public async Task GetServerWithDefaultKey(bool explicitNull) |
| 88 | + { |
| 89 | + await using var conn = Create(); |
| 90 | + bool isCluster = conn.ServerSelectionStrategy.ServerType == ServerType.Cluster; |
| 91 | + Assert.Equal(IsCluster, isCluster); // check our assumptions! |
| 92 | + |
| 93 | + // we expect explicit null and default to act the same, but: check |
| 94 | + RedisKey key = explicitNull ? RedisKey.Null : default(RedisKey); |
| 95 | + |
| 96 | + IServer primary = conn.GetServer(key); |
| 97 | + Assert.False(primary.IsReplica); |
| 98 | + |
| 99 | + IServer replica = conn.GetServer(key, flags: CommandFlags.DemandReplica); |
| 100 | + Assert.True(replica.IsReplica); |
| 101 | + |
| 102 | + // check multiple calls |
| 103 | + HashSet<IServer> uniques = []; |
| 104 | + for (int i = 0; i < 100; i++) |
| 105 | + { |
| 106 | + uniques.Add(conn.GetServer(key)); |
| 107 | + } |
| 108 | + |
| 109 | + if (isCluster) |
| 110 | + { |
| 111 | + Assert.True(uniques.Count > 1); // should be able to get arbitrary servers |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + Assert.Single(uniques); |
| 116 | + } |
| 117 | + |
| 118 | + uniques.Clear(); |
| 119 | + for (int i = 0; i < 100; i++) |
| 120 | + { |
| 121 | + uniques.Add(conn.GetServer(key, flags: CommandFlags.DemandReplica)); |
| 122 | + } |
| 123 | + |
| 124 | + if (isCluster) |
| 125 | + { |
| 126 | + Assert.True(uniques.Count > 1); // should be able to get arbitrary servers |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + Assert.Single(uniques); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +[RunPerProtocol] |
| 136 | +public class GetServerTestsCluster(ITestOutputHelper output, SharedConnectionFixture fixture) : GetServerTestsBase(output, fixture) |
| 137 | +{ |
| 138 | + protected override string GetConfiguration() => TestConfig.Current.ClusterServersAndPorts; |
| 139 | + |
| 140 | + protected override bool IsCluster => true; |
| 141 | +} |
| 142 | + |
| 143 | +[RunPerProtocol] |
| 144 | +public class GetServerTestsStandalone(ITestOutputHelper output, SharedConnectionFixture fixture) : GetServerTestsBase(output, fixture) |
| 145 | +{ |
| 146 | + protected override string GetConfiguration() => // we want to test flags usage including replicas |
| 147 | + TestConfig.Current.PrimaryServerAndPort + "," + TestConfig.Current.ReplicaServerAndPort; |
| 148 | + |
| 149 | + protected override bool IsCluster => false; |
| 150 | +} |
0 commit comments