Skip to content

Commit c9c25d8

Browse files
committed
remove sleep
1 parent b3e4a49 commit c9c25d8

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

@@ -401,7 +402,7 @@ force_inotify: true`, testPattern),
401402
tc.afterConfigure()
402403
}
403404

404-
actualLines := 0
405+
var actualLines atomic.Int64
405406
var wg sync.WaitGroup
406407

407408
if tc.expectedLines != 0 {
@@ -412,11 +413,9 @@ force_inotify: true`, testPattern),
412413
for {
413414
select {
414415
case <-out:
415-
actualLines++
416+
actualLines.Add(1)
416417
case <-tomb.Dying():
417418
return
418-
case <-time.After(100 * time.Millisecond):
419-
// avoid tight loop
420419
}
421420
}
422421
}()
@@ -447,16 +446,14 @@ force_inotify: true`, testPattern),
447446

448447
fd.Close()
449448

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

453453
os.Remove(streamLogFile)
454454

455-
// stop acquisition and wait for tailer
456455
tomb.Kill(nil)
457456
wg.Wait()
458-
459-
assert.Equal(t, tc.expectedLines, actualLines)
460457
}
461458

462459
if tc.expectedOutput != "" {
@@ -580,9 +577,12 @@ mode: tail
580577
require.NoError(t, err)
581578

582579
// Wait for polling to detect the file
583-
time.Sleep(4 * time.Second)
584580

585-
require.True(t, f.IsTailing(testFile), "File should be tailed after polling")
581+
require.Eventually(t, func() bool {
582+
return f.IsTailing(testFile)
583+
}, 5*time.Second, 100*time.Millisecond, "File should be tailed after polling")
584+
585+
// could be require.Never, but detection has triggered already - no need to slow down the test
586586
require.False(t, f.IsTailing(ignoredFile), "File should be ignored after polling")
587587

588588
// Cleanup
@@ -621,21 +621,19 @@ mode: tail
621621
require.NoError(t, err)
622622

623623
// Wait for initial tail setup
624-
time.Sleep(100 * time.Millisecond)
624+
require.Eventually(t, func() bool {
625+
return f.IsTailing(testFile)
626+
}, 3*time.Second, 100*time.Millisecond, "File should be initially tailed")
625627

626628
// Simulate tailer death by removing it from the map
627629
f.RemoveTail(testFile)
628-
isTailed := f.IsTailing(testFile)
629-
require.False(t, isTailed, "File should be removed from the map")
630+
require.False(t, f.IsTailing(testFile), "File should be removed from the map")
630631

631-
// Wait for polling to resurrect the file
632-
time.Sleep(2 * time.Second)
632+
// Wait for polling to resurrect the tail
633+
require.Eventually(t, func() bool {
634+
return f.IsTailing(testFile)
635+
}, 5*time.Second, 100*time.Millisecond, "File should be resurrected via polling")
633636

634-
// Verify file is being tailed again
635-
isTailed = f.IsTailing(testFile)
636-
require.True(t, isTailed, "File should be resurrected via polling")
637-
638-
// Cleanup
639637
tomb.Kill(nil)
640638
require.NoError(t, tomb.Wait())
641639
}

0 commit comments

Comments
 (0)