(t *testing.T)
| 367 | } |
| 368 | |
| 369 | func TestSession_On(t *testing.T) { |
| 370 | t.Run("multiple handlers all receive events", func(t *testing.T) { |
| 371 | session, cleanup := newTestSession() |
| 372 | defer cleanup() |
| 373 | |
| 374 | var wg sync.WaitGroup |
| 375 | wg.Add(3) |
| 376 | var received1, received2, received3 bool |
| 377 | session.On(func(event SessionEvent) { received1 = true; wg.Done() }) |
| 378 | session.On(func(event SessionEvent) { received2 = true; wg.Done() }) |
| 379 | session.On(func(event SessionEvent) { received3 = true; wg.Done() }) |
| 380 | |
| 381 | session.dispatchEvent(newTestEvent()) |
| 382 | wg.Wait() |
| 383 | |
| 384 | if !received1 || !received2 || !received3 { |
| 385 | t.Errorf("Expected all handlers to receive event, got received1=%v, received2=%v, received3=%v", |
| 386 | received1, received2, received3) |
| 387 | } |
| 388 | }) |
| 389 | |
| 390 | t.Run("unsubscribing one handler does not affect others", func(t *testing.T) { |
| 391 | session, cleanup := newTestSession() |
| 392 | defer cleanup() |
| 393 | |
| 394 | var count1, count2, count3 atomic.Int32 |
| 395 | var wg sync.WaitGroup |
| 396 | |
| 397 | wg.Add(3) |
| 398 | session.On(func(event SessionEvent) { count1.Add(1); wg.Done() }) |
| 399 | unsub2 := session.On(func(event SessionEvent) { count2.Add(1); wg.Done() }) |
| 400 | session.On(func(event SessionEvent) { count3.Add(1); wg.Done() }) |
| 401 | |
| 402 | // First event - all handlers receive it |
| 403 | session.dispatchEvent(newTestEvent()) |
| 404 | wg.Wait() |
| 405 | |
| 406 | // Unsubscribe handler 2 |
| 407 | unsub2() |
| 408 | |
| 409 | // Second event - only handlers 1 and 3 should receive it |
| 410 | wg.Add(2) |
| 411 | session.dispatchEvent(newTestEvent()) |
| 412 | wg.Wait() |
| 413 | |
| 414 | if count1.Load() != 2 { |
| 415 | t.Errorf("Expected handler 1 to receive 2 events, got %d", count1.Load()) |
| 416 | } |
| 417 | if count2.Load() != 1 { |
| 418 | t.Errorf("Expected handler 2 to receive 1 event (before unsubscribe), got %d", count2.Load()) |
| 419 | } |
| 420 | if count3.Load() != 2 { |
| 421 | t.Errorf("Expected handler 3 to receive 2 events, got %d", count3.Load()) |
| 422 | } |
| 423 | }) |
| 424 | |
| 425 | t.Run("calling unsubscribe multiple times is safe", func(t *testing.T) { |
| 426 | session, cleanup := newTestSession() |
nothing calls this directly
no test coverage detected
searching dependent graphs…