-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
215 lines (174 loc) · 6.06 KB
/
options_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
package emitter
import (
"errors"
"sync"
"testing"
)
// TestWithErrorHandler tests that the custom error handler is called on error.
func TestWithErrorHandler(t *testing.T) {
// Define a variable to determine if the custom error handler was called.
var handlerCalled bool
// Define a custom error to be returned by a listener.
customError := errors.New("custom error")
// Define a custom error handler that sets handlerCalled to true.
customErrorHandler := func(event Event, err error) error {
if errors.Is(err, customError) {
handlerCalled = true
t.Logf("Custom error handler called with event: %s and error: %s", event.Topic(), err.Error())
}
return nil // Returning nil to indicate the error is handled.
}
// Create a new MemoryEmitter with the custom error handler.
emitter := NewMemoryEmitter(WithErrorHandler(customErrorHandler))
// Define a listener that returns the custom error.
listener := func(e Event) error {
return customError
}
// Subscribe the listener to a topic.
_, err := emitter.On("testTopic", listener)
if err != nil {
t.Fatalf("On() failed with error: %v", err)
}
// Emit the event synchronously to trigger the error.
emitter.EmitSync("testTopic", NewBaseEvent("testTopic", "testPayload"))
// Check if the custom error handler was called.
if !handlerCalled {
t.Fatalf("Custom error handler was not called on listener error")
}
}
func TestWithErrorHandlerAsync(t *testing.T) {
// Define a variable to determine if the custom error handler was called.
var handlerCalled bool
var handlerMutex sync.Mutex // To safely update handlerCalled from different goroutines
// Define a custom error to be returned by a listener.
customError := errors.New("custom error")
// Define a custom error handler that sets handlerCalled to true.
customErrorHandler := func(event Event, err error) error {
handlerMutex.Lock()
defer handlerMutex.Unlock()
if errors.Is(err, customError) {
handlerCalled = true
}
return nil // Assume the error is handled and return nil.
}
// Create a new MemoryEmitter with the custom error handler.
emitter := NewMemoryEmitter(WithErrorHandler(customErrorHandler))
// Define a listener that returns the custom error.
listener := func(e Event) error {
return customError
}
// Subscribe the listener to a topic.
_, err := emitter.On("testTopic", listener)
if err != nil {
t.Fatalf("On() failed with error: %v", err)
}
// Emit the event asynchronously to trigger the error.
errChan := emitter.Emit("testTopic", NewBaseEvent("testTopic", "testPayload"))
// Wait for all errors to be processed.
for err := range errChan {
if err != nil {
t.Errorf("Expected nil error due to custom handler, got: %v", err)
}
}
// Check if the custom error handler was called.
handlerMutex.Lock()
wasHandlerCalled := handlerCalled
handlerMutex.Unlock()
if !wasHandlerCalled {
t.Fatalf("Custom error handler was not called on listener error")
}
}
func TestWithPanicHandlerSync(t *testing.T) {
// Flag to indicate panic handler invocation
var panicHandlerInvoked bool
// Define a custom panic handler
customPanicHandler := func(p interface{}) {
if p == "test panic" {
panicHandlerInvoked = true
}
}
// Create a new MemoryEmitter with the custom panic handler.
emitter := NewMemoryEmitter(WithPanicHandler(customPanicHandler))
// Define a listener that panics
listener := func(e Event) error {
panic("test panic")
}
// Subscribe the listener to a topic.
_, err := emitter.On("testTopic", listener)
if err != nil {
t.Fatalf("On() failed with error: %v", err)
}
// Recover from panic to prevent test failure
defer func() {
if r := recover(); r != nil {
// This is expected
t.Logf("Recovered from panic: %v", r)
}
}()
// Emit the event synchronously to trigger the panic.
emitter.EmitSync("testTopic", "testPayload")
// Verify that the custom panic handler was invoked
if !panicHandlerInvoked {
t.Fatalf("Custom panic handler was not called on listener panic")
}
}
func TestWithPanicHandlerAsync(t *testing.T) {
// Flag to indicate panic handler invocation
var panicHandlerInvoked bool
var panicHandlerMutex sync.Mutex // To safely update panicHandlerInvoked from different goroutines
// Define a custom panic handler
customPanicHandler := func(p interface{}) {
panicHandlerMutex.Lock()
defer panicHandlerMutex.Unlock()
if p == "test panic" {
panicHandlerInvoked = true
}
}
// Create a new MemoryEmitter with the custom panic handler.
emitter := NewMemoryEmitter(WithPanicHandler(customPanicHandler))
// Define a listener that panics
listener := func(e Event) error {
panic("test panic")
}
// Subscribe the listener to a topic.
_, err := emitter.On("testTopic", listener)
if err != nil {
t.Fatalf("On() failed with error: %v", err)
}
// Emit the event asynchronously to trigger the panic.
errChan := emitter.Emit("testTopic", "testPayload")
// Wait for all events to be processed (which includes recovering from panic).
for range errChan {
// Normally, you'd check for errors here, but in this case, we expect a panic, not an error
}
// Verify that the custom panic handler was invoked
panicHandlerMutex.Lock()
wasPanicHandlerInvoked := panicHandlerInvoked
panicHandlerMutex.Unlock()
if !wasPanicHandlerInvoked {
t.Fatalf("Custom panic handler was not called on listener panic")
}
}
func TestWithIDGenerator(t *testing.T) {
// Custom ID to be returned by the custom ID generator
customID := "customID"
// Define a custom ID generator that returns the custom ID
customIDGenerator := func() string {
return customID
}
// Create a new MemoryEmitter with the custom ID generator.
emitter := NewMemoryEmitter(WithIDGenerator(customIDGenerator))
// Define a no-op listener.
listener := func(e Event) error {
return nil
}
// Subscribe the listener to a topic and capture the returned ID.
returnedID, err := emitter.On("testTopic", listener)
if err != nil {
t.Fatalf("On() failed with error: %v", err)
}
// Check if the returned ID matches the custom ID.
if returnedID != customID {
t.Fatalf("Expected ID to be '%s', but got '%s'", customID, returnedID)
}
}