|
| 1 | +package eventbus |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// Test basic publish/subscribe lifecycle using memory engine ensuring message receipt and stats increments. |
| 10 | +func TestEventBusPublishSubscribeBasic(t *testing.T) { |
| 11 | + m := NewModule().(*EventBusModule) |
| 12 | + app := newMockApp() |
| 13 | + // Register default config section as RegisterConfig would |
| 14 | + if err := m.RegisterConfig(app); err != nil { |
| 15 | + t.Fatalf("register config: %v", err) |
| 16 | + } |
| 17 | + if err := m.Init(app); err != nil { |
| 18 | + t.Fatalf("init: %v", err) |
| 19 | + } |
| 20 | + if err := m.Start(context.Background()); err != nil { |
| 21 | + t.Fatalf("start: %v", err) |
| 22 | + } |
| 23 | + defer m.Stop(context.Background()) |
| 24 | + |
| 25 | + received := make(chan struct{}, 1) |
| 26 | + _, err := m.Subscribe(context.Background(), "test.topic", func(ctx context.Context, e Event) error { |
| 27 | + if e.Topic != "test.topic" { |
| 28 | + t.Errorf("unexpected topic %s", e.Topic) |
| 29 | + } |
| 30 | + received <- struct{}{} |
| 31 | + return nil |
| 32 | + }) |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("subscribe: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + if err := m.Publish(context.Background(), "test.topic", "payload"); err != nil { |
| 38 | + t.Fatalf("publish: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + select { |
| 42 | + case <-received: |
| 43 | + case <-time.After(2 * time.Second): |
| 44 | + t.Fatal("timeout waiting for event delivery") |
| 45 | + } |
| 46 | + |
| 47 | + del, _ := m.Stats() |
| 48 | + if del == 0 { |
| 49 | + t.Fatalf("expected delivered stats > 0") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Test unsubscribe removes subscription and no further deliveries occur. |
| 54 | +func TestEventBusUnsubscribe(t *testing.T) { |
| 55 | + m := NewModule().(*EventBusModule) |
| 56 | + app := newMockApp() |
| 57 | + if err := m.RegisterConfig(app); err != nil { |
| 58 | + t.Fatalf("register config: %v", err) |
| 59 | + } |
| 60 | + if err := m.Init(app); err != nil { |
| 61 | + t.Fatalf("init: %v", err) |
| 62 | + } |
| 63 | + if err := m.Start(context.Background()); err != nil { |
| 64 | + t.Fatalf("start: %v", err) |
| 65 | + } |
| 66 | + defer m.Stop(context.Background()) |
| 67 | + |
| 68 | + count := 0 |
| 69 | + sub, err := m.Subscribe(context.Background(), "once.topic", func(ctx context.Context, e Event) error { count++; return nil }) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("subscribe: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + if err := m.Publish(context.Background(), "once.topic", 1); err != nil { |
| 75 | + t.Fatalf("publish1: %v", err) |
| 76 | + } |
| 77 | + time.Sleep(50 * time.Millisecond) |
| 78 | + if count != 1 { |
| 79 | + t.Fatalf("expected 1 delivery got %d", count) |
| 80 | + } |
| 81 | + |
| 82 | + if err := m.Unsubscribe(context.Background(), sub); err != nil { |
| 83 | + t.Fatalf("unsubscribe: %v", err) |
| 84 | + } |
| 85 | + if err := m.Publish(context.Background(), "once.topic", 2); err != nil { |
| 86 | + t.Fatalf("publish2: %v", err) |
| 87 | + } |
| 88 | + time.Sleep(50 * time.Millisecond) |
| 89 | + if count != 1 { |
| 90 | + t.Fatalf("expected no additional deliveries after unsubscribe") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Test async subscription processes events without blocking publisher. |
| 95 | +func TestEventBusAsyncSubscription(t *testing.T) { |
| 96 | + m := NewModule().(*EventBusModule) |
| 97 | + app := newMockApp() |
| 98 | + if err := m.RegisterConfig(app); err != nil { |
| 99 | + t.Fatalf("register config: %v", err) |
| 100 | + } |
| 101 | + if err := m.Init(app); err != nil { |
| 102 | + t.Fatalf("init: %v", err) |
| 103 | + } |
| 104 | + if err := m.Start(context.Background()); err != nil { |
| 105 | + t.Fatalf("start: %v", err) |
| 106 | + } |
| 107 | + defer m.Stop(context.Background()) |
| 108 | + |
| 109 | + received := make(chan struct{}, 1) |
| 110 | + _, err := m.SubscribeAsync(context.Background(), "async.topic", func(ctx context.Context, e Event) error { received <- struct{}{}; return nil }) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("subscribe async: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + start := time.Now() |
| 116 | + if err := m.Publish(context.Background(), "async.topic", 123); err != nil { |
| 117 | + t.Fatalf("publish: %v", err) |
| 118 | + } |
| 119 | + // We expect Publish to return quickly (well under 100ms) even if handler not yet executed. |
| 120 | + if time.Since(start) > 200*time.Millisecond { |
| 121 | + t.Fatalf("publish blocked unexpectedly long") |
| 122 | + } |
| 123 | + |
| 124 | + select { |
| 125 | + case <-received: |
| 126 | + case <-time.After(2 * time.Second): |
| 127 | + t.Fatal("timeout waiting for async delivery") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Removed local mockApp (reuse the one defined in module_test.go) |
0 commit comments