Skip to content

Commit ce086d9

Browse files
committed
more bad merge
1 parent e19f401 commit ce086d9

File tree

4 files changed

+175
-3
lines changed

4 files changed

+175
-3
lines changed

tests/RedisConfigs/.docker/Redis/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM redis:7.4.2
1+
FROM redis:8.2.0
22

33
COPY --from=configs ./Basic /data/Basic/
44
COPY --from=configs ./Failover /data/Failover/
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
}

tests/StackExchange.Redis.Tests/KeyTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ public async Task KeyEncoding()
182182
db.KeyDelete(key, CommandFlags.FireAndForget);
183183
db.StringSet(key, "new value", flags: CommandFlags.FireAndForget);
184184

185-
Assert.Equal("embstr", db.KeyEncoding(key));
186-
Assert.Equal("embstr", await db.KeyEncodingAsync(key));
185+
Assert.True(db.KeyEncoding(key) is "embstr" or "raw"); // server-version dependent
186+
Assert.True(await db.KeyEncodingAsync(key) is "embstr" or "raw");
187187

188188
db.KeyDelete(key, CommandFlags.FireAndForget);
189189
db.ListLeftPush(key, "new value", flags: CommandFlags.FireAndForget);

tests/StackExchange.Redis.Tests/StreamTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,28 @@ public async Task AddWithApproxCount(StreamTrimMode mode)
21552155
db.StreamAdd(key, "field", "value", maxLength: 10, useApproximateMaxLength: true, trimMode: mode, flags: CommandFlags.None);
21562156
}
21572157

2158+
[Theory]
2159+
[InlineData(StreamTrimMode.KeepReferences, 1)]
2160+
[InlineData(StreamTrimMode.DeleteReferences, 1)]
2161+
[InlineData(StreamTrimMode.Acknowledged, 1)]
2162+
[InlineData(StreamTrimMode.KeepReferences, 2)]
2163+
[InlineData(StreamTrimMode.DeleteReferences, 2)]
2164+
[InlineData(StreamTrimMode.Acknowledged, 2)]
2165+
public async Task AddWithMultipleApproxCount(StreamTrimMode mode, int count)
2166+
{
2167+
await using var conn = Create(require: ForMode(mode));
2168+
2169+
var db = conn.GetDatabase();
2170+
var key = Me() + ":" + mode;
2171+
2172+
var pairs = new NameValueEntry[count];
2173+
for (var i = 0; i < count; i++)
2174+
{
2175+
pairs[i] = new NameValueEntry($"field{i}", $"value{i}");
2176+
}
2177+
db.StreamAdd(key, maxLength: 10, useApproximateMaxLength: true, trimMode: mode, flags: CommandFlags.None, streamPairs: pairs);
2178+
}
2179+
21582180
[Fact]
21592181
public async Task StreamReadGroupWithNoAckShowsNoPendingMessages()
21602182
{

0 commit comments

Comments
 (0)