-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks_test.go
More file actions
265 lines (207 loc) · 6.82 KB
/
hooks_test.go
File metadata and controls
265 lines (207 loc) · 6.82 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package dispatch
import (
"context"
"encoding/json"
"errors"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
type contextKey string
// sourceWithHooks implements optional hook interfaces for testing.
type sourceWithHooks struct {
name string
onParseCalled bool
onDispatchCalled bool
onSuccessCalled bool
onFailureCalled bool
onNoHandlerCalled bool
onUnmarshalErrorCalled bool
onValidationErrorCalled bool
onNoHandlerErr error
onUnmarshalErrorErr error
onValidationErrorErr error
}
func (s *sourceWithHooks) Name() string { return s.name }
func (s *sourceWithHooks) Discriminator() Discriminator {
return HasFields("type", "payload")
}
func (s *sourceWithHooks) Parse(raw []byte) (Parsed, error) {
var env struct {
Type string `json:"type"`
Payload json.RawMessage `json:"payload"`
}
if err := json.Unmarshal(raw, &env); err != nil {
return Parsed{}, err
}
if env.Type == "" {
return Parsed{}, errors.New("missing type")
}
return Parsed{Key: env.Type, Payload: env.Payload}, nil
}
func (s *sourceWithHooks) OnParse(ctx context.Context, key string) context.Context {
s.onParseCalled = true
return context.WithValue(ctx, contextKey("source-hook"), "called")
}
func (s *sourceWithHooks) OnDispatch(ctx context.Context, key string) {
s.onDispatchCalled = true
}
func (s *sourceWithHooks) OnSuccess(ctx context.Context, key string, d time.Duration) {
s.onSuccessCalled = true
}
func (s *sourceWithHooks) OnFailure(ctx context.Context, key string, err error, d time.Duration) {
s.onFailureCalled = true
}
func (s *sourceWithHooks) OnNoHandler(ctx context.Context, key string) error {
s.onNoHandlerCalled = true
return s.onNoHandlerErr
}
func (s *sourceWithHooks) OnUnmarshalError(ctx context.Context, key string, err error) error {
s.onUnmarshalErrorCalled = true
return s.onUnmarshalErrorErr
}
func (s *sourceWithHooks) OnValidationError(ctx context.Context, key string, err error) error {
s.onValidationErrorCalled = true
return s.onValidationErrorErr
}
// Verify interface implementations
var (
_ Source = (*sourceWithHooks)(nil)
_ OnParseHook = (*sourceWithHooks)(nil)
_ OnDispatchHook = (*sourceWithHooks)(nil)
_ OnSuccessHook = (*sourceWithHooks)(nil)
_ OnFailureHook = (*sourceWithHooks)(nil)
_ OnNoHandlerHook = (*sourceWithHooks)(nil)
_ OnUnmarshalErrorHook = (*sourceWithHooks)(nil)
_ OnValidationErrorHook = (*sourceWithHooks)(nil)
)
type SourceHooksSuite struct {
suite.Suite
}
func TestSourceHooksSuite(t *testing.T) {
suite.Run(t, new(SourceHooksSuite))
}
func (s *SourceHooksSuite) TestOnParseCalledAfterGlobal() {
var order []string
source := &sourceWithHooks{name: "test"}
r := New(WithOnParse(func(ctx context.Context, src, key string) context.Context {
order = append(order, "global")
return ctx
}))
r.AddSource(source)
Register(r, "test", &testHandler{})
msg := []byte(`{"type": "test", "payload": {}}`)
err := r.Process(context.Background(), msg)
s.NoError(err)
s.Assert().True(source.onParseCalled)
s.Require().Len(order, 1)
s.Assert().Equal("global", order[0])
}
func (s *SourceHooksSuite) TestOnDispatchCalledAfterGlobal() {
var order []string
source := &sourceWithHooks{name: "test"}
r := New(WithOnDispatch(func(ctx context.Context, src, key string) {
order = append(order, "global")
}))
r.AddSource(source)
Register(r, "test", &testHandler{})
msg := []byte(`{"type": "test", "payload": {}}`)
err := r.Process(context.Background(), msg)
s.NoError(err)
s.Assert().True(source.onDispatchCalled)
s.Require().Len(order, 1)
s.Assert().Equal("global", order[0])
}
func (s *SourceHooksSuite) TestOnSuccessCalledAfterGlobal() {
var order []string
source := &sourceWithHooks{name: "test"}
r := New(WithOnSuccess(func(ctx context.Context, src, key string, d time.Duration) {
order = append(order, "global")
}))
r.AddSource(source)
Register(r, "test", &testHandler{})
msg := []byte(`{"type": "test", "payload": {}}`)
err := r.Process(context.Background(), msg)
s.NoError(err)
s.Assert().True(source.onSuccessCalled)
s.Require().Len(order, 1)
s.Assert().Equal("global", order[0])
}
func (s *SourceHooksSuite) TestOnFailureCalledAfterGlobal() {
var order []string
source := &sourceWithHooks{name: "test"}
r := New(WithOnFailure(func(ctx context.Context, src, key string, err error, d time.Duration) {
order = append(order, "global")
}))
r.AddSource(source)
Register(r, "test", &testHandler{err: errors.New("fail")})
msg := []byte(`{"type": "test", "payload": {}}`)
err := r.Process(context.Background(), msg)
s.Error(err)
s.Assert().True(source.onFailureCalled)
s.Require().Len(order, 1)
s.Assert().Equal("global", order[0])
}
func (s *SourceHooksSuite) TestSourceOnNoHandlerCanOverrideGlobalSkip() {
source := &sourceWithHooks{
name: "test",
onNoHandlerErr: errors.New("source says fail"),
}
r := New(WithOnNoHandler(func(ctx context.Context, src, key string) error {
return nil
}))
r.AddSource(source)
msg := []byte(`{"type": "unknown", "payload": {}}`)
err := r.Process(context.Background(), msg)
s.Assert().True(source.onNoHandlerCalled)
s.Assert().Error(err)
}
func (s *SourceHooksSuite) TestGlobalOnNoHandlerErrorPreventsSourceHook() {
globalErr := errors.New("global error")
source := &sourceWithHooks{
name: "test",
onNoHandlerErr: errors.New("source error"),
}
r := New(WithOnNoHandler(func(ctx context.Context, src, key string) error {
return globalErr
}))
r.AddSource(source)
msg := []byte(`{"type": "unknown", "payload": {}}`)
err := r.Process(context.Background(), msg)
s.Assert().ErrorIs(err, globalErr)
}
func (s *SourceHooksSuite) TestSourceOnUnmarshalErrorCanOverrideGlobal() {
source := &sourceWithHooks{
name: "test",
onUnmarshalErrorErr: errors.New("source says fail"),
}
r := New(WithOnUnmarshalError(func(ctx context.Context, src, key string, err error) error {
return nil
}))
r.AddSource(source)
Register(r, "test", &testHandler{})
msg := []byte(`{"type": "test", "payload": "invalid"}`)
err := r.Process(context.Background(), msg)
s.Assert().True(source.onUnmarshalErrorCalled)
s.Assert().Error(err)
}
type SourceHooksContextPropagationSuite struct {
suite.Suite
}
func TestSourceHooksContextPropagationSuite(t *testing.T) {
suite.Run(t, new(SourceHooksContextPropagationSuite))
}
func (s *SourceHooksContextPropagationSuite) TestSourceOnParseContextAvailableToHandler() {
source := &sourceWithHooks{name: "test"}
var handlerCtx context.Context
r := New()
r.AddSource(source)
RegisterFunc(r, "test", func(ctx context.Context, p testPayload) error {
handlerCtx = ctx
return nil
})
msg := []byte(`{"type": "test", "payload": {}}`)
err := r.Process(context.Background(), msg)
s.NoError(err)
s.Assert().Equal("called", handlerCtx.Value(contextKey("source-hook")))
}