-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus_test.go
182 lines (144 loc) · 3.6 KB
/
bus_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package membus
import (
"context"
"log/slog"
"strconv"
"sync"
"testing"
"time"
)
func TestNewInstance(t *testing.T) {
logger := slog.Default()
bus := NewInstance(logger)
if bus == nil {
t.Fatal("NewInstance returned nil")
}
if bus.queues == nil {
t.Error("queues map not initialized")
}
if bus.msgCh == nil {
t.Error("message channel not initialized")
}
if bus.log == nil {
t.Error("logger not initialized")
}
}
func TestSubscribe(t *testing.T) {
logger := slog.Default()
bus := NewInstance(logger)
type TestMessage struct {
Data string
}
Subscribe(bus, "TestService", "test.event", func(ctx context.Context, msg TestMessage) error {
return nil
})
queues, exists := bus.queues["test.event"]
if !exists {
t.Fatal("event queue not created")
}
if len(queues) != 1 {
t.Fatal("expected 1 queue, got", len(queues))
}
if queues[0].service != "TestService" {
t.Fatal("wrong service name")
}
}
func TestMultipleSubscribers(t *testing.T) {
logger := slog.Default()
bus := NewInstance(logger)
type TestMessage struct {
Data string
}
Subscribe(bus, "Service1", "test.event", func(ctx context.Context, msg TestMessage) error {
return nil
})
Subscribe(bus, "Service2", "test.event", func(ctx context.Context, msg TestMessage) error {
return nil
})
queues, exists := bus.queues["test.event"]
if !exists {
t.Fatal("event queue not created")
}
if len(queues) != 2 {
t.Fatal("expected 2 queues, got", len(queues))
}
}
func TestPublishAndReceive(t *testing.T) {
logger := slog.Default()
bus := NewInstance(logger)
type TestMessage struct {
Data string
}
var wg sync.WaitGroup
wg.Add(1)
receivedData := ""
Subscribe(bus, "TestService", "test.event", func(ctx context.Context, msg TestMessage) error {
defer wg.Done()
receivedData = msg.Data
return nil
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
go bus.Start(ctx)
testMsg := TestMessage{Data: "test data"}
bus.Publish(ctx, "test.event", testMsg)
wg.Wait()
if receivedData != testMsg.Data {
t.Errorf("expected to receive %q, got %q", testMsg.Data, receivedData)
}
}
func TestContextCancellation(t *testing.T) {
logger := slog.Default()
bus := NewInstance(logger)
type TestMessage struct {
Data string
}
processed := false
Subscribe(bus, "TestService", "test.event", func(ctx context.Context, msg TestMessage) error {
processed = true
return nil
})
ctx, cancel := context.WithCancel(context.Background())
go bus.Start(ctx)
// Cancel context immediately
cancel()
// Try to publish after cancellation
bus.Publish(ctx, "test.event", TestMessage{Data: "test"})
// Give some time for potential processing
time.Sleep(100 * time.Millisecond)
if processed {
t.Error("message was processed after context cancellation")
}
}
func TestConcurrentPublish(t *testing.T) {
logger := slog.Default()
bus := NewInstance(logger)
type TestMessage struct {
Data string
}
messageCount := 100
var wg sync.WaitGroup
wg.Add(messageCount)
receivedCount := 0
var mu sync.Mutex
Subscribe(bus, "TestService", "test.event", func(ctx context.Context, msg TestMessage) error {
mu.Lock()
defer mu.Unlock()
defer wg.Done()
receivedCount++
return nil
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
go bus.Start(ctx)
// Publish messages concurrently
for i := 0; i < messageCount; i++ {
go func(i int) {
bus.Publish(ctx, "test.event", TestMessage{Data: strconv.Itoa(i)})
}(i)
}
wg.Wait()
if receivedCount != messageCount {
t.Errorf("expected to receive %d messages, got %d", messageCount, receivedCount)
}
}