Skip to content

Commit fe153c9

Browse files
committed
More key conflict cleanup
1 parent 6b6aca7 commit fe153c9

File tree

5 files changed

+42
-28
lines changed

5 files changed

+42
-28
lines changed

tests/StackExchange.Redis.Tests/ConfigTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ public async Task TestAutomaticHeartbeat()
507507
configConn.GetDatabase();
508508
var srv = GetAnyPrimary(configConn);
509509
oldTimeout = srv.ConfigGet("timeout")[0].Value;
510-
srv.ConfigSet("timeout", 5);
510+
Log("Old Timeout: " + oldTimeout);
511+
srv.ConfigSet("timeout", 2);
511512

512513
await using var innerConn = Create();
513514
var innerDb = innerConn.GetDatabase();
@@ -516,7 +517,7 @@ public async Task TestAutomaticHeartbeat()
516517
var before = innerConn.OperationCount;
517518

518519
Log("sleeping to test heartbeat...");
519-
await Task.Delay(8000).ForAwait();
520+
await Task.Delay(3000).ForAwait();
520521

521522
var after = innerConn.OperationCount;
522523
Assert.True(after >= before + 1, $"after: {after}, before: {before}");
@@ -525,6 +526,7 @@ public async Task TestAutomaticHeartbeat()
525526
{
526527
if (!oldTimeout.IsNull)
527528
{
529+
Log("Resetting old timeout: " + oldTimeout);
528530
var srv = GetAnyPrimary(configConn);
529531
srv.ConfigSet("timeout", oldTimeout);
530532
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ public static class TestConfig
1313

1414
public static Config Current { get; }
1515

16+
#if NET
1617
private static int _db = 17;
18+
#else
19+
private static int _db = 77;
20+
#endif
1721
public static int GetDedicatedDB(IConnectionMultiplexer? conn = null)
1822
{
1923
int db = Interlocked.Increment(ref _db);

tests/StackExchange.Redis.Tests/LatencyTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace StackExchange.Redis.Tests;
55

6+
[Collection(NonParallelCollection.Name)]
67
public class LatencyTests(ITestOutputHelper output, SharedConnectionFixture fixture) : TestBase(output, fixture)
78
{
89
[Fact]

tests/StackExchange.Redis.Tests/RespProtocolTests.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ public async Task ConnectWithBrokenHello(string command, bool isResp3)
127127
[InlineData("return 'abc'", RedisProtocol.Resp2, ResultType.BulkString, ResultType.BulkString, "abc")]
128128
[InlineData(@"return {1,2,3}", RedisProtocol.Resp2, ResultType.Array, ResultType.Array, ARR_123)]
129129
[InlineData("return nil", RedisProtocol.Resp2, ResultType.BulkString, ResultType.Null, null)]
130-
[InlineData(@"return redis.pcall('hgetall', 'key')", RedisProtocol.Resp2, ResultType.Array, ResultType.Array, MAP_ABC)]
131-
[InlineData(@"redis.setresp(3) return redis.pcall('hgetall', 'key')", RedisProtocol.Resp2, ResultType.Array, ResultType.Array, MAP_ABC)]
130+
[InlineData(@"return redis.pcall('hgetall', '{key}')", RedisProtocol.Resp2, ResultType.Array, ResultType.Array, MAP_ABC)]
131+
[InlineData(@"redis.setresp(3) return redis.pcall('hgetall', '{key}')", RedisProtocol.Resp2, ResultType.Array, ResultType.Array, MAP_ABC)]
132132
[InlineData("return true", RedisProtocol.Resp2, ResultType.Integer, ResultType.Integer, 1)]
133133
[InlineData("return false", RedisProtocol.Resp2, ResultType.BulkString, ResultType.Null, null)]
134134
[InlineData("redis.setresp(3) return true", RedisProtocol.Resp2, ResultType.Integer, ResultType.Integer, 1)]
@@ -142,8 +142,8 @@ public async Task ConnectWithBrokenHello(string command, bool isResp3)
142142
[InlineData("return 'abc'", RedisProtocol.Resp3, ResultType.BulkString, ResultType.BulkString, "abc")]
143143
[InlineData("return {1,2,3}", RedisProtocol.Resp3, ResultType.Array, ResultType.Array, ARR_123)]
144144
[InlineData("return nil", RedisProtocol.Resp3, ResultType.BulkString, ResultType.Null, null)]
145-
[InlineData(@"return redis.pcall('hgetall', 'key')", RedisProtocol.Resp3, ResultType.Array, ResultType.Array, MAP_ABC)]
146-
[InlineData(@"redis.setresp(3) return redis.pcall('hgetall', 'key')", RedisProtocol.Resp3, ResultType.Array, ResultType.Map, MAP_ABC)]
145+
[InlineData(@"return redis.pcall('hgetall', '{key}')", RedisProtocol.Resp3, ResultType.Array, ResultType.Array, MAP_ABC)]
146+
[InlineData(@"redis.setresp(3) return redis.pcall('hgetall', '{key}')", RedisProtocol.Resp3, ResultType.Array, ResultType.Map, MAP_ABC)]
147147
[InlineData("return true", RedisProtocol.Resp3, ResultType.Integer, ResultType.Integer, 1)]
148148
[InlineData("return false", RedisProtocol.Resp3, ResultType.BulkString, ResultType.Null, null)]
149149
[InlineData("redis.setresp(3) return true", RedisProtocol.Resp3, ResultType.Integer, ResultType.Boolean, true)]
@@ -167,14 +167,16 @@ public async Task CheckLuaResult(string script, RedisProtocol protocol, ResultTy
167167
}
168168
if (ep.Protocol is null) throw new InvalidOperationException($"No protocol! {ep.InteractiveConnectionState}");
169169
Assert.Equal(protocol, ep.Protocol);
170+
var key = Me();
171+
script = script.Replace("{key}", key);
170172

171173
var db = muxer.GetDatabase();
172174
if (expected is MAP_ABC)
173175
{
174-
db.KeyDelete("key");
175-
db.HashSet("key", "a", 1);
176-
db.HashSet("key", "b", 2);
177-
db.HashSet("key", "c", 3);
176+
db.KeyDelete(key);
177+
db.HashSet(key, "a", 1);
178+
db.HashSet(key, "b", 2);
179+
db.HashSet(key, "c", 3);
178180
}
179181
var result = await db.ScriptEvaluateAsync(script: script, flags: CommandFlags.NoScriptCache);
180182
Assert.Equal(resp2, result.Resp2Type);

tests/StackExchange.Redis.Tests/ScriptingTests.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,20 @@ public async Task CheckLoads(bool async)
395395
// the flush to drop the local cache - assume it is a surprise!)
396396
var server = conn0.GetServer(TestConfig.Current.PrimaryServerAndPort);
397397
var db = conn1.GetDatabase();
398-
const string Script = "return 1;";
398+
var key = Me();
399+
var Script = $"return '{key}';";
399400

400401
// start empty
401402
server.ScriptFlush();
402403
Assert.False(server.ScriptExists(Script));
403404

404405
// run once, causes to be cached
405-
Assert.True(await EvaluateScript());
406+
Assert.Equal(key, await EvaluateScript());
406407

407408
Assert.True(server.ScriptExists(Script));
408409

409410
// can run again
410-
Assert.True(await EvaluateScript());
411+
Assert.Equal(key, await EvaluateScript());
411412

412413
// ditch the scripts; should no longer exist
413414
await db.PingAsync();
@@ -416,19 +417,19 @@ public async Task CheckLoads(bool async)
416417
await db.PingAsync();
417418

418419
// just works; magic
419-
Assert.True(await EvaluateScript());
420+
Assert.Equal(key, await EvaluateScript());
420421

421422
// but gets marked as unloaded, so we can use it again...
422-
Assert.True(await EvaluateScript());
423+
Assert.Equal(key, await EvaluateScript());
423424

424425
// which will cause it to be cached
425426
Assert.True(server.ScriptExists(Script));
426427

427-
async Task<bool> EvaluateScript()
428+
async Task<string?> EvaluateScript()
428429
{
429430
return async ?
430-
(bool)await db.ScriptEvaluateAsync(script: Script) :
431-
(bool)db.ScriptEvaluate(script: Script);
431+
(string?)await db.ScriptEvaluateAsync(script: Script) :
432+
(string?)db.ScriptEvaluate(script: Script);
432433
}
433434
}
434435

@@ -1083,12 +1084,14 @@ public async Task TestEvalShaReadOnly()
10831084
{
10841085
await using var conn = GetScriptConn();
10851086
var db = conn.GetDatabase();
1086-
db.StringSet("foo", "bar");
1087-
db.ScriptEvaluate(script: "return redis.call('get','foo')");
1088-
// Create a SHA1 hash of the script: 6b1bf486c81ceb7edf3c093f4c48582e38c0e791
1089-
SHA1 sha1Hash = SHA1.Create();
1087+
var key = Me();
1088+
var script = $"return redis.call('get','{key}')";
1089+
db.StringSet(key, "bar");
1090+
db.ScriptEvaluate(script: script);
10901091

1091-
byte[] hash = sha1Hash.ComputeHash(Encoding.UTF8.GetBytes("return redis.call('get','foo')"));
1092+
SHA1 sha1Hash = SHA1.Create();
1093+
byte[] hash = sha1Hash.ComputeHash(Encoding.UTF8.GetBytes(script));
1094+
Log("Hash: " + Convert.ToBase64String(hash));
10921095
var result = db.ScriptEvaluateReadOnly(hash);
10931096

10941097
Assert.Equal("bar", result.ToString());
@@ -1099,12 +1102,14 @@ public async Task TestEvalShaReadOnlyAsync()
10991102
{
11001103
await using var conn = GetScriptConn();
11011104
var db = conn.GetDatabase();
1102-
db.StringSet("foo", "bar");
1103-
db.ScriptEvaluate(script: "return redis.call('get','foo')");
1104-
// Create a SHA1 hash of the script: 6b1bf486c81ceb7edf3c093f4c48582e38c0e791
1105-
SHA1 sha1Hash = SHA1.Create();
1105+
var key = Me();
1106+
var script = $"return redis.call('get','{key}')";
1107+
db.StringSet(key, "bar");
1108+
db.ScriptEvaluate(script: script);
11061109

1107-
byte[] hash = sha1Hash.ComputeHash(Encoding.UTF8.GetBytes("return redis.call('get','foo')"));
1110+
SHA1 sha1Hash = SHA1.Create();
1111+
byte[] hash = sha1Hash.ComputeHash(Encoding.UTF8.GetBytes(script));
1112+
Log("Hash: " + Convert.ToBase64String(hash));
11081113
var result = await db.ScriptEvaluateReadOnlyAsync(hash);
11091114

11101115
Assert.Equal("bar", result.ToString());

0 commit comments

Comments
 (0)