-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_test.go
More file actions
188 lines (153 loc) · 4.37 KB
/
event_test.go
File metadata and controls
188 lines (153 loc) · 4.37 KB
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
//go:build testing
package sum
import (
"context"
"sync"
"testing"
"time"
"github.com/zoobz-io/capitan"
)
func init() {
// Enable synchronous mode for deterministic event processing in tests
capitan.Configure(capitan.WithSyncMode())
}
// testEventData is a struct type for testing Event with sentinel.
type testEventData struct {
Message string
}
// testEventInt is a struct type for testing Event with int-like data.
type testEventInt struct {
Value int
}
func TestNewEvent(t *testing.T) {
t.Parallel()
signal := capitan.NewSignal("test.event", "Test event")
event := NewEvent[testEventData](signal, capitan.SeverityInfo)
if event.Signal != signal {
t.Errorf("expected signal %v, got %v", signal, event.Signal)
}
if event.level != capitan.SeverityInfo {
t.Errorf("expected level %v, got %v", capitan.SeverityInfo, event.level)
}
}
func TestNewEventConstructors(t *testing.T) {
t.Parallel()
tests := []struct {
name string
factory func(capitan.Signal) Event[testEventData]
expected capitan.Severity
}{
{"debug", NewDebugEvent[testEventData], capitan.SeverityDebug},
{"info", NewInfoEvent[testEventData], capitan.SeverityInfo},
{"warn", NewWarnEvent[testEventData], capitan.SeverityWarn},
{"error", NewErrorEvent[testEventData], capitan.SeverityError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
signal := capitan.NewSignal("test."+tt.name, "Test "+tt.name+" event")
event := tt.factory(signal)
if event.level != tt.expected {
t.Errorf("expected level %v, got %v", tt.expected, event.level)
}
})
}
}
func TestEventEmitAndListen(t *testing.T) {
t.Parallel()
signal := capitan.NewSignal("test.emit.listen", "Emit and listen test")
event := NewInfoEvent[testEventData](signal)
var received testEventData
var mu sync.Mutex
done := make(chan struct{})
listener := event.Listen(func(ctx context.Context, data testEventData) {
mu.Lock()
received = data
mu.Unlock()
close(done)
})
defer listener.Close()
ctx := context.Background()
event.Emit(ctx, testEventData{Message: "hello"})
select {
case <-done:
mu.Lock()
if received.Message != "hello" {
t.Errorf("expected 'hello', got '%s'", received.Message)
}
mu.Unlock()
case <-time.After(time.Second):
t.Fatal("timeout waiting for event")
}
}
func TestEventListenOnce(t *testing.T) {
t.Parallel()
signal := capitan.NewSignal("test.listen.once", "Listen once test")
event := NewInfoEvent[testEventInt](signal)
var count int
var mu sync.Mutex
done := make(chan struct{}, 2)
listener := event.ListenOnce(func(ctx context.Context, data testEventInt) {
mu.Lock()
count++
mu.Unlock()
done <- struct{}{}
})
defer listener.Close()
ctx := context.Background()
event.Emit(ctx, testEventInt{Value: 1})
event.Emit(ctx, testEventInt{Value: 2})
// Wait for potential callbacks
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("timeout waiting for first event")
}
// Give time for second event to process (it shouldn't trigger callback)
time.Sleep(50 * time.Millisecond)
mu.Lock()
if count != 1 {
t.Errorf("expected callback count 1, got %d", count)
}
mu.Unlock()
}
func TestEventEmitAllSeverities(t *testing.T) {
t.Parallel()
tests := []struct {
name string
level capitan.Severity
factory func(capitan.Signal) Event[testEventData]
}{
{"debug", capitan.SeverityDebug, NewDebugEvent[testEventData]},
{"info", capitan.SeverityInfo, NewInfoEvent[testEventData]},
{"warn", capitan.SeverityWarn, NewWarnEvent[testEventData]},
{"error", capitan.SeverityError, NewErrorEvent[testEventData]},
{"default", capitan.Severity("CUSTOM"), func(s capitan.Signal) Event[testEventData] {
return NewEvent[testEventData](s, capitan.Severity("CUSTOM"))
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
signal := capitan.NewSignal("test.severity."+tt.name, "Severity "+tt.name+" test")
event := tt.factory(signal)
var received bool
done := make(chan struct{})
listener := event.Listen(func(ctx context.Context, data testEventData) {
received = true
close(done)
})
defer listener.Close()
ctx := context.Background()
event.Emit(ctx, testEventData{Message: "test"})
select {
case <-done:
if !received {
t.Error("expected to receive event")
}
case <-time.After(time.Second):
t.Fatal("timeout waiting for event")
}
})
}
}