-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher_test.go
191 lines (176 loc) · 4.66 KB
/
dispatcher_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
package stop_dispatcher_test
import (
"context"
"errors"
"sync"
"testing"
"time"
stop_dispatcher "github.com/gol4ng/stop-dispatcher"
"github.com/stretchr/testify/assert"
)
func Test_Dispatcher_Stop(t *testing.T) {
callbackCalled := false
d := stop_dispatcher.NewDispatcher()
d.RegisterCallbackFunc(func(ctx context.Context) error {
callbackCalled = true
return nil
})
time.AfterFunc(10*time.Millisecond, func() {
d.Stop("fake_reason")
})
err := d.Wait(context.TODO())
assert.NoError(t, err)
assert.True(t, callbackCalled)
}
func Test_Dispatcher_Error(t *testing.T) {
var step []string
d := stop_dispatcher.NewDispatcher()
d.RegisterPrioritizeCallbackFunc(1, func(ctx context.Context) error {
step = append(step, "priority:1_index:0")
return errors.New("fake_error_priority_1")
})
d.RegisterCallbacksFunc(
func(ctx context.Context) error {
step = append(step, "priority:0_index:0")
return errors.New("fake_error")
},
func(ctx context.Context) error {
step = append(step, "priority:0_index:1")
return errors.New("fake_error2")
},
)
time.AfterFunc(10*time.Millisecond, func() {
d.Stop("fake_reason")
})
err := d.Wait(context.TODO())
assert.EqualError(t, err, "fake_error_priority_1\nfake_error\nfake_error2\n")
assert.Len(t, step, 3)
assert.Equal(t, []string{
"priority:1_index:0",
"priority:0_index:0",
"priority:0_index:1",
}, step)
}
func Test_Dispatcher_WithReasonHandler(t *testing.T) {
reasonHandlerCalled := false
d := stop_dispatcher.NewDispatcher(
stop_dispatcher.WithReasonHandler(func(reason stop_dispatcher.Reason) {
reasonHandlerCalled = true
assert.Equal(t, "fake_reason", reason)
}),
)
time.AfterFunc(10*time.Millisecond, func() {
d.Stop("fake_reason")
})
err := d.Wait(context.TODO())
assert.NoError(t, err)
assert.True(t, reasonHandlerCalled)
}
func Test_Dispatcher_WithEmitter(t *testing.T) {
d := stop_dispatcher.NewDispatcher(
stop_dispatcher.WithEmitter(func(stopFn func(stop_dispatcher.Reason)) {
time.AfterFunc(10*time.Millisecond, func() {
stopFn("fake_reason")
})
}),
)
err := d.Wait(context.TODO())
assert.NoError(t, err)
}
func Test_Dispatcher_UnregisterCallback(t *testing.T) {
safeInnerStopFn := sync.Mutex{}
var innerStopFn func(stop_dispatcher.Reason)
d := stop_dispatcher.NewDispatcher(
stop_dispatcher.WithEmitter(func(stopFn func(stop_dispatcher.Reason)) {
safeInnerStopFn.Lock()
innerStopFn = stopFn
safeInnerStopFn.Unlock()
}),
)
callbackCalled := false
unregisterCallbackFunc := d.RegisterCallbackFunc(func(ctx context.Context) error {
callbackCalled = true
return nil
})
go func() {
time.AfterFunc(10*time.Millisecond, func() {
safeInnerStopFn.Lock()
innerStopFn("fake_reason")
safeInnerStopFn.Unlock()
})
}()
err := d.Wait(context.TODO())
assert.NoError(t, err)
assert.True(t, callbackCalled)
callbackCalled = false
unregisterCallbackFunc()
go func() {
time.AfterFunc(10*time.Millisecond, func() {
safeInnerStopFn.Lock()
innerStopFn("fake_reason")
safeInnerStopFn.Unlock()
})
}()
err1 := d.Wait(context.TODO())
assert.NoError(t, err1)
assert.False(t, callbackCalled)
}
func Test_Dispatcher(t *testing.T) {
callbackCalled := false
d := stop_dispatcher.NewDispatcher()
d.RegisterCallbackFunc(func(ctx context.Context) error {
callbackCalled = true
return nil
})
d.RegisterEmitter(func(stopFn func(stop_dispatcher.Reason)) {
time.AfterFunc(10*time.Millisecond, func() {
stopFn("fake_reason")
})
})
err := d.Wait(context.TODO())
assert.NoError(t, err)
assert.True(t, callbackCalled)
}
func Test_Dispatcher_Prioritize(t *testing.T) {
var step []string
d := stop_dispatcher.NewDispatcher()
d.RegisterCallbacksFunc(
func(ctx context.Context) error {
step = append(step, "priority:0_index:0")
return nil
},
func(ctx context.Context) error {
step = append(step, "priority:0_index:1")
return nil
},
)
d.RegisterCallbacks(
stop_dispatcher.NewPrioritizeCallback(1, func(ctx context.Context) error {
step = append(step, "priority:1_index:0")
return nil
}),
stop_dispatcher.NewPrioritizeCallback(0, func(ctx context.Context) error {
step = append(step, "priority:0_index:2")
return nil
}),
)
d.RegisterPrioritizeCallbackFunc(3, func(ctx context.Context) error {
step = append(step, "priority:3_index:0")
return nil
})
d.RegisterEmitter(func(stopFn func(stop_dispatcher.Reason)) {
time.AfterFunc(10*time.Millisecond, func() {
stopFn("fake_reason")
})
})
err := d.Wait(context.TODO())
assert.NoError(t, err)
assert.Len(t, step, 5)
assert.Equal(t, []string{
"priority:3_index:0",
"priority:1_index:0",
"priority:0_index:0",
"priority:0_index:1",
"priority:0_index:2",
}, step)
}