Skip to content

Commit af3d52c

Browse files
committed
More optimization + stability
1 parent fe153c9 commit af3d52c

File tree

7 files changed

+101
-85
lines changed

7 files changed

+101
-85
lines changed

tests/StackExchange.Redis.Tests/ClusterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ public async Task GetFromRightNodeBasedOnFlags(CommandFlags flags, bool isReplic
541541
await using var conn = Create(allowAdmin: true);
542542

543543
var db = conn.GetDatabase();
544-
for (int i = 0; i < 1000; i++)
544+
for (int i = 0; i < 500; i++)
545545
{
546546
var key = Guid.NewGuid().ToString();
547547
var endpoint = db.IdentifyEndpoint(key, flags);

tests/StackExchange.Redis.Tests/ConfigTests.cs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,17 @@ public async Task GetInfo()
429429
Log("Full info for: " + first.Key);
430430
foreach (var setting in first)
431431
{
432-
Log("{0} ==> {1}", setting.Key, setting.Value);
432+
Log(" {0} ==> {1}", setting.Key, setting.Value);
433433
}
434434

435435
var info2 = server.Info("cpu");
436436
Assert.Single(info2);
437437
var cpu = info2.Single();
438+
Log("Full info for: " + cpu.Key);
439+
foreach (var setting in cpu)
440+
{
441+
Log(" {0} ==> {1}", setting.Key, setting.Value);
442+
}
438443
var cpuCount = cpu.Count();
439444
Assert.True(cpuCount > 2);
440445
Assert.Equal("CPU", cpu.Key);
@@ -496,43 +501,6 @@ public async Task SlowLog()
496501
server.SlowlogReset();
497502
}
498503

499-
[Fact]
500-
public async Task TestAutomaticHeartbeat()
501-
{
502-
RedisValue oldTimeout = RedisValue.Null;
503-
await using var configConn = Create(allowAdmin: true);
504-
505-
try
506-
{
507-
configConn.GetDatabase();
508-
var srv = GetAnyPrimary(configConn);
509-
oldTimeout = srv.ConfigGet("timeout")[0].Value;
510-
Log("Old Timeout: " + oldTimeout);
511-
srv.ConfigSet("timeout", 2);
512-
513-
await using var innerConn = Create();
514-
var innerDb = innerConn.GetDatabase();
515-
await innerDb.PingAsync(); // need to wait to pick up configuration etc
516-
517-
var before = innerConn.OperationCount;
518-
519-
Log("sleeping to test heartbeat...");
520-
await Task.Delay(3000).ForAwait();
521-
522-
var after = innerConn.OperationCount;
523-
Assert.True(after >= before + 1, $"after: {after}, before: {before}");
524-
}
525-
finally
526-
{
527-
if (!oldTimeout.IsNull)
528-
{
529-
Log("Resetting old timeout: " + oldTimeout);
530-
var srv = GetAnyPrimary(configConn);
531-
srv.ConfigSet("timeout", oldTimeout);
532-
}
533-
}
534-
}
535-
536504
[Fact]
537505
public void EndpointIteratorIsReliableOverChanges()
538506
{
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace StackExchange.Redis.Tests;
6+
7+
[RunPerProtocol]
8+
public class HeartbeatTests(ITestOutputHelper output, SharedConnectionFixture fixture) : TestBase(output, fixture)
9+
{
10+
[Fact]
11+
public async Task TestAutomaticHeartbeat()
12+
{
13+
RedisValue oldTimeout = RedisValue.Null;
14+
await using var configConn = Create(allowAdmin: true);
15+
16+
try
17+
{
18+
configConn.GetDatabase();
19+
var srv = GetAnyPrimary(configConn);
20+
oldTimeout = srv.ConfigGet("timeout")[0].Value;
21+
Log("Old Timeout: " + oldTimeout);
22+
srv.ConfigSet("timeout", 3);
23+
24+
await using var innerConn = Create();
25+
var innerDb = innerConn.GetDatabase();
26+
await innerDb.PingAsync(); // need to wait to pick up configuration etc
27+
28+
var before = innerConn.OperationCount;
29+
30+
Log("sleeping to test heartbeat...");
31+
await Task.Delay(TimeSpan.FromSeconds(5)).ForAwait();
32+
33+
var after = innerConn.OperationCount;
34+
Assert.True(after >= before + 1, $"after: {after}, before: {before}");
35+
}
36+
finally
37+
{
38+
if (!oldTimeout.IsNull)
39+
{
40+
Log("Resetting old timeout: " + oldTimeout);
41+
var srv = GetAnyPrimary(configConn);
42+
srv.ConfigSet("timeout", oldTimeout);
43+
}
44+
}
45+
}
46+
}

tests/StackExchange.Redis.Tests/Issues/Issue2507.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,40 @@
33
using System.Threading.Tasks;
44
using Xunit;
55

6-
namespace StackExchange.Redis.Tests.Issues
6+
namespace StackExchange.Redis.Tests.Issues;
7+
8+
[Collection(NonParallelCollection.Name)]
9+
public class Issue2507(ITestOutputHelper output, SharedConnectionFixture? fixture = null) : TestBase(output, fixture)
710
{
8-
[Collection(NonParallelCollection.Name)]
9-
public class Issue2507(ITestOutputHelper output, SharedConnectionFixture? fixture = null) : TestBase(output, fixture)
11+
[Fact]
12+
public async Task Execute()
1013
{
11-
[Fact]
12-
public async Task Execute()
13-
{
14-
await using var conn = Create(shared: false);
15-
var db = conn.GetDatabase();
16-
var pubsub = conn.GetSubscriber();
17-
var queue = await pubsub.SubscribeAsync(RedisChannel.Literal("__redis__:invalidate"));
18-
await Task.Delay(100);
19-
var connectionId = conn.GetConnectionId(conn.GetEndPoints().Single(), ConnectionType.Subscription);
20-
if (connectionId is null) Assert.Skip("Connection id not available");
14+
await using var conn = Create(shared: false);
15+
var db = conn.GetDatabase();
16+
var pubsub = conn.GetSubscriber();
17+
var queue = await pubsub.SubscribeAsync(RedisChannel.Literal("__redis__:invalidate"));
18+
await Task.Delay(100);
19+
var connectionId = conn.GetConnectionId(conn.GetEndPoints().Single(), ConnectionType.Subscription);
20+
if (connectionId is null) Assert.Skip("Connection id not available");
2121

22-
string baseKey = Me();
23-
RedisKey key1 = baseKey + "abc",
24-
key2 = baseKey + "ghi",
25-
key3 = baseKey + "mno";
22+
string baseKey = Me();
23+
RedisKey key1 = baseKey + "abc",
24+
key2 = baseKey + "ghi",
25+
key3 = baseKey + "mno";
2626

27-
await db.StringSetAsync(new KeyValuePair<RedisKey, RedisValue>[] { new(key1, "def"), new(key2, "jkl"), new(key3, "pqr") });
28-
// this is not supported, but: we want it to at least not fail
29-
await db.ExecuteAsync("CLIENT", "TRACKING", "on", "REDIRECT", connectionId!.Value, "BCAST");
30-
await db.KeyDeleteAsync(new RedisKey[] { key1, key2, key3 });
31-
await Task.Delay(100);
32-
queue.Unsubscribe();
33-
Assert.True(queue.TryRead(out var message));
34-
Assert.Equal(key1, message.Message);
35-
Assert.True(queue.TryRead(out message));
36-
Assert.Equal(key2, message.Message);
37-
Assert.True(queue.TryRead(out message));
38-
Assert.Equal(key3, message.Message);
39-
Assert.False(queue.TryRead(out message));
40-
}
27+
await db.StringSetAsync([new(key1, "def"), new(key2, "jkl"), new(key3, "pqr")]);
28+
// this is not supported, but: we want it to at least not fail
29+
await db.ExecuteAsync("CLIENT", "TRACKING", "on", "REDIRECT", connectionId!.Value, "BCAST");
30+
await db.KeyDeleteAsync([key1, key2, key3]);
31+
await Task.Delay(100);
32+
queue.Unsubscribe();
33+
Assert.True(queue.TryRead(out var message), "Queue 1 Read failed");
34+
Assert.Equal(key1, message.Message);
35+
Assert.True(queue.TryRead(out message), "Queue 2 Read failed");
36+
Assert.Equal(key2, message.Message);
37+
Assert.True(queue.TryRead(out message), "Queue 3 Read failed");
38+
Assert.Equal(key3, message.Message);
39+
// Paralle test suites can be invalidating at the same time, so this is not guaranteed to be empty
40+
// Assert.False(queue.TryRead(out message), "Queue 4 Read succeeded");
4141
}
4242
}

tests/StackExchange.Redis.Tests/KeyTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public async Task IdleTime()
180180
var db = conn.GetDatabase();
181181
db.KeyDelete(key, CommandFlags.FireAndForget);
182182
db.StringSet(key, "new value", flags: CommandFlags.FireAndForget);
183-
await Task.Delay(2000).ForAwait();
183+
await Task.Delay(1000).ForAwait();
184184
var idleTime = db.KeyIdleTime(key);
185185
Assert.True(idleTime > TimeSpan.Zero);
186186

@@ -202,13 +202,13 @@ public async Task TouchIdleTime()
202202
var db = conn.GetDatabase();
203203
db.KeyDelete(key, CommandFlags.FireAndForget);
204204
db.StringSet(key, "new value", flags: CommandFlags.FireAndForget);
205-
await Task.Delay(2000).ForAwait();
205+
await Task.Delay(1000).ForAwait();
206206
var idleTime = db.KeyIdleTime(key);
207-
Assert.True(idleTime > TimeSpan.Zero);
207+
Assert.True(idleTime > TimeSpan.Zero, "First check");
208208

209-
Assert.True(db.KeyTouch(key));
209+
Assert.True(db.KeyTouch(key), "Second check");
210210
var idleTime1 = db.KeyIdleTime(key);
211-
Assert.True(idleTime1 < idleTime);
211+
Assert.True(idleTime1 < idleTime, "Third check");
212212
}
213213

214214
[Fact]
@@ -220,13 +220,13 @@ public async Task IdleTimeAsync()
220220
var db = conn.GetDatabase();
221221
db.KeyDelete(key, CommandFlags.FireAndForget);
222222
db.StringSet(key, "new value", flags: CommandFlags.FireAndForget);
223-
await Task.Delay(2000).ForAwait();
223+
await Task.Delay(1000).ForAwait();
224224
var idleTime = await db.KeyIdleTimeAsync(key).ForAwait();
225-
Assert.True(idleTime > TimeSpan.Zero);
225+
Assert.True(idleTime > TimeSpan.Zero, "First check");
226226

227227
db.StringSet(key, "new value2", flags: CommandFlags.FireAndForget);
228228
var idleTime2 = await db.KeyIdleTimeAsync(key).ForAwait();
229-
Assert.True(idleTime2 < idleTime);
229+
Assert.True(idleTime2 < idleTime, "Second check");
230230

231231
db.KeyDelete(key);
232232
var idleTime3 = await db.KeyIdleTimeAsync(key).ForAwait();
@@ -242,13 +242,13 @@ public async Task TouchIdleTimeAsync()
242242
var db = conn.GetDatabase();
243243
db.KeyDelete(key, CommandFlags.FireAndForget);
244244
db.StringSet(key, "new value", flags: CommandFlags.FireAndForget);
245-
await Task.Delay(2000).ForAwait();
245+
await Task.Delay(1000).ForAwait();
246246
var idleTime = await db.KeyIdleTimeAsync(key).ForAwait();
247-
Assert.True(idleTime > TimeSpan.Zero);
247+
Assert.True(idleTime > TimeSpan.Zero, "First check");
248248

249-
Assert.True(await db.KeyTouchAsync(key).ForAwait());
249+
Assert.True(await db.KeyTouchAsync(key).ForAwait(), "Second check");
250250
var idleTime1 = await db.KeyIdleTimeAsync(key).ForAwait();
251-
Assert.True(idleTime1 < idleTime);
251+
Assert.True(idleTime1 < idleTime, "Third check");
252252
}
253253

254254
[Fact]

tests/StackExchange.Redis.Tests/LatencyTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ public async Task CanReset()
3838
[Fact]
3939
public async Task GetLatest()
4040
{
41+
Skip.UnlessLongRunning();
4142
await using var conn = Create(allowAdmin: true);
4243

4344
var server = conn.GetServer(conn.GetEndPoints()[0]);
44-
server.ConfigSet("latency-monitor-threshold", 100);
45+
server.ConfigSet("latency-monitor-threshold", 50);
4546
server.LatencyReset();
4647
var arr = server.LatencyLatest();
4748
Assert.Empty(arr);
@@ -60,10 +61,11 @@ public async Task GetLatest()
6061
[Fact]
6162
public async Task GetHistory()
6263
{
64+
Skip.UnlessLongRunning();
6365
await using var conn = Create(allowAdmin: true);
6466

6567
var server = conn.GetServer(conn.GetEndPoints()[0]);
66-
server.ConfigSet("latency-monitor-threshold", 100);
68+
server.ConfigSet("latency-monitor-threshold", 50);
6769
server.LatencyReset();
6870
var arr = server.LatencyHistory("command");
6971
Assert.Empty(arr);

tests/StackExchange.Redis.Tests/MultiPrimaryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void TestMultiNoTieBreak()
4444
yield return new object?[] { TestConfig.Current.SecureServerAndPort, TestConfig.Current.PrimaryServerAndPort, null };
4545
yield return new object?[] { TestConfig.Current.PrimaryServerAndPort, TestConfig.Current.SecureServerAndPort, null };
4646

47-
yield return new object?[] { null, TestConfig.Current.PrimaryServerAndPort, TestConfig.Current.PrimaryServerAndPort };
47+
yield return new object?[] { null, TestConfig.Current.PrimaryServerAndPort, null };
4848
yield return new object?[] { TestConfig.Current.PrimaryServerAndPort, null, TestConfig.Current.PrimaryServerAndPort };
4949
yield return new object?[] { null, TestConfig.Current.SecureServerAndPort, TestConfig.Current.SecureServerAndPort };
5050
yield return new object?[] { TestConfig.Current.SecureServerAndPort, null, TestConfig.Current.SecureServerAndPort };

0 commit comments

Comments
 (0)