Skip to content

Commit 508d1a8

Browse files
committed
Additional error information from Redis for tests
1 parent fb4ca07 commit 508d1a8

4 files changed

Lines changed: 48 additions & 14 deletions

File tree

tests/CacheTower.Tests/Extensions/Redis/RedisLockExtensionTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void ThrowForRegisteringTwoCacheStacks()
4141
[TestMethod]
4242
public async Task CustomLockTimeout()
4343
{
44-
RedisHelper.FlushDatabase();
44+
RedisHelper.ResetState();
4545

4646
var extension = new RedisLockExtension(RedisHelper.GetConnection(), new RedisLockOptions(lockTimeout: TimeSpan.FromDays(1)));
4747
var refreshWaiterTask = new TaskCompletionSource<bool>();
@@ -76,7 +76,7 @@ public async Task CustomLockTimeout()
7676
[TestMethod]
7777
public async Task RefreshValueNotifiesChannelSubscribers()
7878
{
79-
RedisHelper.FlushDatabase();
79+
RedisHelper.ResetState();
8080

8181
var connection = RedisHelper.GetConnection();
8282

@@ -117,7 +117,7 @@ await extension.WithRefreshAsync("TestKey",
117117
[TestMethod]
118118
public async Task ObservedLockSingle()
119119
{
120-
RedisHelper.FlushDatabase();
120+
RedisHelper.ResetState();
121121

122122
var connection = RedisHelper.GetConnection();
123123

@@ -160,7 +160,7 @@ public async Task ObservedLockSingle()
160160
[TestMethod]
161161
public async Task ObservedLockMultiple()
162162
{
163-
RedisHelper.FlushDatabase();
163+
RedisHelper.ResetState();
164164

165165
var connection = RedisHelper.GetConnection();
166166

tests/CacheTower.Tests/Extensions/Redis/RedisRemoteEvictionExtensionTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void ThrowForRegisteringMoreThanOneCacheStack()
4545
[TestMethod]
4646
public async Task RemoteEvictionOccursOnRefresh()
4747
{
48-
RedisHelper.FlushDatabase();
48+
RedisHelper.ResetState();
4949

5050
var connection = RedisHelper.GetConnection();
5151

@@ -88,7 +88,7 @@ public async Task RemoteEvictionOccursOnRefresh()
8888
[TestMethod]
8989
public async Task RemoteEvictionOccursOnLocalEviction()
9090
{
91-
RedisHelper.FlushDatabase();
91+
RedisHelper.ResetState();
9292

9393
var connection = RedisHelper.GetConnection();
9494

@@ -135,7 +135,7 @@ public async Task RemoteEvictionOccursOnLocalEviction()
135135
[TestMethod]
136136
public async Task RemoteFlush()
137137
{
138-
RedisHelper.FlushDatabase();
138+
RedisHelper.ResetState();
139139

140140
var connection = RedisHelper.GetConnection();
141141

@@ -182,7 +182,7 @@ public async Task RemoteFlush()
182182
[TestMethod]
183183
public async Task NoEvictionOnNewEntries()
184184
{
185-
RedisHelper.FlushDatabase();
185+
RedisHelper.ResetState();
186186

187187
var realConnection = RedisHelper.GetConnection();
188188

tests/CacheTower.Tests/Providers/Redis/RedisCacheLayerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class RedisCacheLayerTests : BaseCacheLayerTests
1717
[TestInitialize]
1818
public void Setup()
1919
{
20-
RedisHelper.FlushDatabase();
20+
RedisHelper.ResetState();
2121
}
2222

2323
[TestMethod]
Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
2+
using System.Collections.Concurrent;
33
using System.Diagnostics;
4-
using System.Text;
5-
using System.Threading.Tasks;
64
using StackExchange.Redis;
75

86
namespace CacheTower.Tests.Utils
@@ -11,6 +9,24 @@ public static class RedisHelper
119
{
1210
public static string Endpoint => Environment.GetEnvironmentVariable("REDIS_ENDPOINT") ?? "localhost:6379";
1311

12+
private static readonly ConcurrentQueue<string> Errors = new();
13+
14+
static RedisHelper()
15+
{
16+
var connection = GetConnection();
17+
connection.ErrorMessage += (sender, args) =>
18+
{
19+
Errors.Enqueue(args.Message);
20+
};
21+
connection.InternalError += (sender, args) =>
22+
{
23+
if (args.Exception is not null)
24+
{
25+
Errors.Enqueue(args.Exception.Message);
26+
}
27+
};
28+
}
29+
1430
public static ConnectionMultiplexer GetConnection()
1531
{
1632
var config = new ConfigurationOptions
@@ -21,15 +37,33 @@ public static ConnectionMultiplexer GetConnection()
2137
return ConnectionMultiplexer.Connect(config);
2238
}
2339

24-
public static void FlushDatabase()
40+
/// <summary>
41+
/// Flushes Redis and resets the state of error logging
42+
/// </summary>
43+
public static void ResetState()
2544
{
2645
GetConnection().GetServer(Endpoint).FlushDatabase();
46+
47+
//.NET Framework doesn't support `Clear()` on Errors so we do it manually
48+
while (!Errors.IsEmpty)
49+
{
50+
Errors.TryDequeue(out _);
51+
}
2752
}
2853

2954
public static void DebugInfo(IConnectionMultiplexer connection)
3055
{
56+
Debug.WriteLine("== Redis Connection Status ==");
3157
Debug.WriteLine(connection.GetStatus());
32-
Debug.WriteLine(connection.GetCounters().ToString());
58+
59+
Debug.WriteLine("== Errors (Redis and Internal) ==");
60+
while (!Errors.IsEmpty)
61+
{
62+
if (Errors.TryDequeue(out var message))
63+
{
64+
Debug.WriteLine(message);
65+
}
66+
}
3367
}
3468
}
3569
}

0 commit comments

Comments
 (0)