-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
client_test.go
631 lines (538 loc) · 15.4 KB
/
client_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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
package sse_test
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/tmaxmax/go-sse"
"github.com/tmaxmax/go-sse/internal/parser"
"github.com/tmaxmax/go-sse/internal/tests"
)
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (r roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return r(req)
}
func reqCtx(tb testing.TB, ctx context.Context, method, address string, body io.Reader) *http.Request { //nolint
tb.Helper()
r, err := http.NewRequestWithContext(ctx, method, address, body)
tests.Equal(tb, err, nil, "failed to create request")
return r
}
func req(tb testing.TB, method, address string, body io.Reader) *http.Request { //nolint
tb.Helper()
return reqCtx(tb, context.Background(), method, address, body)
}
func toEv(tb testing.TB, s string) (ev sse.Event) {
tb.Helper()
defer func() {
if l := len(ev.Data); l > 0 {
ev.Data = ev.Data[:l-1]
}
}()
p := parser.NewFieldParser(s)
for f := (parser.Field{}); p.Next(&f); {
switch f.Name { //nolint:exhaustive // Comment fields are not parsed.
case parser.FieldNameData:
ev.Data += f.Value + "\n"
case parser.FieldNameID:
ev.LastEventID = string(f.Value)
case parser.FieldNameEvent:
ev.Type = string(f.Value)
case parser.FieldNameRetry:
default:
return
}
}
tests.Equal(tb, p.Err(), nil, "unexpected toEv fail")
return
}
func TestClient_NewConnection(t *testing.T) {
tests.Panics(t, func() {
sse.NewConnection(nil)
}, "a connection cannot be created without a request")
c := sse.Client{}
r := req(t, "", "", nil)
_ = c.NewConnection(r)
tests.Equal(t, c.HTTPClient, http.DefaultClient, "incorrect default HTTP client")
}
func TestConnection_Connect_retry(t *testing.T) {
var firstReconnectionTime time.Duration
var retryAttempts int
testErr := errors.New("done")
c := &sse.Client{
HTTPClient: &http.Client{
Transport: roundTripperFunc(func(_ *http.Request) (*http.Response, error) {
return nil, testErr
}),
},
OnRetry: func(_ error, duration time.Duration) {
retryAttempts++
if retryAttempts == 1 {
firstReconnectionTime = duration
}
},
Backoff: sse.Backoff{
MaxRetries: 3,
InitialInterval: time.Millisecond,
},
}
r := req(t, "", "", http.NoBody)
err := c.NewConnection(r).Connect()
tests.ErrorIs(t, err, testErr, "invalid error received from Connect")
tests.Equal(t, retryAttempts, c.Backoff.MaxRetries, "connection was not retried enough times")
timeDelta := time.Duration(float64(c.Backoff.InitialInterval) * sse.DefaultClient.Backoff.Jitter)
tests.Expect(t, c.Backoff.InitialInterval-timeDelta <= firstReconnectionTime && firstReconnectionTime <= c.Backoff.InitialInterval+timeDelta, "reconnection time incorrectly set")
}
func TestConnection_Connect_noRetryCtxErr(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ticker := time.NewTicker(time.Millisecond)
defer ticker.Stop()
for {
select {
case ts := <-ticker.C:
fmt.Fprintf(w, "id: %s\n\n", ts)
case <-r.Context().Done():
return
}
}
}))
t.Cleanup(ts.Close)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
c := &sse.Client{
HTTPClient: ts.Client(),
ResponseValidator: sse.NoopValidator,
}
r := reqCtx(t, ctx, "", ts.URL, http.NoBody)
go func() {
time.Sleep(time.Millisecond)
cancel()
}()
err := c.NewConnection(r).Connect()
tests.ErrorIs(t, err, ctx.Err(), "invalid connect error")
}
type readerWrapper struct {
io.Reader
}
func TestConnection_Connect_resetBody(t *testing.T) {
type test struct {
body io.Reader
err error
getBody func() (io.ReadCloser, error)
name string
}
getBodyErr := errors.New("haha")
tt := []test{
{
name: "No body",
},
{
name: "Body for which GetBody is set",
body: strings.NewReader("nice"),
},
{
name: "Body without GetBody",
body: readerWrapper{strings.NewReader("haha")},
err: sse.ErrNoGetBody,
},
{
name: "GetBody that returns error",
err: getBodyErr,
body: readerWrapper{nil},
getBody: func() (io.ReadCloser, error) {
return nil, getBodyErr
},
},
}
ts := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
time.Sleep(time.Millisecond * 5)
}))
defer ts.Close()
httpClient := ts.Client()
rt := httpClient.Transport
c := &sse.Client{
HTTPClient: httpClient,
ResponseValidator: sse.NoopValidator,
Backoff: sse.Backoff{
MaxRetries: 1,
InitialInterval: time.Nanosecond,
},
}
for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
firstTry := true
c.HTTPClient.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
if firstTry {
firstTry = false
return nil, errors.New("fail")
}
return rt.RoundTrip(r)
})
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*3)
defer cancel()
r := reqCtx(t, ctx, "", ts.URL, test.body)
if test.getBody != nil {
r.GetBody = test.getBody
}
err := c.NewConnection(r).Connect()
if test.err != nil {
tests.ErrorIs(t, err, test.err, "incorrect error received from Connect")
} else {
tests.Equal(t, err, ctx.Err(), "connection error should be context error")
}
})
}
}
func TestConnection_Connect_validator(t *testing.T) {
validatorErr := errors.New("invalid")
type test struct {
err error
validator sse.ResponseValidator
name string
}
tt := []test{
{
name: "No validation error",
validator: sse.NoopValidator,
err: io.EOF,
},
{
name: "Validation error",
validator: func(_ *http.Response) error {
return validatorErr
},
err: validatorErr,
},
}
ts := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))
defer ts.Close()
c := &sse.Client{
HTTPClient: ts.Client(),
Backoff: sse.Backoff{
MaxRetries: -1,
},
}
for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
c.ResponseValidator = test.validator
err := c.NewConnection(req(t, "", ts.URL, nil)).Connect()
tests.ErrorIs(t, err, test.err, "incorrect error received from Connect")
})
}
}
func TestConnection_Connect_defaultValidator(t *testing.T) {
type test struct {
handler http.Handler
name string
expectErr bool
}
tt := []test{
{
name: "Valid request",
handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
w.WriteHeader(http.StatusOK)
}),
},
{
name: "Invalid content type",
handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "plain text")
}),
expectErr: true,
},
{
name: "Empty content type",
handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "")
w.WriteHeader(http.StatusOK)
}),
expectErr: true,
},
{
name: "Invalid response status code",
handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(http.StatusUnauthorized)
}),
expectErr: true,
},
}
for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
ts := httptest.NewServer(test.handler)
defer ts.Close()
c := &sse.Client{HTTPClient: ts.Client(), Backoff: sse.Backoff{MaxRetries: -1}}
err := c.NewConnection(req(t, "", ts.URL, nil)).Connect()
if test.expectErr {
tests.Expect(t, err != nil, "expected Connect error")
} else {
tests.ErrorIs(t, err, io.EOF, "should propagate EOF error")
}
})
}
}
func events(tb testing.TB, c *sse.Connection, topics ...string) (events <-chan []sse.Event, unsubscribe sse.EventCallbackRemover) {
tb.Helper()
ch := make(chan []sse.Event)
recv := make(chan sse.Event, 1)
done := make(chan struct{})
var unsub sse.EventCallbackRemover
cb := func(e sse.Event) {
select {
case <-done:
case recv <- e:
}
}
events = ch
if l := len(topics); l == 1 {
if t := topics[0]; t == "" {
unsub = c.SubscribeMessages(cb) // for coverage, SubscribeEvent("", recv) would be equivalent
} else {
unsub = c.SubscribeEvent(t, cb)
}
} else {
if l == 0 {
unsub = c.SubscribeToAll(cb)
} else {
unsubFns := make([]sse.EventCallbackRemover, 0, len(topics))
for _, t := range topics {
unsubFns = append(unsubFns, c.SubscribeEvent(t, cb))
}
unsub = func() {
for _, fn := range unsubFns {
fn()
}
}
}
}
unsubscribe = func() {
defer func() { _ = recover() }()
defer close(done)
unsub()
}
go func() {
defer close(ch)
var evs []sse.Event
for {
select {
case ev := <-recv:
evs = append(evs, ev)
case <-done:
ch <- evs
return
}
}
}()
return
}
func TestConnection_Subscriptions(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
data := "retry: 1000\n\nevent: test\ndata: something\nid: 1\n\nevent: test2\ndata: something else\n\ndata: unnamed\nid: 2\n\ndata: this shouldn't be received"
for _, s := range strings.SplitAfter(data, "\n\n") {
_, _ = io.WriteString(w, s)
w.(http.Flusher).Flush()
time.Sleep(time.Millisecond)
}
}))
defer ts.Close()
c := &sse.Client{
HTTPClient: ts.Client(),
ResponseValidator: sse.NoopValidator,
Backoff: sse.Backoff{MaxRetries: -1},
}
conn := c.NewConnection(req(t, "", ts.URL, nil))
firstEvent := sse.Event{}
secondEvent := sse.Event{Type: "test", Data: "something", LastEventID: "1"}
thirdEvent := sse.Event{Type: "test2", Data: "something else", LastEventID: "1"}
fourthEvent := sse.Event{Data: "unnamed", LastEventID: "2"}
all, unsubAll := events(t, conn)
defer unsubAll()
expectedAll := []sse.Event{firstEvent, secondEvent, thirdEvent, fourthEvent}
test, unsubTest := events(t, conn, "test")
defer unsubTest()
expectedTest := []sse.Event{secondEvent}
test2, unsubTest2 := events(t, conn, "test2")
defer unsubTest2()
expectedTest2 := []sse.Event{thirdEvent}
messages, unsubMessages := events(t, conn, "")
defer unsubMessages()
expectedMessages := []sse.Event{firstEvent, fourthEvent}
tests.ErrorIs(t, conn.Connect(), sse.ErrUnexpectedEOF, "incorrect Connect error")
unsubAll()
tests.DeepEqual(t, <-all, expectedAll, "unexpected events for all")
unsubTest()
tests.DeepEqual(t, <-test, expectedTest, "unexpected events for test")
unsubTest2()
tests.DeepEqual(t, <-test2, expectedTest2, "unexpected events for test2")
unsubMessages()
tests.DeepEqual(t, <-messages, expectedMessages, "unexpected events for messages")
}
func TestConnection_dispatchDirty(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "data: hello\ndata: world\n")
}))
defer ts.Close()
c := &sse.Client{
HTTPClient: ts.Client(),
ResponseValidator: sse.NoopValidator,
Backoff: sse.Backoff{
MaxRetries: -1,
},
}
conn := c.NewConnection(req(t, "", ts.URL, nil))
expected := sse.Event{Data: "hello\nworld"}
var got sse.Event
conn.SubscribeMessages(func(e sse.Event) {
got = e
})
tests.ErrorIs(t, conn.Connect(), io.EOF, "unexpected Connect error")
tests.Equal(t, got, expected, "unexpected event received")
}
func TestConnection_Unsubscriptions(t *testing.T) {
evs := make(chan string)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
panic(http.ErrAbortHandler)
}
for ev := range evs {
_, _ = io.WriteString(w, ev)
flusher.Flush()
}
}))
defer ts.Close()
c := &sse.Client{
HTTPClient: ts.Client(),
ResponseValidator: sse.NoopValidator,
Backoff: sse.Backoff{
MaxRetries: -1,
},
}
conn := c.NewConnection(req(t, "", ts.URL, nil))
all, unsubAll := events(t, conn)
some, unsubSome := events(t, conn, "a", "b")
one, unsubOne := events(t, conn, "a")
messages, unsubMessages := events(t, conn, "")
type action struct {
unsub func()
message string
}
actions := []action{
{message: "data: unnamed\n\n", unsub: unsubMessages},
{message: "data: for one and some\nevent: a\n\n", unsub: unsubOne},
{message: "data: for some\nevent: b\n\n", unsub: unsubSome},
{message: "data: for one and some again\nevent: a\n\n", unsub: unsubAll},
{message: "data: unnamed again\n\n"},
{message: "data: for some again\nevent: b\n\n"},
}
firstEvent := toEv(t, actions[0].message)
secondEvent := toEv(t, actions[1].message)
thirdEvent := toEv(t, actions[2].message)
fourthEvent := toEv(t, actions[3].message)
expectedAll := []sse.Event{firstEvent, secondEvent, thirdEvent, fourthEvent}
expectedSome := []sse.Event{secondEvent, thirdEvent}
expectedOne := []sse.Event{secondEvent}
expectedMessages := []sse.Event{firstEvent}
go func() {
defer close(evs)
for _, action := range actions {
evs <- action.message
// we wait for the subscribers to receive the event
time.Sleep(time.Millisecond * 5)
if action.unsub != nil {
action.unsub()
}
}
}()
tests.ErrorIs(t, conn.Connect(), io.EOF, "unexpected Connect error")
tests.DeepEqual(t, <-all, expectedAll, "unexpected events for all")
tests.DeepEqual(t, <-some, expectedSome, "unexpected events for some")
tests.DeepEqual(t, <-one, expectedOne, "unexpected events for one")
tests.DeepEqual(t, <-messages, expectedMessages, "unexpected events for messages")
}
func TestConnection_serverError(t *testing.T) {
type action struct {
message string
cancel bool
}
evs := make(chan action)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
panic(http.ErrAbortHandler)
}
for ev := range evs {
if ev.cancel {
panic(http.ErrAbortHandler)
}
_, _ = io.WriteString(w, ev.message)
flusher.Flush()
}
}))
defer ts.Close()
c := sse.Client{
HTTPClient: ts.Client(),
ResponseValidator: sse.NoopValidator,
Backoff: sse.Backoff{MaxRetries: -1},
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn := c.NewConnection(reqCtx(t, ctx, "", ts.URL, nil))
all, unsubAll := events(t, conn)
defer unsubAll()
actions := []action{
{message: "data: first\n"},
{message: "data: second\n\n", cancel: true},
{message: "data: third\n\n"},
}
expected := []sse.Event(nil)
go func() {
defer close(evs)
for _, action := range actions {
evs <- action
if action.cancel {
break
}
time.Sleep(time.Millisecond)
}
}()
tests.Expect(t, conn.Connect() != nil, "expected Connect error")
unsubAll()
tests.DeepEqual(t, <-all, expected, "unexpected values for all")
}
func TestConnection_reconnect(t *testing.T) {
try := 0
lastEventIDs := []string(nil)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lastEventIDs = append(lastEventIDs, r.Header.Get("Last-Event-Id"))
try++
fmt.Fprintf(w, "id: %d\n\n", try)
}))
t.Cleanup(ts.Close)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
retries := 0
c := sse.Client{
HTTPClient: ts.Client(),
OnRetry: func(_ error, _ time.Duration) {
retries++
if retries == 3 {
cancel()
}
},
Backoff: sse.Backoff{
InitialInterval: time.Nanosecond,
},
ResponseValidator: sse.NoopValidator,
}
r := reqCtx(t, ctx, "", ts.URL, http.NoBody)
err := c.NewConnection(r).Connect()
tests.Equal(t, err, ctx.Err(), "expected context error")
tests.DeepEqual(t, lastEventIDs, []string{"", "1", "2"}, "incorrect last event IDs")
}