Skip to content

Commit 88f23ed

Browse files
committed
remove sleep
1 parent 5858cef commit 88f23ed

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

pkg/acquisition/modules/file/file_test.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"runtime"
88
"sync"
9+
"sync/atomic"
910
"testing"
1011
"time"
1112

@@ -404,7 +405,7 @@ force_inotify: true`, testPattern),
404405
tc.afterConfigure()
405406
}
406407

407-
actualLines := 0
408+
var actualLines atomic.Int64
408409
var wg sync.WaitGroup
409410

410411
if tc.expectedLines != 0 {
@@ -415,11 +416,9 @@ force_inotify: true`, testPattern),
415416
for {
416417
select {
417418
case <-out:
418-
actualLines++
419+
actualLines.Add(1)
419420
case <-tomb.Dying():
420421
return
421-
case <-time.After(100 * time.Millisecond):
422-
// avoid tight loop
423422
}
424423
}
425424
}()
@@ -450,16 +449,14 @@ force_inotify: true`, testPattern),
450449

451450
fd.Close()
452451

453-
// sleep to ensure the tail events are processed
454-
time.Sleep(2 * time.Second)
452+
require.Eventually(t, func() bool {
453+
return actualLines.Load() == int64(tc.expectedLines)
454+
}, 5*time.Second, 100*time.Millisecond)
455455

456456
os.Remove(streamLogFile)
457457

458-
// stop acquisition and wait for tailer
459458
tomb.Kill(nil)
460459
wg.Wait()
461-
462-
assert.Equal(t, tc.expectedLines, actualLines)
463460
}
464461

465462
if tc.expectedOutput != "" {
@@ -587,9 +584,12 @@ mode: tail
587584
require.NoError(t, err)
588585

589586
// Wait for polling to detect the file
590-
time.Sleep(4 * time.Second)
591587

592-
require.True(t, f.IsTailing(testFile), "File should be tailed after polling")
588+
require.Eventually(t, func() bool {
589+
return f.IsTailing(testFile)
590+
}, 5*time.Second, 100*time.Millisecond, "File should be tailed after polling")
591+
592+
// could be require.Never, but detection has triggered already - no need to slow down the test
593593
require.False(t, f.IsTailing(ignoredFile), "File should be ignored after polling")
594594

595595
// Cleanup
@@ -628,21 +628,19 @@ mode: tail
628628
require.NoError(t, err)
629629

630630
// Wait for initial tail setup
631-
time.Sleep(100 * time.Millisecond)
631+
require.Eventually(t, func() bool {
632+
return f.IsTailing(testFile)
633+
}, 3*time.Second, 100*time.Millisecond, "File should be initially tailed")
632634

633635
// Simulate tailer death by removing it from the map
634636
f.RemoveTail(testFile)
635-
isTailed := f.IsTailing(testFile)
636-
require.False(t, isTailed, "File should be removed from the map")
637+
require.False(t, f.IsTailing(testFile), "File should be removed from the map")
637638

638-
// Wait for polling to resurrect the file
639-
time.Sleep(2 * time.Second)
639+
// Wait for polling to resurrect the tail
640+
require.Eventually(t, func() bool {
641+
return f.IsTailing(testFile)
642+
}, 5*time.Second, 100*time.Millisecond, "File should be resurrected via polling")
640643

641-
// Verify file is being tailed again
642-
isTailed = f.IsTailing(testFile)
643-
require.True(t, isTailed, "File should be resurrected via polling")
644-
645-
// Cleanup
646644
tomb.Kill(nil)
647645
require.NoError(t, tomb.Wait())
648646
}

0 commit comments

Comments
 (0)