66 "path/filepath"
77 "runtime"
88 "sync"
9+ "sync/atomic"
910 "testing"
1011 "time"
1112
@@ -405,7 +406,7 @@ force_inotify: true`, testPattern),
405406 tc .afterConfigure ()
406407 }
407408
408- actualLines := 0
409+ var actualLines atomic. Int64
409410 var wg sync.WaitGroup
410411
411412 if tc .expectedLines != 0 {
@@ -416,11 +417,9 @@ force_inotify: true`, testPattern),
416417 for {
417418 select {
418419 case <- out :
419- actualLines ++
420+ actualLines . Add ( 1 )
420421 case <- tomb .Dying ():
421422 return
422- case <- time .After (100 * time .Millisecond ):
423- // avoid tight loop
424423 }
425424 }
426425 }()
@@ -451,16 +450,14 @@ force_inotify: true`, testPattern),
451450
452451 fd .Close ()
453452
454- // sleep to ensure the tail events are processed
455- time .Sleep (2 * time .Second )
453+ require .Eventually (t , func () bool {
454+ return actualLines .Load () == int64 (tc .expectedLines )
455+ }, 5 * time .Second , 100 * time .Millisecond )
456456
457457 os .Remove (streamLogFile )
458458
459- // stop acquisition and wait for tailer
460459 tomb .Kill (nil )
461460 wg .Wait ()
462-
463- assert .Equal (t , tc .expectedLines , actualLines )
464461 }
465462
466463 if tc .expectedOutput != "" {
@@ -588,9 +585,12 @@ mode: tail
588585 require .NoError (t , err )
589586
590587 // Wait for polling to detect the file
591- time .Sleep (4 * time .Second )
592588
593- require .True (t , f .IsTailing (testFile ), "File should be tailed after polling" )
589+ require .Eventually (t , func () bool {
590+ return f .IsTailing (testFile )
591+ }, 5 * time .Second , 100 * time .Millisecond , "File should be tailed after polling" )
592+
593+ // could be require.Never, but detection has triggered already - no need to slow down the test
594594 require .False (t , f .IsTailing (ignoredFile ), "File should be ignored after polling" )
595595
596596 // Cleanup
@@ -629,21 +629,19 @@ mode: tail
629629 require .NoError (t , err )
630630
631631 // Wait for initial tail setup
632- time .Sleep (100 * time .Millisecond )
632+ require .Eventually (t , func () bool {
633+ return f .IsTailing (testFile )
634+ }, 3 * time .Second , 100 * time .Millisecond , "File should be initially tailed" )
633635
634636 // Simulate tailer death by removing it from the map
635637 f .RemoveTail (testFile )
636- isTailed := f .IsTailing (testFile )
637- require .False (t , isTailed , "File should be removed from the map" )
638+ require .False (t , f .IsTailing (testFile ), "File should be removed from the map" )
638639
639- // Wait for polling to resurrect the file
640- time .Sleep (2 * time .Second )
640+ // Wait for polling to resurrect the tail
641+ require .Eventually (t , func () bool {
642+ return f .IsTailing (testFile )
643+ }, 5 * time .Second , 100 * time .Millisecond , "File should be resurrected via polling" )
641644
642- // Verify file is being tailed again
643- isTailed = f .IsTailing (testFile )
644- require .True (t , isTailed , "File should be resurrected via polling" )
645-
646- // Cleanup
647645 tomb .Kill (nil )
648646 require .NoError (t , tomb .Wait ())
649647}
0 commit comments