@@ -10,6 +10,7 @@ import (
1010 "runtime"
1111 "strings"
1212 "sync"
13+ "sync/atomic"
1314 "testing"
1415 "time"
1516
@@ -887,38 +888,45 @@ func TestNewlyBlockedPRAfterGracePeriod(t *testing.T) {
887888
888889// TestAuthRetryLoopStopsOnSuccess tests that the auth retry loop stops when auth succeeds.
889890func TestAuthRetryLoopStopsOnSuccess (t * testing.T ) {
890- ctx := t .Context ()
891+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
892+ defer cancel ()
891893
892894 app := & App {
893895 mu : sync.RWMutex {},
894896 authError : "initial auth error" ,
895897 systrayInterface : & MockSystray {},
896898 }
897899
898- // Track how many times we check authError
899- checkCount := 0
900+ // Track how many times we check authError (use atomic for thread safety)
901+ var checkCount atomic. Int32
900902 done := make (chan struct {})
903+ exitReason := make (chan string , 1 )
901904
902905 // Start a goroutine that simulates the auth retry loop behavior
903906 go func () {
904907 defer close (done )
908+ ticker := time .NewTicker (10 * time .Millisecond )
909+ defer ticker .Stop ()
910+
905911 for {
906912 select {
907913 case <- ctx .Done ():
914+ exitReason <- "context canceled"
908915 return
909- case <- time . After ( 10 * time . Millisecond ): // Fast ticker for testing
916+ case <- ticker . C :
910917 app .mu .RLock ()
911918 hasError := app .authError != ""
912919 app .mu .RUnlock ()
913920
914- checkCount ++
921+ count := checkCount . Add ( 1 )
915922
916923 if ! hasError {
924+ exitReason <- fmt .Sprintf ("auth succeeded after %d checks" , count )
917925 return // Loop should exit when auth succeeds
918926 }
919927
920928 // Simulate clearing auth error on 3rd attempt
921- if checkCount >= 3 {
929+ if count >= 3 {
922930 app .mu .Lock ()
923931 app .authError = ""
924932 app .mu .Unlock ()
@@ -931,8 +939,14 @@ func TestAuthRetryLoopStopsOnSuccess(t *testing.T) {
931939 select {
932940 case <- done :
933941 // Success - loop exited
934- case <- time .After (1 * time .Second ):
935- t .Fatal ("Auth retry loop did not stop after auth succeeded" )
942+ reason := "unknown"
943+ select {
944+ case reason = <- exitReason :
945+ default :
946+ }
947+ t .Logf ("Goroutine exited: %s" , reason )
948+ case <- time .After (2 * time .Second ):
949+ t .Fatalf ("Auth retry loop did not stop after auth succeeded (checkCount=%d)" , checkCount .Load ())
936950 }
937951
938952 // Verify auth error was cleared
@@ -944,8 +958,9 @@ func TestAuthRetryLoopStopsOnSuccess(t *testing.T) {
944958 t .Errorf ("Expected auth error to be cleared, got: %s" , finalError )
945959 }
946960
947- if checkCount < 3 {
948- t .Errorf ("Expected at least 3 retry attempts, got: %d" , checkCount )
961+ finalCount := checkCount .Load ()
962+ if finalCount < 3 {
963+ t .Errorf ("Expected at least 3 retry attempts, got: %d" , finalCount )
949964 }
950965}
951966
0 commit comments