11using System ;
2- using System . Collections . Generic ;
2+ using System . Collections . Concurrent ;
33using System . Diagnostics ;
4- using System . Text ;
5- using System . Threading . Tasks ;
64using StackExchange . Redis ;
75
86namespace 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