-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhooks_test.go
68 lines (54 loc) · 1.16 KB
/
hooks_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
package hooks
import (
"sync"
"testing"
)
const (
hookName = "test.hook"
listenerCount = 3
)
// message holds the data being dispatched via the test hooks
type message struct {
id int
}
// listener facilitate the testing of hook listeners
type listener struct {
counter int
msg *message
hook *Hook[*message]
wg sync.WaitGroup
t *testing.T
}
// newListener creates and initializes a new listener with a hook and message
func newListener(t *testing.T) (*listener, *Hook[*message], *message) {
msg := &message{id: 123}
h := NewHook[*message](hookName)
l := &listener{
t: t,
msg: msg,
hook: h,
}
for i := 0; i < listenerCount; i++ {
h.Listen(l.Callback)
}
l.wg.Add(listenerCount)
if listenerCount != h.GetListenerCount() {
t.Fail()
}
return l, h, msg
}
// Callback is the callback method for the test hooks that counts executions, confirms the event data, and
// handles waitgroups for concurrency
func (l *listener) Callback(event Event[*message]) {
l.counter++
if l.msg != event.Msg {
l.t.Fail()
}
if l.hook != event.Hook {
l.t.Fail()
}
if hookName != event.Hook.GetName() {
l.t.Fail()
}
l.wg.Done()
}