-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_for_test.go
317 lines (267 loc) · 8.31 KB
/
wait_for_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package realtime_pubsub
import (
"context"
"errors"
"log"
"sync"
"testing"
"time"
)
// MockEventEmitter is a mock implementation of EventEmitter.
type MockEventEmitter struct {
mu sync.RWMutex
listeners map[string][]ListenerFunc
}
func NewMockEventEmitter() *MockEventEmitter {
return &MockEventEmitter{
listeners: make(map[string][]ListenerFunc),
}
}
func (e *MockEventEmitter) On(event string, listener ListenerFunc) int {
e.mu.Lock()
defer e.mu.Unlock()
e.listeners[event] = append(e.listeners[event], listener)
// Return a dummy listener ID (not used in the mock)
return len(e.listeners[event]) - 1
}
func (e *MockEventEmitter) Off(event string, id int) {
e.mu.Lock()
defer e.mu.Unlock()
if listeners, ok := e.listeners[event]; ok {
if id >= 0 && id < len(listeners) {
// Remove the listener at index id
e.listeners[event] = append(listeners[:id], listeners[id+1:]...)
}
}
}
func (e *MockEventEmitter) Emit(event string, args ...interface{}) {
e.mu.RLock()
defer e.mu.RUnlock()
if listeners, ok := e.listeners[event]; ok {
for _, listener := range listeners {
// Call the listener in a separate goroutine to mimic asynchronous behavior
go listener(args...)
}
}
}
// MockClient is a mock implementation of Client.
type MockClient struct {
eventEmitter *MockEventEmitter
logger *log.Logger
}
func NewMockClient() *MockClient {
return &MockClient{
eventEmitter: NewMockEventEmitter(),
logger: log.New(log.Writer(), "mockclient: ", log.LstdFlags),
}
}
func (c *MockClient) On(event string, listener ListenerFunc) int {
return c.eventEmitter.On(event, listener)
}
func (c *MockClient) Off(event string, id int) {
c.eventEmitter.Off(event, id)
}
func (c *MockClient) WaitFor(eventName string, timeout time.Duration) (interface{}, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ch := make(chan interface{}, 1)
listenerID := c.On(eventName, func(args ...interface{}) {
select {
case ch <- args:
default:
}
})
defer c.Off(eventName, listenerID)
select {
case result := <-ch:
c.logger.Printf("Event '%s' received: %v", eventName, result)
return result, nil
case <-ctx.Done():
c.logger.Printf("Timeout waiting for event '%s'", eventName)
return nil, ctx.Err()
}
}
func (c *MockClient) Emit(eventName string, args ...interface{}) {
c.eventEmitter.Emit(eventName, args...)
}
// Ensure MockClient implements WaitForClient interface
var _ WaitForClient = (*MockClient)(nil)
// TestWaitForAckSuccess tests that WaitForAck successfully returns when the ack event is emitted.
func TestWaitForAckSuccess(t *testing.T) {
client := NewMockClient()
waitFor := &WaitFor{
client: client, // Now acceptable because MockClient implements WaitForClient
id: "test-id",
}
// Emit the ack event after a short delay
go func() {
time.Sleep(100 * time.Millisecond)
client.Emit("ack.test-id", "ack data")
}()
result, err := waitFor.WaitForAck(1 * time.Second)
if err != nil {
t.Errorf("Expected WaitForAck to succeed, but got error: %v", err)
}
args, ok := result.([]interface{})
if !ok || len(args) == 0 {
t.Errorf("Expected result to be non-empty []interface{}, got %v", result)
}
if args[0] != "ack data" {
t.Errorf("Expected ack data to be 'ack data', got '%v'", args[0])
}
}
// TestWaitForAckTimeout tests that WaitForAck times out if the ack event is not emitted.
func TestWaitForAckTimeout(t *testing.T) {
client := NewMockClient()
waitFor := &WaitFor{
client: client,
id: "test-id",
}
// Do not emit the ack event
result, err := waitFor.WaitForAck(200 * time.Millisecond)
if err == nil {
t.Errorf("Expected WaitForAck to timeout, but got result: %v", result)
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("Expected error to be context.DeadlineExceeded, got %v", err)
}
}
// TestWaitForReplySuccess tests that WaitForReply successfully returns when the response event is emitted.
func TestWaitForReplySuccess(t *testing.T) {
client := NewMockClient()
waitFor := &WaitFor{
client: client,
id: "reply-id",
}
// Emit the response event after a short delay
go func() {
time.Sleep(100 * time.Millisecond)
client.Emit("response.reply-id", map[string]interface{}{
"data": "reply data",
"id": "reply-id",
"status": "ok",
})
}()
result, err := waitFor.WaitForReply(1 * time.Second)
if err != nil {
t.Errorf("Expected WaitForReply to succeed, but got error: %v", err)
}
if result.Data() != "reply data" {
t.Errorf("Expected reply data to be 'reply data', got '%v'", result.Data())
}
if result.Id() != "reply-id" {
t.Errorf("Expected reply id to be 'reply-id', got '%v'", result.Id())
}
if result.Status() != "ok" {
t.Errorf("Expected reply status to be 'ok', got '%v'", result.Status())
}
}
// TestWaitForReplyTimeout tests that WaitForReply times out if the response event is not emitted.
func TestWaitForReplyTimeout(t *testing.T) {
client := NewMockClient()
waitFor := &WaitFor{
client: client,
id: "reply-id",
}
// Do not emit the response event
result, err := waitFor.WaitForReply(200 * time.Millisecond)
if err == nil {
t.Errorf("Expected WaitForReply to timeout, but got result: %v", result)
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("Expected error to be context.DeadlineExceeded, got %v", err)
}
}
// TestClientWaitForSuccess tests that Client.WaitFor successfully returns when the event is emitted.
func TestClientWaitForSuccess(t *testing.T) {
client := NewMockClient()
// Emit the event after a short delay
go func() {
time.Sleep(100 * time.Millisecond)
client.Emit("test.event", "event data")
}()
result, err := client.WaitFor("test.event", 1*time.Second)
if err != nil {
t.Errorf("Expected WaitFor to succeed, but got error: %v", err)
}
args, ok := result.([]interface{})
if !ok || len(args) == 0 {
t.Errorf("Expected result to be non-empty []interface{}, got %v", result)
}
if args[0] != "event data" {
t.Errorf("Expected event data to be 'event data', got '%v'", args[0])
}
}
// TestClientWaitForTimeout tests that Client.WaitFor times out if the event is not emitted.
func TestClientWaitForTimeout(t *testing.T) {
client := NewMockClient()
// Do not emit the event
result, err := client.WaitFor("test.event", 200*time.Millisecond)
if err == nil {
t.Errorf("Expected WaitFor to timeout, but got result: %v", result)
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("Expected error to be context.DeadlineExceeded, got %v", err)
}
}
// TestWaitForMultipleListeners tests that multiple listeners can wait for the same event.
func TestWaitForMultipleListeners(t *testing.T) {
client := NewMockClient()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
result, err := client.WaitFor("test.event", 1*time.Second)
if err != nil {
t.Errorf("Listener 1: Expected WaitFor to succeed, but got error: %v", err)
return
}
args, _ := result.([]interface{})
if args[0] != "event data" {
t.Errorf("Listener 1: Expected event data to be 'event data', got '%v'", args[0])
}
}()
go func() {
defer wg.Done()
result, err := client.WaitFor("test.event", 1*time.Second)
if err != nil {
t.Errorf("Listener 2: Expected WaitFor to succeed, but got error: %v", err)
return
}
args, _ := result.([]interface{})
if args[0] != "event data" {
t.Errorf("Listener 2: Expected event data to be 'event data', got '%v'", args[0])
}
}()
// Emit the event after a short delay
time.Sleep(100 * time.Millisecond)
client.Emit("test.event", "event data")
wg.Wait()
}
// TestWaitForEventReceivedAfterTimeout tests that events received after timeout are ignored.
func TestWaitForEventReceivedAfterTimeout(t *testing.T) {
client := NewMockClient()
resultCh := make(chan interface{})
errCh := make(chan error)
go func() {
result, err := client.WaitFor("test.event", 100*time.Millisecond)
if err != nil {
errCh <- err
return
}
resultCh <- result
}()
// Emit the event after the timeout
time.Sleep(200 * time.Millisecond)
client.Emit("test.event", "event data")
select {
case <-resultCh:
t.Errorf("Expected WaitFor to timeout, but it succeeded")
case err := <-errCh:
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("Expected error to be context.DeadlineExceeded, got %v", err)
}
case <-time.After(1 * time.Second):
t.Errorf("Test timed out waiting for WaitFor to return")
}
}