| 294 | } |
| 295 | |
| 296 | func TestWatcher_Integration(t *testing.T) { |
| 297 | t.Run("typical usage pattern", func(t *testing.T) { |
| 298 | messageReceived := make(chan string, 1) |
| 299 | watcher := &Watcher{ |
| 300 | readyCh: make(chan struct{}), |
| 301 | watchingFunc: func() error { |
| 302 | // Simulate receiving a message |
| 303 | time.Sleep(50 * time.Millisecond) |
| 304 | messageReceived <- "test message" |
| 305 | return nil |
| 306 | }, |
| 307 | isReady: false, |
| 308 | } |
| 309 | |
| 310 | // Start watcher |
| 311 | var wg sync.WaitGroup |
| 312 | wg.Add(1) |
| 313 | go func() { |
| 314 | defer wg.Done() |
| 315 | err := watcher.Run() |
| 316 | assert.NoError(t, err) |
| 317 | }() |
| 318 | |
| 319 | // Wait for ready |
| 320 | err := watcher.WaitReady(1 * time.Second) |
| 321 | require.NoError(t, err) |
| 322 | |
| 323 | // Wait for message |
| 324 | select { |
| 325 | case msg := <-messageReceived: |
| 326 | assert.Equal(t, "test message", msg) |
| 327 | case <-time.After(2 * time.Second): |
| 328 | t.Fatal("should have received message") |
| 329 | } |
| 330 | |
| 331 | wg.Wait() |
| 332 | }) |
| 333 | |
| 334 | t.Run("watcher with context cancellation", func(t *testing.T) { |
| 335 | ctx, cancel := context.WithCancel(context.Background()) |
| 336 | defer cancel() |
| 337 | |
| 338 | started := make(chan struct{}) |
| 339 | watcher := &Watcher{ |
| 340 | readyCh: make(chan struct{}), |
| 341 | watchingFunc: func() error { |
| 342 | close(started) |
| 343 | <-ctx.Done() |
| 344 | return ctx.Err() |
| 345 | }, |
| 346 | isReady: false, |
| 347 | } |
| 348 | |
| 349 | var wg sync.WaitGroup |
| 350 | var runErr error |
| 351 | |
| 352 | wg.Add(1) |
| 353 | go func() { |