-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
waiter_test.go
138 lines (127 loc) · 4.21 KB
/
waiter_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
package playwright
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const (
testEventNameFoobar = "foobar"
testEventNameReject = "reject"
testEventPayload = "payload data"
)
func TestWaiterWaitForEvent(t *testing.T) {
timeout := 500.0
emitter := &eventEmitter{}
waiter := newWaiter().WithTimeout(timeout)
_, err := waiter.Wait()
require.Error(t, err)
waiter.WaitForEvent(emitter, testEventNameFoobar, nil)
go func() {
emitter.Emit(testEventNameFoobar, testEventPayload)
emitter.Emit(testEventNameFoobar, "2")
emitter.Emit(testEventNameFoobar, "3")
}()
result, err := waiter.Wait()
require.NoError(t, err)
require.Equal(t, result, testEventPayload)
}
func TestWaiterWaitForEventWithPredicate(t *testing.T) {
timeout := 500.0
emitter := &eventEmitter{}
waiter := newWaiter().WithTimeout(timeout)
waiter.WaitForEvent(emitter, testEventNameFoobar, func(payload interface{}) bool {
content, ok := payload.(string)
if ok && content == testEventPayload {
return true
}
return false
})
go func() {
emitter.Emit(testEventNameFoobar, "1")
emitter.Emit(testEventNameFoobar, testEventPayload)
emitter.Emit(testEventNameFoobar, "3")
}()
result, err := waiter.Wait()
require.NoError(t, err)
require.Equal(t, result, testEventPayload)
}
func TestWaiterRejectOnTimeout(t *testing.T) {
timeout := 300.0
emitter := &eventEmitter{}
waiter := newWaiter().WithTimeout(timeout)
waiter.WaitForEvent(emitter, testEventNameFoobar, nil)
go func() {
time.Sleep(time.Duration(timeout*2) * time.Millisecond)
emitter.Emit(testEventNameFoobar, testEventPayload)
}()
result, err := waiter.Wait()
require.ErrorContains(t, err, fmt.Sprintf("Timeout %.2fms exceeded.", timeout))
require.Nil(t, result)
}
func TestWaiterRejectOnEvent(t *testing.T) {
errCause := fmt.Errorf("reject on event")
errPredicate := fmt.Errorf("payload on event")
emitter := &eventEmitter{}
waiter := newWaiter().RejectOnEvent(emitter, testEventNameReject, errCause)
waiter.RejectOnEvent(emitter, testEventNameFoobar, errPredicate, func(payload interface{}) bool {
content, ok := payload.(string)
if ok && content == "testEventPayload" {
return true
}
return false
})
waiter.WaitForEvent(emitter, testEventNameFoobar, nil)
require.Equal(t, 1, emitter.ListenerCount(testEventNameReject))
go func() {
emitter.Emit(testEventNameReject)
emitter.Emit(testEventNameFoobar, testEventPayload)
emitter.Emit(testEventNameFoobar, "3")
}()
result, err := waiter.Wait()
require.EqualError(t, err, errCause.Error())
require.Equal(t, 0, emitter.ListenerCount(testEventNameReject))
require.Nil(t, result)
}
func TestWaiterRejectOnEventWithPredicate(t *testing.T) {
errCause := fmt.Errorf("reject on event")
errPredicate := fmt.Errorf("payload on event")
emitter := &eventEmitter{}
waiter := newWaiter().RejectOnEvent(emitter, testEventNameReject, errCause)
waiter.RejectOnEvent(emitter, testEventNameFoobar, errPredicate, func(payload interface{}) bool {
content, ok := payload.(string)
if ok && content == testEventPayload {
return true
}
return false
})
waiter.WaitForEvent(emitter, testEventNameFoobar, nil)
require.Equal(t, 1, emitter.ListenerCount(testEventNameReject))
go func() {
emitter.Emit(testEventNameFoobar, testEventPayload)
emitter.Emit(testEventNameReject)
emitter.Emit(testEventNameFoobar, "3")
}()
result, err := waiter.Wait()
require.EqualError(t, err, errPredicate.Error())
require.Equal(t, 0, emitter.ListenerCount(testEventNameReject))
require.Nil(t, result)
}
func TestWaiterReturnErrorWhenMisuse(t *testing.T) {
emitter := &eventEmitter{}
waiter := newWaiter()
waiter.WaitForEvent(emitter, testEventNameFoobar, nil)
waiter.WithTimeout(500)
_, err := waiter.Wait()
require.ErrorContains(t, err, "please set timeout before WaitForEvent")
waiter = newWaiter()
waiter.WaitForEvent(emitter, testEventNameFoobar, nil)
waiter.WaitForEvent(emitter, testEventNameFoo, nil)
_, err = waiter.Wait()
require.ErrorContains(t, err, "WaitForEvent can only be called once")
waiter = newWaiter()
waiter.WaitForEvent(emitter, testEventNameFoobar, nil)
waiter.RejectOnEvent(emitter, testEventNameFoo, nil)
_, err = waiter.Wait()
require.ErrorContains(t, err, "call RejectOnEvent before WaitForEvent")
}