|
| 1 | +/* |
| 2 | +Copyright 2023 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package fswatcher |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "runtime" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + clocktesting "k8s.io/utils/clock/testing" |
| 29 | + |
| 30 | + "github.com/dapr/kit/events/batcher" |
| 31 | + "github.com/dapr/kit/ptr" |
| 32 | +) |
| 33 | + |
| 34 | +func TestFSWatcher(t *testing.T) { |
| 35 | + runWatcher := func(t *testing.T, opts Options, bacher *batcher.Batcher[string]) <-chan struct{} { |
| 36 | + t.Helper() |
| 37 | + |
| 38 | + f, err := New(opts) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + if bacher != nil { |
| 42 | + f.WithBatcher(bacher) |
| 43 | + } |
| 44 | + |
| 45 | + errCh := make(chan error) |
| 46 | + ctx, cancel := context.WithCancel(context.Background()) |
| 47 | + eventsCh := make(chan struct{}) |
| 48 | + go func() { |
| 49 | + errCh <- f.Run(ctx, eventsCh) |
| 50 | + }() |
| 51 | + |
| 52 | + t.Cleanup(func() { |
| 53 | + cancel() |
| 54 | + select { |
| 55 | + case err := <-errCh: |
| 56 | + require.NoError(t, err) |
| 57 | + case <-time.After(time.Second): |
| 58 | + assert.Fail(t, "timeout waiting for watcher to stop") |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + assert.Eventually(t, f.running.Load, time.Second, time.Millisecond*10) |
| 63 | + return eventsCh |
| 64 | + } |
| 65 | + |
| 66 | + t.Run("creating fswatcher with no directory should not error", func(t *testing.T) { |
| 67 | + runWatcher(t, Options{}, nil) |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("creating fswatcher with 0 interval should not error", func(t *testing.T) { |
| 71 | + _, err := New(Options{ |
| 72 | + Interval: ptr.Of(time.Duration(0)), |
| 73 | + }) |
| 74 | + require.NoError(t, err) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("creating fswatcher with negative interval should error", func(t *testing.T) { |
| 78 | + _, err := New(Options{ |
| 79 | + Interval: ptr.Of(time.Duration(-1)), |
| 80 | + }) |
| 81 | + require.Error(t, err) |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("running Run twice should error", func(t *testing.T) { |
| 85 | + fs, err := New(Options{}) |
| 86 | + require.NoError(t, err) |
| 87 | + ctx, cancel := context.WithCancel(context.Background()) |
| 88 | + cancel() |
| 89 | + require.NoError(t, fs.Run(ctx, make(chan struct{}))) |
| 90 | + require.Error(t, fs.Run(ctx, make(chan struct{}))) |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("creating fswatcher with non-existent directory should error", func(t *testing.T) { |
| 94 | + dir := t.TempDir() |
| 95 | + require.NoError(t, os.RemoveAll(dir)) |
| 96 | + _, err := New(Options{ |
| 97 | + Targets: []string{dir}, |
| 98 | + }) |
| 99 | + require.Error(t, err) |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("should fire event when event occurs on target file", func(t *testing.T) { |
| 103 | + fp := filepath.Join(t.TempDir(), "test.txt") |
| 104 | + require.NoError(t, os.WriteFile(fp, []byte{}, 0o644)) |
| 105 | + eventsCh := runWatcher(t, Options{ |
| 106 | + Targets: []string{fp}, |
| 107 | + Interval: ptr.Of(time.Duration(1)), |
| 108 | + }, nil) |
| 109 | + assert.Empty(t, eventsCh) |
| 110 | + |
| 111 | + if runtime.GOOS == "windows" { |
| 112 | + // If running in windows, wait for notify to be ready. |
| 113 | + time.Sleep(time.Second) |
| 114 | + } |
| 115 | + require.NoError(t, os.WriteFile(fp, []byte{}, 0o644)) |
| 116 | + |
| 117 | + select { |
| 118 | + case <-eventsCh: |
| 119 | + case <-time.After(time.Second): |
| 120 | + assert.Fail(t, "timeout waiting for event") |
| 121 | + } |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("should fire 2 events when event occurs on 2 file target", func(t *testing.T) { |
| 125 | + fp1 := filepath.Join(t.TempDir(), "test.txt") |
| 126 | + fp2 := filepath.Join(t.TempDir(), "test.txt") |
| 127 | + require.NoError(t, os.WriteFile(fp1, []byte{}, 0o644)) |
| 128 | + require.NoError(t, os.WriteFile(fp2, []byte{}, 0o644)) |
| 129 | + eventsCh := runWatcher(t, Options{ |
| 130 | + Targets: []string{fp1, fp2}, |
| 131 | + Interval: ptr.Of(time.Duration(1)), |
| 132 | + }, nil) |
| 133 | + assert.Empty(t, eventsCh) |
| 134 | + require.NoError(t, os.WriteFile(fp1, []byte{}, 0o644)) |
| 135 | + require.NoError(t, os.WriteFile(fp2, []byte{}, 0o644)) |
| 136 | + for i := 0; i < 2; i++ { |
| 137 | + select { |
| 138 | + case <-eventsCh: |
| 139 | + case <-time.After(time.Second): |
| 140 | + assert.Fail(t, "timeout waiting for event") |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | + |
| 145 | + t.Run("should fire 2 events when event occurs on 2 files inside target directory", func(t *testing.T) { |
| 146 | + dir := t.TempDir() |
| 147 | + fp1 := filepath.Join(dir, "test1.txt") |
| 148 | + fp2 := filepath.Join(dir, "test2.txt") |
| 149 | + require.NoError(t, os.WriteFile(fp1, []byte{}, 0o644)) |
| 150 | + require.NoError(t, os.WriteFile(fp2, []byte{}, 0o644)) |
| 151 | + eventsCh := runWatcher(t, Options{ |
| 152 | + Targets: []string{fp1, fp2}, |
| 153 | + Interval: ptr.Of(time.Duration(1)), |
| 154 | + }, nil) |
| 155 | + if runtime.GOOS == "windows" { |
| 156 | + // If running in windows, wait for notify to be ready. |
| 157 | + time.Sleep(time.Second) |
| 158 | + } |
| 159 | + assert.Empty(t, eventsCh) |
| 160 | + require.NoError(t, os.WriteFile(fp1, []byte{}, 0o644)) |
| 161 | + require.NoError(t, os.WriteFile(fp2, []byte{}, 0o644)) |
| 162 | + for i := 0; i < 2; i++ { |
| 163 | + select { |
| 164 | + case <-eventsCh: |
| 165 | + case <-time.After(time.Second): |
| 166 | + assert.Fail(t, "timeout waiting for event") |
| 167 | + } |
| 168 | + } |
| 169 | + }) |
| 170 | + |
| 171 | + t.Run("should fire 2 events when event occurs on 2 target directories", func(t *testing.T) { |
| 172 | + dir1 := t.TempDir() |
| 173 | + dir2 := t.TempDir() |
| 174 | + fp1 := filepath.Join(dir1, "test1.txt") |
| 175 | + fp2 := filepath.Join(dir2, "test2.txt") |
| 176 | + eventsCh := runWatcher(t, Options{ |
| 177 | + Targets: []string{dir1, dir2}, |
| 178 | + Interval: ptr.Of(time.Duration(1)), |
| 179 | + }, nil) |
| 180 | + assert.Empty(t, eventsCh) |
| 181 | + require.NoError(t, os.WriteFile(fp1, []byte{}, 0o644)) |
| 182 | + require.NoError(t, os.WriteFile(fp2, []byte{}, 0o644)) |
| 183 | + for i := 0; i < 2; i++ { |
| 184 | + select { |
| 185 | + case <-eventsCh: |
| 186 | + case <-time.After(time.Second): |
| 187 | + assert.Fail(t, "timeout waiting for event") |
| 188 | + } |
| 189 | + } |
| 190 | + }) |
| 191 | + |
| 192 | + t.Run("should batch events of the same file for multiple events", func(t *testing.T) { |
| 193 | + clock := clocktesting.NewFakeClock(time.Time{}) |
| 194 | + batcher := batcher.New[string](time.Millisecond * 500) |
| 195 | + batcher.WithClock(clock) |
| 196 | + dir1 := t.TempDir() |
| 197 | + dir2 := t.TempDir() |
| 198 | + fp1 := filepath.Join(dir1, "test1.txt") |
| 199 | + fp2 := filepath.Join(dir2, "test2.txt") |
| 200 | + eventsCh := runWatcher(t, Options{Targets: []string{dir1, dir2}}, batcher) |
| 201 | + assert.Empty(t, eventsCh) |
| 202 | + |
| 203 | + if runtime.GOOS == "windows" { |
| 204 | + // If running in windows, wait for notify to be ready. |
| 205 | + time.Sleep(time.Second) |
| 206 | + } |
| 207 | + |
| 208 | + for i := 0; i < 10; i++ { |
| 209 | + require.NoError(t, os.WriteFile(fp1, []byte{}, 0o644)) |
| 210 | + require.NoError(t, os.WriteFile(fp2, []byte{}, 0o644)) |
| 211 | + } |
| 212 | + |
| 213 | + assert.Eventually(t, clock.HasWaiters, time.Second, time.Millisecond*10) |
| 214 | + |
| 215 | + select { |
| 216 | + case <-eventsCh: |
| 217 | + assert.Fail(t, "unexpected event") |
| 218 | + case <-time.After(time.Millisecond * 10): |
| 219 | + } |
| 220 | + |
| 221 | + clock.Step(time.Millisecond * 250) |
| 222 | + |
| 223 | + for i := 0; i < 10; i++ { |
| 224 | + require.NoError(t, os.WriteFile(fp1, []byte{}, 0o644)) |
| 225 | + require.NoError(t, os.WriteFile(fp2, []byte{}, 0o644)) |
| 226 | + } |
| 227 | + |
| 228 | + select { |
| 229 | + case <-eventsCh: |
| 230 | + assert.Fail(t, "unexpected event") |
| 231 | + case <-time.After(time.Millisecond * 10): |
| 232 | + } |
| 233 | + |
| 234 | + assert.Eventually(t, clock.HasWaiters, time.Second, time.Millisecond*10) |
| 235 | + clock.Step(time.Millisecond * 500) |
| 236 | + |
| 237 | + for i := 0; i < 2; i++ { |
| 238 | + select { |
| 239 | + case <-eventsCh: |
| 240 | + case <-time.After(time.Second): |
| 241 | + assert.Fail(t, "timeout waiting for event") |
| 242 | + } |
| 243 | + clock.Step(1) |
| 244 | + } |
| 245 | + }) |
| 246 | +} |
0 commit comments