@@ -2,15 +2,51 @@ package tail
22
33import (
44 "context"
5- "fmt"
65 "io"
76 "os"
7+ "path/filepath"
88 "testing"
99 "time"
1010
1111 "github.com/stretchr/testify/require"
1212)
1313
14+ func TestBlockUntilExists (t * testing.T ) {
15+ watcherConfig := WatcherConfig {
16+ MinPollFrequency : 5 * time .Millisecond ,
17+ MaxPollFrequency : 5 * time .Millisecond ,
18+ }
19+
20+ t .Run ("should block until file exists" , func (t * testing.T ) {
21+ filename := filepath .Join (t .TempDir (), "eventually" )
22+
23+ go func () {
24+ time .Sleep (10 * time .Millisecond )
25+ createFileWithPath (t , filename , "" )
26+ }()
27+
28+ err := blockUntilExists (context .Background (), & Config {
29+ Filename : filename ,
30+ WatcherConfig : watcherConfig ,
31+ })
32+ require .NoError (t , err )
33+ })
34+
35+ t .Run ("should exit when context is canceled" , func (t * testing.T ) {
36+ ctx , cancel := context .WithCancel (context .Background ())
37+ go func () {
38+ time .Sleep (10 * time .Millisecond )
39+ cancel ()
40+ }()
41+
42+ err := blockUntilExists (ctx , & Config {
43+ Filename : filepath .Join (t .TempDir (), "never" ),
44+ WatcherConfig : watcherConfig ,
45+ })
46+ require .ErrorIs (t , err , context .Canceled )
47+ })
48+ }
49+
1450func TestBlockUntilEvent (t * testing.T ) {
1551 watcherConfig := WatcherConfig {
1652 MinPollFrequency : 5 * time .Millisecond ,
@@ -22,7 +58,7 @@ func TestBlockUntilEvent(t *testing.T) {
2258 defer f .Close ()
2359
2460 go func () {
25- time .Sleep (50 * time .Millisecond )
61+ time .Sleep (10 * time .Millisecond )
2662 _ , err := f .WriteString ("updated" )
2763 require .NoError (t , err )
2864 }()
@@ -40,7 +76,7 @@ func TestBlockUntilEvent(t *testing.T) {
4076 defer f .Close ()
4177
4278 go func () {
43- time .Sleep (50 * time .Millisecond )
79+ time .Sleep (10 * time .Millisecond )
4480 require .NoError (t , os .Chtimes (f .Name (), time .Now (), time .Now ()))
4581 }()
4682
@@ -57,7 +93,7 @@ func TestBlockUntilEvent(t *testing.T) {
5793 defer f .Close ()
5894
5995 go func () {
60- time .Sleep (50 * time .Millisecond )
96+ time .Sleep (10 * time .Millisecond )
6197 removeFile (t , f .Name ())
6298 }()
6399
@@ -91,9 +127,8 @@ func TestBlockUntilEvent(t *testing.T) {
91127 require .NoError (t , err )
92128
93129 go func () {
94- time .Sleep (50 * time .Millisecond )
95- err := f .Truncate (0 )
96- fmt .Println (err )
130+ time .Sleep (10 * time .Millisecond )
131+ require .NoError (t , f .Truncate (0 ))
97132 }()
98133
99134 event , err := blockUntilEvent (context .Background (), f , offset , & Config {
@@ -103,4 +138,40 @@ func TestBlockUntilEvent(t *testing.T) {
103138 require .NoError (t , err )
104139 require .Equal (t , eventTruncated , event )
105140 })
141+
142+ t .Run ("should exit when context is canceled" , func (t * testing.T ) {
143+ f := createEmptyFile (t , "startempty" )
144+ defer f .Close ()
145+
146+ ctx , cancel := context .WithCancel (context .Background ())
147+ go func () {
148+ time .Sleep (10 * time .Millisecond )
149+ cancel ()
150+ }()
151+
152+ event , err := blockUntilEvent (ctx , f , 0 , & Config {
153+ Filename : f .Name (),
154+ WatcherConfig : watcherConfig ,
155+ })
156+ require .ErrorIs (t , err , context .Canceled )
157+ require .Equal (t , eventNone , event )
158+ })
159+ }
160+
161+ func createEmptyFile (t * testing.T , name string ) * os.File {
162+ path := filepath .Join (t .TempDir (), name )
163+ f , err := os .Create (path )
164+ require .NoError (t , err )
165+ return f
166+ }
167+
168+ func createFileWithContent (t * testing.T , name , content string ) * os.File {
169+ path := createFile (t , name , content )
170+ f , err := os .OpenFile (path , os .O_RDWR , 0 )
171+ require .NoError (t , err )
172+ return f
173+ }
174+
175+ func createFileWithPath (t * testing.T , path , content string ) {
176+ require .NoError (t , os .WriteFile (path , []byte (content ), 0600 ))
106177}
0 commit comments