@@ -175,33 +175,50 @@ func TestAuthFailureCounterIncrement(t *testing.T) {
175175 }
176176
177177 // Test that counter increments only for auth errors
178- errors := []struct {
179- err error
180- shouldCount bool
178+ testCases := []struct {
179+ err error
180+ shouldCount bool
181+ description string
181182 }{
182- {errors .New ("ERROR 1045: Access denied" ), true },
183- {errors .New ("connection timeout" ), false },
184- {errors .New ("ERROR 1130: Host not allowed" ), true },
185- {errors .New ("syntax error" ), false },
186- {nil , false },
183+ {& gomysql.MyError {Code : 1045 , Message : "Access denied" }, true , "MySQL 1045 error" },
184+ {errors .New ("connection timeout" ), false , "Non-auth error" },
185+ {& gomysql.MyError {Code : 1130 , Message : "Host not allowed" }, true , "MySQL 1130 error" },
186+ {errors .New ("syntax error" ), false , "SQL syntax error" },
187+ {nil , false , "Nil error" },
188+ {errors .New ("access denied for user" ), true , "String fallback auth error" },
187189 }
188190
189- expectedCount := 0
190- for _ , e := range errors {
191- if reader .isAuthenticationError (e .err ) {
192- reader .authFailureCount ++
193- if e .shouldCount {
194- expectedCount ++
195- } else {
196- t .Errorf ("Counter incremented for non-auth error: %v" , e .err )
191+ for _ , tc := range testCases {
192+ initialCount := reader .authFailureCount
193+
194+ // For nil errors, handleAuthError would reset the counter
195+ // So we test isAuthenticationError directly for nil
196+ if tc .err == nil {
197+ if reader .isAuthenticationError (tc .err ) {
198+ t .Errorf ("%s: nil should not be detected as auth error" , tc .description )
199+ }
200+ continue
201+ }
202+
203+ // Use handleAuthError which manages the counter
204+ reader .handleAuthError (tc .err , "test" )
205+
206+ if tc .shouldCount {
207+ if reader .authFailureCount != initialCount + 1 {
208+ t .Errorf ("%s: Counter did not increment for auth error: %v" , tc .description , tc .err )
209+ }
210+ } else {
211+ if reader .authFailureCount != initialCount {
212+ t .Errorf ("%s: Counter incorrectly incremented for non-auth error: %v" , tc .description , tc .err )
197213 }
198- } else if e .shouldCount {
199- t .Errorf ("Counter did not increment for auth error: %v" , e .err )
200214 }
201215 }
202216
203- if reader .authFailureCount != expectedCount {
204- t .Errorf ("Expected auth failure count %d, got %d" , expectedCount , reader .authFailureCount )
217+ // Test that successful operation resets counter
218+ reader .authFailureCount = 5
219+ reader .handleAuthError (nil , "test success" )
220+ if reader .authFailureCount != 0 {
221+ t .Errorf ("Counter not reset on success, got %d" , reader .authFailureCount )
205222 }
206223}
207224
0 commit comments