77 "regexp"
88 "strings"
99 "testing"
10+
11+ "github.com/redis/go-redis/v9/internal"
1012)
1113
1214// validateJSONInLogMessage extracts and validates JSON from a log message
@@ -48,8 +50,13 @@ func validateJSONInLogMessage(t *testing.T, logMessage string, expectedData map[
4850}
4951
5052func TestHandoffLogFunctions (t * testing.T ) {
53+ // Set debug level for testing
54+ oldLogLevel := internal .LogLevel
55+ internal .LogLevel = 3 // Debug level
56+ defer func () { internal .LogLevel = oldLogLevel }()
57+
5158 t .Run ("HandoffStarted" , func (t * testing.T ) {
52- result := HandoffStarted (LogLevelDebug , 123 , "localhost:6379" )
59+ result := HandoffStarted (123 , "localhost:6379" )
5360 if ! strings .Contains (result , "conn[123] handoff started to localhost:6379" ) {
5461 t .Errorf ("Expected message to contain handoff started text, got: %s" , result )
5562 }
@@ -61,7 +68,7 @@ func TestHandoffLogFunctions(t *testing.T) {
6168
6269 t .Run ("HandoffFailed" , func (t * testing.T ) {
6370 err := errors .New ("connection refused" )
64- result := HandoffFailed (LogLevelDebug , 123 , "localhost:6379" , 2 , 3 , err )
71+ result := HandoffFailed (123 , "localhost:6379" , 2 , 3 , err )
6572 if ! strings .Contains (result , "conn[123] handoff failed to localhost:6379 (attempt 2/3): connection refused" ) {
6673 t .Errorf ("Expected message to contain handoff failed text, got: %s" , result )
6774 }
@@ -75,7 +82,7 @@ func TestHandoffLogFunctions(t *testing.T) {
7582 })
7683
7784 t .Run ("HandoffSucceeded" , func (t * testing.T ) {
78- result := HandoffSucceeded (LogLevelDebug , 123 , "localhost:6379" )
85+ result := HandoffSucceeded (123 , "localhost:6379" )
7986 if ! strings .Contains (result , "conn[123] handoff succeeded to localhost:6379" ) {
8087 t .Errorf ("Expected message to contain handoff succeeded text, got: %s" , result )
8188 }
@@ -87,24 +94,29 @@ func TestHandoffLogFunctions(t *testing.T) {
8794}
8895
8996func TestTimeoutLogFunctions (t * testing.T ) {
97+ // Set debug level for testing
98+ oldLogLevel := internal .LogLevel
99+ internal .LogLevel = 3 // Debug level
100+ defer func () { internal .LogLevel = oldLogLevel }()
101+
90102 t .Run ("RelaxedTimeoutDueToNotification" , func (t * testing.T ) {
91- result := RelaxedTimeoutDueToNotification (LogLevelDebug , 123 , "MIGRATING" , "10s" )
103+ result := RelaxedTimeoutDueToNotification (123 , "MIGRATING" , "10s" )
92104 expected := "conn[123] applying relaxed timeout due to notification MIGRATING (10s) {\" connID\" :123,\" notificationType\" :\" MIGRATING\" ,\" timeout\" :\" 10s\" }"
93105 if result != expected {
94106 t .Errorf ("Expected %q, got %q" , expected , result )
95107 }
96108 })
97109
98110 t .Run ("UnrelaxedTimeout" , func (t * testing.T ) {
99- result := UnrelaxedTimeout (LogLevelDebug , 123 )
111+ result := UnrelaxedTimeout (123 )
100112 expected := "conn[123] clearing relaxed timeout {\" connID\" :123}"
101113 if result != expected {
102114 t .Errorf ("Expected %q, got %q" , expected , result )
103115 }
104116 })
105117
106118 t .Run ("UnrelaxedTimeoutAfterDeadline" , func (t * testing.T ) {
107- result := UnrelaxedTimeoutAfterDeadline (LogLevelDebug , 123 )
119+ result := UnrelaxedTimeoutAfterDeadline (123 )
108120 expected := "conn[123] clearing relaxed timeout after deadline {\" connID\" :123}"
109121 if result != expected {
110122 t .Errorf ("Expected %q, got %q" , expected , result )
@@ -148,23 +160,28 @@ func TestHandoffQueueLogFunctions(t *testing.T) {
148160}
149161
150162func TestNotificationLogFunctions (t * testing.T ) {
163+ // Set debug level for testing
164+ oldLogLevel := internal .LogLevel
165+ internal .LogLevel = 3 // Debug level
166+ defer func () { internal .LogLevel = oldLogLevel }()
167+
151168 t .Run ("ProcessingNotification" , func (t * testing.T ) {
152- result := ProcessingNotification (LogLevelDebug , 123 , 1 , "MOVING" , []interface {}{"MOVING" , "slot_data" })
169+ result := ProcessingNotification (123 , 1 , "MOVING" , []interface {}{"MOVING" , "slot_data" })
153170 if ! strings .Contains (result , "conn[123] seqId[1] processing notification MOVING:" ) {
154171 t .Errorf ("Expected message to contain processing notification, got %q" , result )
155172 }
156173 })
157174
158175 t .Run ("ProcessingNotificationFailed" , func (t * testing.T ) {
159176 err := errors .New ("invalid notification" )
160- result := ProcessingNotificationFailed (LogLevelDebug , 123 , "MOVING" , err , []interface {}{"MOVING" })
177+ result := ProcessingNotificationFailed (123 , "MOVING" , err , []interface {}{"MOVING" })
161178 if ! strings .Contains (result , "conn[123] failed to process notification MOVING: invalid notification" ) {
162179 t .Errorf ("Expected message to contain failed processing, got %q" , result )
163180 }
164181 })
165182
166183 t .Run ("ProcessingNotificationSucceeded" , func (t * testing.T ) {
167- result := ProcessingNotificationSucceeded (LogLevelDebug , 123 , "MOVING" )
184+ result := ProcessingNotificationSucceeded (123 , "MOVING" )
168185 expected := "conn[123] processed notification successfully MOVING {\" connID\" :123,\" notificationType\" :\" MOVING\" }"
169186 if result != expected {
170187 t .Errorf ("Expected %q, got %q" , expected , result )
@@ -173,32 +190,37 @@ func TestNotificationLogFunctions(t *testing.T) {
173190}
174191
175192func TestMovingOperationLogFunctions (t * testing.T ) {
193+ // Set debug level for testing
194+ oldLogLevel := internal .LogLevel
195+ internal .LogLevel = 3 // Debug level
196+ defer func () { internal .LogLevel = oldLogLevel }()
197+
176198 t .Run ("DuplicateMovingOperation" , func (t * testing.T ) {
177- result := DuplicateMovingOperation (LogLevelDebug , 123 , "localhost:6379" , 456 )
199+ result := DuplicateMovingOperation (123 , "localhost:6379" , 456 )
178200 expected := "conn[123] duplicate MOVING operation ignored for localhost:6379 (seqID: 456) {\" connID\" :123,\" endpoint\" :\" localhost:6379\" ,\" seqID\" :456}"
179201 if result != expected {
180202 t .Errorf ("Expected %q, got %q" , expected , result )
181203 }
182204 })
183205
184206 t .Run ("TrackingMovingOperation" , func (t * testing.T ) {
185- result := TrackingMovingOperation (LogLevelDebug , 123 , "localhost:6379" , 456 )
207+ result := TrackingMovingOperation (123 , "localhost:6379" , 456 )
186208 expected := "conn[123] tracking MOVING operation for localhost:6379 (seqID: 456) {\" connID\" :123,\" endpoint\" :\" localhost:6379\" ,\" seqID\" :456}"
187209 if result != expected {
188210 t .Errorf ("Expected %q, got %q" , expected , result )
189211 }
190212 })
191213
192214 t .Run ("UntrackingMovingOperation" , func (t * testing.T ) {
193- result := UntrackingMovingOperation (LogLevelDebug , 123 , 456 )
215+ result := UntrackingMovingOperation (123 , 456 )
194216 expected := "conn[123] untracking MOVING operation (seqID: 456) {\" connID\" :123,\" seqID\" :456}"
195217 if result != expected {
196218 t .Errorf ("Expected %q, got %q" , expected , result )
197219 }
198220 })
199221
200222 t .Run ("OperationNotTracked" , func (t * testing.T ) {
201- result := OperationNotTracked (LogLevelDebug , 123 , 456 )
223+ result := OperationNotTracked (123 , 456 )
202224 expected := "conn[123] operation not tracked (seqID: 456) {\" connID\" :123,\" seqID\" :456}"
203225 if result != expected {
204226 t .Errorf ("Expected %q, got %q" , expected , result )
@@ -207,15 +229,20 @@ func TestMovingOperationLogFunctions(t *testing.T) {
207229}
208230
209231func TestConditionalJSONLogging (t * testing.T ) {
232+ oldLogLevel := internal .LogLevel
233+ defer func () { internal .LogLevel = oldLogLevel }()
234+
210235 t .Run ("DebugLevel_IncludesJSON" , func (t * testing.T ) {
211- result := HandoffStarted (LogLevelDebug , 123 , "localhost:6379" )
236+ internal .LogLevel = 3 // Debug level
237+ result := HandoffStarted (123 , "localhost:6379" )
212238 if ! strings .Contains (result , "{\" connID\" :123,\" endpoint\" :\" localhost:6379\" }" ) {
213239 t .Errorf ("Expected JSON to be included at Debug level, got: %s" , result )
214240 }
215241 })
216242
217243 t .Run ("ErrorLevel_ExcludesJSON" , func (t * testing.T ) {
218- result := HandoffStarted (LogLevelError , 123 , "localhost:6379" )
244+ internal .LogLevel = 0 // Error level
245+ result := HandoffStarted (123 , "localhost:6379" )
219246 if strings .Contains (result , "{" ) {
220247 t .Errorf ("Expected JSON to be excluded at Error level, got: %s" , result )
221248 }
@@ -226,11 +253,12 @@ func TestConditionalJSONLogging(t *testing.T) {
226253 })
227254
228255 t .Run ("InfoLevel_ExcludesJSON" , func (t * testing.T ) {
229- result := DuplicateMovingOperation (LogLevelInfo , 123 , "localhost:6379" , 456 )
256+ internal .LogLevel = 2 // Info level
257+ result := DuplicateMovingOperation (123 , "localhost:6379" , 456 )
230258 if strings .Contains (result , "{" ) {
231259 t .Errorf ("Expected JSON to be excluded at Info level, got: %s" , result )
232260 }
233- expected := "conn[123] duplicate MOVING operation ignored for localhost:6379 ( seqID: 456) "
261+ expected := "conn[123] duplicate MOVING operation ignored for localhost:6379 seqID[ 456] "
234262 if result != expected {
235263 t .Errorf ("Expected %q, got %q" , expected , result )
236264 }
@@ -306,30 +334,35 @@ func TestConnectionStateLogFunctions(t *testing.T) {
306334
307335// TestAllFunctionsHaveStructuredFormat ensures all log functions have structured format
308336func TestAllFunctionsHaveStructuredFormat (t * testing.T ) {
337+ // Set debug level for testing
338+ oldLogLevel := internal .LogLevel
339+ internal .LogLevel = 3 // Debug level
340+ defer func () { internal .LogLevel = oldLogLevel }()
341+
309342 testCases := []struct {
310343 name string
311344 function func () string
312345 }{
313- {"HandoffStarted" , func () string { return HandoffStarted (LogLevelDebug , 1 , "test" ) }},
314- {"HandoffFailed" , func () string { return HandoffFailed (LogLevelDebug , 1 , "test" , 1 , 3 , errors .New ("test" )) }},
315- {"HandoffSucceeded" , func () string { return HandoffSucceeded (LogLevelDebug , 1 , "test" ) }},
316- {"RelaxedTimeoutDueToNotification" , func () string { return RelaxedTimeoutDueToNotification (LogLevelDebug , 1 , "TEST" , "5s" ) }},
346+ {"HandoffStarted" , func () string { return HandoffStarted (1 , "test" ) }},
347+ {"HandoffFailed" , func () string { return HandoffFailed (1 , "test" , 1 , 3 , errors .New ("test" )) }},
348+ {"HandoffSucceeded" , func () string { return HandoffSucceeded (1 , "test" ) }},
349+ {"RelaxedTimeoutDueToNotification" , func () string { return RelaxedTimeoutDueToNotification (1 , "TEST" , "5s" ) }},
317350
318- {"UnrelaxedTimeout" , func () string { return UnrelaxedTimeout (LogLevelDebug , 1 ) }},
319- {"UnrelaxedTimeoutAfterDeadline" , func () string { return UnrelaxedTimeoutAfterDeadline (LogLevelDebug , 1 ) }},
351+ {"UnrelaxedTimeout" , func () string { return UnrelaxedTimeout (1 ) }},
352+ {"UnrelaxedTimeoutAfterDeadline" , func () string { return UnrelaxedTimeoutAfterDeadline (1 ) }},
320353 {"HandoffQueueFull" , func () string { return HandoffQueueFull (1 , 10 ) }},
321354 {"FailedToQueueHandoff" , func () string { return FailedToQueueHandoff (1 , errors .New ("test" )) }},
322355 {"ConnectionAlreadyMarkedForHandoff" , func () string { return ConnectionAlreadyMarkedForHandoff (1 ) }},
323356 {"ReachedMaxHandoffRetries" , func () string { return ReachedMaxHandoffRetries (1 , "test" , 3 ) }},
324- {"ProcessingNotification" , func () string { return ProcessingNotification (LogLevelDebug , 1 , 2 , "TEST" , "data" ) }},
357+ {"ProcessingNotification" , func () string { return ProcessingNotification (1 , 2 , "TEST" , "data" ) }},
325358 {"ProcessingNotificationFailed" , func () string {
326- return ProcessingNotificationFailed (LogLevelDebug , 1 , "TEST" , errors .New ("test" ), "data" )
359+ return ProcessingNotificationFailed (1 , "TEST" , errors .New ("test" ), "data" )
327360 }},
328- {"ProcessingNotificationSucceeded" , func () string { return ProcessingNotificationSucceeded (LogLevelDebug , 1 , "TEST" ) }},
329- {"DuplicateMovingOperation" , func () string { return DuplicateMovingOperation (LogLevelDebug , 1 , "test" , 1 ) }},
330- {"TrackingMovingOperation" , func () string { return TrackingMovingOperation (LogLevelDebug , 1 , "test" , 1 ) }},
331- {"UntrackingMovingOperation" , func () string { return UntrackingMovingOperation (LogLevelDebug , 1 , 1 ) }},
332- {"OperationNotTracked" , func () string { return OperationNotTracked (LogLevelDebug , 1 , 1 ) }},
361+ {"ProcessingNotificationSucceeded" , func () string { return ProcessingNotificationSucceeded (1 , "TEST" ) }},
362+ {"DuplicateMovingOperation" , func () string { return DuplicateMovingOperation (1 , "test" , 1 ) }},
363+ {"TrackingMovingOperation" , func () string { return TrackingMovingOperation (1 , "test" , 1 ) }},
364+ {"UntrackingMovingOperation" , func () string { return UntrackingMovingOperation (1 , 1 ) }},
365+ {"OperationNotTracked" , func () string { return OperationNotTracked (1 , 1 ) }},
333366 {"RemovingConnectionFromPool" , func () string { return RemovingConnectionFromPool (1 , errors .New ("test" )) }},
334367 {"NoPoolProvidedCannotRemove" , func () string { return NoPoolProvidedCannotRemove (1 , errors .New ("test" )) }},
335368 {"CircuitBreakerOpen" , func () string { return CircuitBreakerOpen (1 , "test" ) }},
@@ -338,8 +371,8 @@ func TestAllFunctionsHaveStructuredFormat(t *testing.T) {
338371 {"ShuttingDown" , func () string { return ShuttingDown () }},
339372 {"ConnectionInInvalidStateForHandoff" , func () string { return ConnectionInInvalidStateForHandoff (1 , "test" ) }},
340373
341- {"HandoffStarted" , func () string { return HandoffStarted (LogLevelDebug , 1 , "localhost:6379" ) }},
342- {"HandoffFailed" , func () string { return HandoffFailed (LogLevelDebug , 1 , "localhost:6379" , 1 , 3 , errors .New ("test" )) }},
374+ {"HandoffStarted" , func () string { return HandoffStarted (1 , "localhost:6379" ) }},
375+ {"HandoffFailed" , func () string { return HandoffFailed (1 , "localhost:6379" , 1 , 3 , errors .New ("test" )) }},
343376 {"ConnectionNotMarkedForHandoff" , func () string { return ConnectionNotMarkedForHandoff (1 ) }},
344377 {"HandoffRetryAttempt" , func () string { return HandoffRetryAttempt (1 , 2 , "new" , "old" ) }},
345378 {"CannotQueueHandoffForRetry" , func () string { return CannotQueueHandoffForRetry (errors .New ("test" )) }},
0 commit comments