xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / core / bus_test.go

3.4 KB
1package core
2
3import (
4	"testing"
5	"time"
6)
7
8func TestEventBus(t *testing.T) {
9	bus := NewEventBus(10)
10
11	// Subscribe
12	ch := bus.Subscribe()
13
14	// Publish event
15	event := Event{
16		Type:      EventMysisCreated,
17		MysisID:   "test-id",
18		MysisName: "test-mysis",
19		Timestamp: time.Now(),
20	}
21	bus.Publish(event)
22
23	// Receive event
24	select {
25	case received := <-ch:
26		if received.Type != EventMysisCreated {
27			t.Errorf("expected type=%s, got %s", EventMysisCreated, received.Type)
28		}
29		if received.MysisID != "test-id" {
30			t.Errorf("expected mysis_id=test-id, got %s", received.MysisID)
31		}
32	case <-time.After(100 * time.Millisecond):
33		t.Fatal("timeout waiting for event")
34	}
35
36	// Unsubscribe
37	bus.Unsubscribe(ch)
38
39	// Channel should be closed
40	_, ok := <-ch
41	if ok {
42		t.Error("expected channel to be closed")
43	}
44}
45
46func TestEventBusMultipleSubscribers(t *testing.T) {
47	bus := NewEventBus(10)
48
49	ch1 := bus.Subscribe()
50	ch2 := bus.Subscribe()
51
52	event := Event{
53		Type:      EventMysisMessage,
54		MysisID:   "mysis-1",
55		Timestamp: time.Now(),
56	}
57	bus.Publish(event)
58
59	// Both should receive
60	select {
61	case <-ch1:
62	case <-time.After(100 * time.Millisecond):
63		t.Fatal("ch1 timeout")
64	}
65
66	select {
67	case <-ch2:
68	case <-time.After(100 * time.Millisecond):
69		t.Fatal("ch2 timeout")
70	}
71
72	bus.Close()
73}
74
75func TestEventBusNonBlocking(t *testing.T) {
76	// Small buffer
77	bus := NewEventBus(1)
78	ch := bus.Subscribe()
79
80	for len(ch) < cap(ch) {
81		bus.Publish(Event{Type: EventMysisCreated})
82	}
83
84	// This should not block (event dropped)
85	done := make(chan bool)
86	go func() {
87		bus.Publish(Event{Type: EventMysisDeleted})
88		done <- true
89	}()
90
91	select {
92	case <-done:
93		// Good, didn't block
94	case <-time.After(100 * time.Millisecond):
95		t.Fatal("publish blocked")
96	}
97
98	// Drain the buffer
99	<-ch
100	bus.Close()
101}
102
103func TestEventBusPublishBlockingDelivers(t *testing.T) {
104	bus := NewEventBus(1)
105	ch := bus.Subscribe()
106
107	for len(ch) < cap(ch) {
108		bus.Publish(Event{Type: EventMysisCreated})
109	}
110
111	resultCh := make(chan bool, 1)
112	go func() {
113		resultCh <- bus.PublishBlocking(Event{Type: EventMysisDeleted}, 100*time.Millisecond)
114	}()
115
116	select {
117	case <-resultCh:
118		t.Fatal("expected PublishBlocking to wait for buffer space")
119	case <-time.After(20 * time.Millisecond):
120	}
121
122	<-ch
123
124	select {
125	case delivered := <-resultCh:
126		if !delivered {
127			t.Fatal("expected PublishBlocking to deliver")
128		}
129	case <-time.After(200 * time.Millisecond):
130		t.Fatal("timeout waiting for PublishBlocking")
131	}
132
133	deletedFound := false
134	for i := 0; i < cap(ch); i++ {
135		received := <-ch
136		if received.Type == EventMysisDeleted {
137			deletedFound = true
138		}
139	}
140	if !deletedFound {
141		t.Fatal("expected to find blocking event")
142	}
143
144	bus.Close()
145}
146
147func TestEventBusPublishBlockingTimeout(t *testing.T) {
148	bus := NewEventBus(1)
149	ch := bus.Subscribe()
150
151	for len(ch) < cap(ch) {
152		bus.Publish(Event{Type: EventMysisCreated})
153	}
154
155	start := time.Now()
156	delivered := bus.PublishBlocking(Event{Type: EventMysisDeleted}, 20*time.Millisecond)
157	if delivered {
158		t.Fatal("expected PublishBlocking to timeout")
159	}
160	if time.Since(start) < 15*time.Millisecond {
161		t.Fatal("expected PublishBlocking to wait for timeout")
162	}
163
164	<-ch
165	bus.Close()
166}
167
168func TestEventBusClose(t *testing.T) {
169	bus := NewEventBus(10)
170
171	ch1 := bus.Subscribe()
172	ch2 := bus.Subscribe()
173
174	bus.Close()
175
176	// Both channels should be closed
177	_, ok1 := <-ch1
178	_, ok2 := <-ch2
179
180	if ok1 || ok2 {
181		t.Error("expected all channels to be closed")
182	}
183}