|
| 1 | +package tail |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestBlockUntilEvent(t *testing.T) { |
| 15 | + watcherConfig := WatcherConfig{ |
| 16 | + MinPollFrequency: 5 * time.Millisecond, |
| 17 | + MaxPollFrequency: 5 * time.Millisecond, |
| 18 | + } |
| 19 | + |
| 20 | + t.Run("should return modified event when file is written to", func(t *testing.T) { |
| 21 | + f := createEmptyFile(t, "startempty") |
| 22 | + defer f.Close() |
| 23 | + |
| 24 | + go func() { |
| 25 | + time.Sleep(50 * time.Millisecond) |
| 26 | + _, err := f.WriteString("updated") |
| 27 | + require.NoError(t, err) |
| 28 | + }() |
| 29 | + |
| 30 | + event, err := blockUntilEvent(context.Background(), f, 0, &Config{ |
| 31 | + Filename: f.Name(), |
| 32 | + WatcherConfig: watcherConfig, |
| 33 | + }) |
| 34 | + require.NoError(t, err) |
| 35 | + require.Equal(t, eventModified, event) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("should return modified event if mod time is updated", func(t *testing.T) { |
| 39 | + f := createEmptyFile(t, "startempty") |
| 40 | + defer f.Close() |
| 41 | + |
| 42 | + go func() { |
| 43 | + time.Sleep(50 * time.Millisecond) |
| 44 | + require.NoError(t, os.Chtimes(f.Name(), time.Now(), time.Now())) |
| 45 | + }() |
| 46 | + |
| 47 | + event, err := blockUntilEvent(context.Background(), f, 0, &Config{ |
| 48 | + Filename: f.Name(), |
| 49 | + WatcherConfig: watcherConfig, |
| 50 | + }) |
| 51 | + require.NoError(t, err) |
| 52 | + require.Equal(t, eventModified, event) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("should return deleted event if file is deleted", func(t *testing.T) { |
| 56 | + f := createEmptyFile(t, "startempty") |
| 57 | + defer f.Close() |
| 58 | + |
| 59 | + go func() { |
| 60 | + time.Sleep(50 * time.Millisecond) |
| 61 | + removeFile(t, f.Name()) |
| 62 | + }() |
| 63 | + |
| 64 | + event, err := blockUntilEvent(context.Background(), f, 0, &Config{ |
| 65 | + Filename: f.Name(), |
| 66 | + WatcherConfig: watcherConfig, |
| 67 | + }) |
| 68 | + require.NoError(t, err) |
| 69 | + require.Equal(t, eventDeleted, event) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("should return deleted event if file is deleted before", func(t *testing.T) { |
| 73 | + f := createEmptyFile(t, "startempty") |
| 74 | + defer f.Close() |
| 75 | + |
| 76 | + removeFile(t, f.Name()) |
| 77 | + |
| 78 | + event, err := blockUntilEvent(context.Background(), f, 0, &Config{ |
| 79 | + Filename: f.Name(), |
| 80 | + WatcherConfig: watcherConfig, |
| 81 | + }) |
| 82 | + require.NoError(t, err) |
| 83 | + require.Equal(t, eventDeleted, event) |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("should return truncated event", func(t *testing.T) { |
| 87 | + f := createFileWithContent(t, "truncate", "content") |
| 88 | + defer f.Close() |
| 89 | + |
| 90 | + offset, err := f.Seek(0, io.SeekEnd) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + go func() { |
| 94 | + time.Sleep(50 * time.Millisecond) |
| 95 | + err := f.Truncate(0) |
| 96 | + fmt.Println(err) |
| 97 | + }() |
| 98 | + |
| 99 | + event, err := blockUntilEvent(context.Background(), f, offset, &Config{ |
| 100 | + Filename: f.Name(), |
| 101 | + WatcherConfig: watcherConfig, |
| 102 | + }) |
| 103 | + require.NoError(t, err) |
| 104 | + require.Equal(t, eventTruncated, event) |
| 105 | + }) |
| 106 | +} |
0 commit comments