-
Notifications
You must be signed in to change notification settings - Fork 1
/
impl_test.go
455 lines (398 loc) · 14.9 KB
/
impl_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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* Copyright (c) 2021-present unTill Pro, Ltd.
*/
package ibusmem
import (
"context"
"encoding/json"
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
ibus "github.com/untillpro/airs-ibus"
)
func TestBasicUsage(t *testing.T) {
require := require.New(t)
t.Run("Single response basic usage", func(t *testing.T) {
var bus ibus.IBus
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
// wrong sender -> panic
require.Panics(func() { bus.SendResponse("wrong sender", ibus.Response{}) })
bus.SendResponse(sender, ibus.Response{Data: []byte("hello world")})
// second response -> panic
require.Panics(func() { bus.SendParallelResponse2(sender) })
require.Panics(func() { bus.SendResponse(sender, ibus.Response{}) })
})
response, sections, secErr, err := bus.SendRequest2(context.Background(), ibus.Request{}, ibus.DefaultTimeout)
require.Equal("hello world", string(response.Data))
require.Nil(sections)
require.Nil(secErr)
require.Nil(err)
})
t.Run("Sectioned response basic usage", func(t *testing.T) {
testErr := errors.New("error from result sender")
ch := make(chan interface{})
var bus ibus.IBus
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
// wrong sender -> panic
require.Panics(func() { bus.SendParallelResponse2("wrong sender") })
rs := bus.SendParallelResponse2(sender)
go func() {
// try to send an element without section -> panic
require.Panics(func() { require.Nil(rs.SendElement("", "element1")) })
rs.StartArraySection("array", []string{"array-path"})
require.Nil(rs.SendElement("", "element1"))
require.Nil(rs.SendElement("", "element2"))
require.Nil(rs.SendElement("", nil)) // nothingness will not be sent
// send an unmarshalable element -> error
require.NotNil(rs.SendElement("", func() {}))
require.Nil(rs.ObjectSection("object", []string{"object-path"}, "value"))
require.Nil(rs.ObjectSection("", nil, nil)) // nothingness will not be sent
// try to send element on object section -> panic
require.Panics(func() { rs.SendElement("", 42) })
rs.StartMapSection("map", []string{"map-path"})
require.Nil(rs.SendElement("key1", "value1"))
require.Nil(rs.SendElement("key2", "value2"))
require.Nil(rs.SendElement("", nil)) // nothingness will not be sent
rs.Close(testErr)
// any action after close -> panic
require.Panics(func() { rs.SendElement("", 42) })
require.Panics(func() { rs.StartArraySection("array", []string{"array-path"}) })
require.Panics(func() { rs.StartMapSection("array", []string{"array-path"}) })
require.Panics(func() { rs.ObjectSection("array", []string{"array-path"}, nil) })
close(ch)
}()
// second response -> panic
require.Panics(func() { bus.SendParallelResponse2(sender) })
require.Panics(func() { bus.SendResponse(sender, ibus.Response{}) })
})
requestCtx := context.Background()
response, sections, secErr, err := bus.SendRequest2(requestCtx, ibus.Request{}, ibus.DefaultTimeout)
require.Nil(err)
require.Empty(response)
// expect array section
section := <-sections
arraySection := section.(ibus.IArraySection)
require.Equal("array", arraySection.Type())
require.Equal([]string{"array-path"}, arraySection.Path())
// elems of array section
val, ok := arraySection.Next(requestCtx)
require.True(ok)
require.Equal(`"element1"`, string(val))
val, ok = arraySection.Next(requestCtx)
require.True(ok)
require.Equal(`"element2"`, string(val))
val, ok = arraySection.Next(requestCtx)
require.Nil(val)
require.False(ok)
// expect object section
section = <-sections
objectSection := section.(ibus.IObjectSection)
require.Equal("object", objectSection.Type())
require.Equal([]string{"object-path"}, objectSection.Path())
require.Equal(`"value"`, string(objectSection.Value(requestCtx)))
// expect map section
section = <-sections
mapSection := section.(ibus.IMapSection)
require.Equal("map", mapSection.Type())
require.Equal([]string{"map-path"}, mapSection.Path())
// elems of map section
name, val, ok := mapSection.Next(requestCtx)
require.True(ok)
require.Equal("key1", name)
require.Equal(`"value1"`, string(val))
name, val, ok = mapSection.Next(requestCtx)
require.True(ok)
require.Equal("key2", name)
require.Equal(`"value2"`, string(val))
name, val, ok = mapSection.Next(requestCtx)
require.False(ok)
require.Equal("", name)
require.Nil(val)
// no more sections
_, ok = <-sections
require.False(ok)
require.ErrorIs(testErr, *secErr)
<-ch
})
t.Run("Provide should panic on nil requestHandler provided", func(t *testing.T) {
require.Panics(func() { Provide(nil) })
})
t.Run("Should return timeout error if no response at all", func(t *testing.T) {
bus := provide(func(srequestCtx context.Context, ender interface{}, request ibus.Request) {
// do not send response to trigger the timeout case
}, timeoutTrigger, time.After, time.After)
response, sections, secErr, err := bus.SendRequest2(context.Background(), ibus.Request{}, ibus.DefaultTimeout)
require.Equal(ibus.ErrTimeoutExpired, err)
require.Empty(response)
require.Nil(sections)
require.Nil(secErr)
})
}
func TestBus_SendRequest2(t *testing.T) {
require := require.New(t)
t.Run("Should return error on ctx done", func(t *testing.T) {
bus := Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
// do not send response to trigger ctx.Done() case
})
ctx, cancel := context.WithCancel(context.Background())
cancel()
response, sections, secErr, err := bus.SendRequest2(ctx, ibus.Request{}, ibus.DefaultTimeout)
require.Equal("context canceled", err.Error())
require.Empty(response)
require.Nil(sections)
require.Nil(secErr)
})
t.Run("Should handle panic in request handler with message", func(t *testing.T) {
bus := Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
panic("boom")
})
response, sections, secErr, err := bus.SendRequest2(context.Background(), ibus.Request{}, ibus.DefaultTimeout)
require.Equal("boom", err.Error())
require.Empty(response)
require.Nil(sections)
require.Nil(secErr)
})
t.Run("Should handle panic in request handler with error", func(t *testing.T) {
testErr := errors.New("boom")
bus := Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
panic(testErr)
})
response, sections, secErr, err := bus.SendRequest2(context.Background(), ibus.Request{}, ibus.DefaultTimeout)
require.ErrorIs(err, testErr)
require.Empty(response)
require.Nil(sections)
require.Nil(secErr)
})
}
func TestResultSenderClosable_StartArraySection(t *testing.T) {
require := require.New(t)
t.Run("Should return timeout error on long section read", func(t *testing.T) {
var bus ibus.IBus
ch := make(chan interface{})
bus = provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
err := rs.ObjectSection("", nil, 42)
ch <- nil // signal tried to send a section
require.ErrorIs(err, ibus.ErrNoConsumer)
rs.Close(nil)
}()
}, time.After, timeoutTrigger, time.After)
resp, sections, secErr, err := bus.SendRequest2(context.Background(), ibus.Request{}, ibus.DefaultTimeout)
require.Nil(err)
require.NotNil(sections)
require.Empty(resp)
<-ch // do not read section to trigger the timeout case
_, ok := <-sections
require.False(ok)
require.Nil(*secErr)
})
t.Run("Should return error when ctx done on send section", func(t *testing.T) {
var bus ibus.IBus
ch := make(chan interface{})
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
<-ch // wait for cancel
err := rs.ObjectSection("", nil, 42)
ch <- nil // signal ok to start read sections. That forces ctx.Done case fire at tryToSendSection
require.Equal("context canceled", err.Error())
rs.Close(nil)
}()
})
ctx, cancel := context.WithCancel(context.Background())
response, sections, secErr, err := bus.SendRequest2(ctx, ibus.Request{}, ibus.DefaultTimeout)
require.Nil(err)
require.Empty(response)
require.NotNil(sections)
cancel()
ch <- nil // signal cancelled
<-ch // delay to read sections to make ctx.Done() branch at tryToSendSection fire.
// note: section could be sent on ctx.Done() because cases order is undefined at tryToSendSection. But ObjectSection() will return error in any case
for range sections {
}
require.Nil(*secErr)
})
}
func TestResultSenderClosable_SendElement(t *testing.T) {
require := require.New(t)
t.Run("Should accept object", func(t *testing.T) {
type article struct {
ID int64
Name string
}
var bus ibus.IBus
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
require.Nil(rs.ObjectSection("", nil, article{ID: 100, Name: "Cola"}))
rs.Close(nil)
}()
})
a := article{}
requestCtx := context.Background()
response, sections, secErr, err := bus.SendRequest2(requestCtx, ibus.Request{}, ibus.DefaultTimeout)
require.Nil(err)
require.Empty(response)
require.Nil(json.Unmarshal((<-sections).(ibus.IObjectSection).Value(requestCtx), &a))
require.Equal(int64(100), a.ID)
require.Equal("Cola", a.Name)
_, ok := <-sections
require.False(ok)
require.Nil(*secErr)
})
t.Run("Should accept JSON", func(t *testing.T) {
type point struct {
X int64
Y int64
}
var bus ibus.IBus
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
require.Nil(rs.ObjectSection("", nil, []byte(`{"X":52,"Y":89}`)))
rs.Close(nil)
}()
})
p := point{}
requestCtx := context.Background()
response, sections, secErr, err := bus.SendRequest2(requestCtx, ibus.Request{}, ibus.DefaultTimeout)
require.Nil(err)
require.Empty(response)
require.Nil(json.Unmarshal((<-sections).(ibus.IObjectSection).Value(requestCtx), &p))
require.Equal(int64(52), p.X)
require.Equal(int64(89), p.Y)
_, ok := <-sections
require.False(ok)
require.Nil(*secErr)
})
t.Run("Should return error when client reads element too long", func(t *testing.T) {
var bus ibus.IBus
bus = provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
rs.StartArraySection("", nil)
require.ErrorIs(rs.SendElement("", 0), ibus.ErrNoConsumer)
rs.Close(nil)
}()
}, time.After, time.After, timeoutTrigger)
response, sections, secErr, err := bus.SendRequest2(context.Background(), ibus.Request{}, ibus.DefaultTimeout)
require.Nil(err)
require.Empty(response)
_ = (<-sections).(ibus.IArraySection)
// do not read an element to trigger timeout
_, ok := <-sections
require.False(ok)
require.Nil(*secErr)
})
t.Run("Should return error when ctx done on send element", func(t *testing.T) {
var bus ibus.IBus
ch := make(chan interface{})
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
rs.StartArraySection("", nil)
require.Nil(rs.SendElement("", 0))
<-ch // wait for element read
<-ch // wait for cancel
err := rs.SendElement("", 1)
ch <- nil // signal ok to read next element. That forces ctx.Done() case fire at tryToSendElement
require.Equal("context canceled", err.Error())
rs.Close(nil)
}()
})
ctx, cancel := context.WithCancel(context.Background())
response, sections, secErr, err := bus.SendRequest2(ctx, ibus.Request{}, ibus.DefaultTimeout)
require.Nil(err)
require.Empty(response)
array := (<-sections).(ibus.IArraySection)
val, ok := array.Next(ctx)
require.True(ok)
require.Equal([]byte("0"), val)
ch <- nil // signal we're read an element
// cancel should be synchronzied, otherwise possible at tryToSendElement: write to elements channel, read it here, cancel here,
// then return non-nil ctx.Err() from tryToSendElement -> SendElement early failed
cancel()
ch <- nil // signal cancelled
<-ch // wait for ok to read next element to force ctx.Done case fire at tryToSendElement
_, _ = array.Next(ctx) // note: element could be sent on ctx.Done() because cases order is undefined at tryToSendElement. But SendElement() will return error in any case
_, ok = <-sections
require.False(ok)
require.Nil(*secErr)
})
}
func TestObjectSection_Value(t *testing.T) {
var bus ibus.IBus
require := require.New(t)
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
require.Nil(rs.ObjectSection("", nil, []byte("bb")))
rs.Close(nil)
}()
})
requestCtx := context.Background()
response, sections, secErr, err := bus.SendRequest2(requestCtx, ibus.Request{}, ibus.DefaultTimeout)
object := (<-sections).(ibus.IObjectSection)
require.Nil(err)
require.Empty(response)
require.Equal([]byte("bb"), object.Value(requestCtx))
require.Nil(object.Value(requestCtx))
_, ok := <-sections
require.False(ok)
require.Nil(*secErr)
}
func TestClientDisconnectOnSectionsSending(t *testing.T) {
require := require.New(t)
ch := make(chan interface{})
var bus ibus.IBus
bus = Provide(func(requestCtx context.Context, sender interface{}, request ibus.Request) {
rs := bus.SendParallelResponse2(sender)
go func() {
rs.StartArraySection("array", []string{"array-path"})
require.Nil(rs.SendElement("1", "element1"))
ch <- nil
// client is disconnected here
<-ch
require.Error(context.Canceled, rs.SendElement("2", "element2"))
rs.Close(nil)
close(ch)
}()
})
clientCtx, clientCtxCancel := context.WithCancel(context.Background())
response, sections, secErr, err := bus.SendRequest2(clientCtx, ibus.Request{}, ibus.DefaultTimeout)
require.Nil(err)
require.Empty(response)
// expect array section
section := <-sections
arraySection := section.(ibus.IArraySection)
require.Equal("array", arraySection.Type())
require.Equal([]string{"array-path"}, arraySection.Path())
// elems of array section
val, ok := arraySection.Next(clientCtx)
require.True(ok)
require.Equal(`"element1"`, string(val))
// simulate client disconnect
<-ch
clientCtxCancel()
ch <- nil
val, ok = arraySection.Next(clientCtx)
if ok {
// client context canceled -> write to sections channel is possible anyway because
// sectons<- and <-ctx.Done() cases has the same probability at resultSenderClosable.tryToSendElement(). ctx.Err() will be retuned even on sectons<-
require.Equal(`"element2"`, string(val))
_, ok = arraySection.Next(clientCtx)
require.False(ok)
}
// no more sections
_, ok = <-sections
require.False(ok)
require.Nil(*secErr)
<-ch
}
func timeoutTrigger(d time.Duration) <-chan time.Time {
res := make(chan time.Time)
close(res)
return res
}