| 12 | ) |
| 13 | |
| 14 | func TestWatcher_Run(t *testing.T) { |
| 15 | t.Run("should execute watchingFunc and set ready", func(t *testing.T) { |
| 16 | executed := false |
| 17 | watcher := &Watcher{ |
| 18 | readyCh: make(chan struct{}), |
| 19 | watchingFunc: func() error { |
| 20 | executed = true |
| 21 | return nil |
| 22 | }, |
| 23 | isReady: false, |
| 24 | } |
| 25 | |
| 26 | err := watcher.Run() |
| 27 | |
| 28 | assert.NoError(t, err) |
| 29 | assert.True(t, executed, "watchingFunc should have been executed") |
| 30 | assert.True(t, watcher.isReady, "watcher should be marked as ready") |
| 31 | }) |
| 32 | |
| 33 | t.Run("should close readyCh when started", func(t *testing.T) { |
| 34 | watcher := &Watcher{ |
| 35 | readyCh: make(chan struct{}), |
| 36 | watchingFunc: func() error { |
| 37 | return nil |
| 38 | }, |
| 39 | isReady: false, |
| 40 | } |
| 41 | |
| 42 | go func() { |
| 43 | _ = watcher.Run() |
| 44 | }() |
| 45 | |
| 46 | // Wait for readyCh to be closed |
| 47 | select { |
| 48 | case <-watcher.readyCh: |
| 49 | // Success - channel was closed |
| 50 | case <-time.After(1 * time.Second): |
| 51 | t.Fatal("readyCh should have been closed") |
| 52 | } |
| 53 | }) |
| 54 | |
| 55 | t.Run("should return error from watchingFunc", func(t *testing.T) { |
| 56 | expectedErr := errors.New("watch error") |
| 57 | watcher := &Watcher{ |
| 58 | readyCh: make(chan struct{}), |
| 59 | watchingFunc: func() error { |
| 60 | return expectedErr |
| 61 | }, |
| 62 | isReady: false, |
| 63 | } |
| 64 | |
| 65 | err := watcher.Run() |
| 66 | |
| 67 | assert.ErrorIs(t, err, expectedErr) |
| 68 | }) |
| 69 | |
| 70 | t.Run("should be idempotent - calling Run twice should not execute twice", func(t *testing.T) { |
| 71 | callCount := 0 |