Skip to content

Commit 1fa3763

Browse files
committed
add tests for blockUntilExists
1 parent 5c99189 commit 1fa3763

File tree

3 files changed

+78
-22
lines changed

3 files changed

+78
-22
lines changed

internal/component/loki/source/file/internal/tail/block.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func blockUntilExists(ctx context.Context, cfg *Config) error {
2525
} else if !os.IsNotExist(err) {
2626
return err
2727
}
28-
2928
backoff.Wait()
3029
}
3130

internal/component/loki/source/file/internal/tail/block_test.go

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,51 @@ package tail
22

33
import (
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+
1450
func 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
}

internal/component/loki/source/file/internal/tail/file_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,6 @@ func createFile(t *testing.T, name, content string) string {
238238
return path
239239
}
240240

241-
func createEmptyFile(t *testing.T, name string) *os.File {
242-
path := t.TempDir() + "/" + name
243-
f, err := os.Create(path)
244-
require.NoError(t, err)
245-
return f
246-
}
247-
248-
func createFileWithContent(t *testing.T, name, content string) *os.File {
249-
path := createFile(t, name, content)
250-
f, err := os.OpenFile(path, os.O_RDWR, 0)
251-
require.NoError(t, err)
252-
return f
253-
}
254-
255241
func appendToFile(t *testing.T, name, content string) {
256242
f, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY, 0600)
257243
require.NoError(t, err)

0 commit comments

Comments
 (0)