forked from donovanhide/eventsource
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstream_restart_close_test.go
125 lines (102 loc) · 3.13 KB
/
stream_restart_close_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
package eventsource
import (
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/launchdarkly/go-test-helpers/v2/httphelpers"
)
func toPublication(e httphelpers.SSEEvent) *publication {
return &publication{
id: e.ID,
event: e.Event,
data: e.Data,
}
}
func TestStreamRestart(t *testing.T) {
streamHandler1, streamControl1 := httphelpers.SSEHandler(nil)
defer streamControl1.Close()
streamHandler2, streamControl2 := httphelpers.SSEHandler(nil)
defer streamControl2.Close()
httpServer := httptest.NewServer(httphelpers.SequentialHandler(streamHandler1, streamHandler2))
defer httpServer.Close()
stream := mustSubscribe(t, httpServer.URL,
StreamOptionInitialRetry(time.Millisecond))
defer stream.Close()
eventIn1 := httphelpers.SSEEvent{ID: "123"}
streamControl1.Enqueue(eventIn1)
eventOut1 := <-stream.Events
assert.Equal(t, "123", eventOut1.Id())
stream.Restart()
eventIn2 := httphelpers.SSEEvent{ID: "456"}
streamControl2.Enqueue(eventIn2)
eventOut2 := <-stream.Events // received an event from streamHandler2
assert.Equal(t, "456", eventOut2.Id())
assert.Equal(t, 0, len(stream.Errors)) // restart is not reported as an error
}
func TestStreamClose(t *testing.T) {
streamHandler, streamControl := httphelpers.SSEHandler(nil)
defer streamControl.Close()
httpServer := httptest.NewServer(streamHandler)
defer httpServer.Close()
stream := mustSubscribe(t, httpServer.URL)
stream.Close()
// it's safe to Close the stream multiple times
stream.Close()
select {
case _, ok := <-stream.Events:
if ok {
t.Error("Expected stream.Events channel to be closed. Is still open.")
}
case <-time.After(timeToWaitForEvent):
t.Error("Timed out waiting for stream.Events channel to close")
}
select {
case _, ok := <-stream.Errors:
if ok {
t.Error("Expected stream.Errors channel to be closed. Is still open.")
}
case <-time.After(timeToWaitForEvent):
t.Error("Timed out waiting for stream.Errors channel to close")
}
}
func TestStreamCloseWhileReconnecting(t *testing.T) {
streamHandler, streamControl := httphelpers.SSEHandler(nil)
defer streamControl.Close()
httpServer := httptest.NewServer(streamHandler)
defer httpServer.Close()
stream := mustSubscribe(t, httpServer.URL, StreamOptionInitialRetry(time.Hour))
defer stream.Close()
streamControl.Enqueue(httphelpers.SSEEvent{ID: "123"})
select {
case <-stream.Events:
case <-time.After(timeToWaitForEvent):
t.Error("Timed out waiting for event")
return
}
streamControl.EndAll()
// Expect at least one error
select {
case <-stream.Errors:
case <-time.After(timeToWaitForEvent):
t.Error("Timed out waiting for event")
return
}
stream.Close()
select {
case _, ok := <-stream.Events:
if ok {
t.Error("Expected stream.Events channel to be closed. Is still open.")
}
case <-time.After(timeToWaitForEvent):
t.Error("Timed out waiting for stream.Events channel to close")
}
select {
case _, ok := <-stream.Errors:
if ok {
t.Error("Expected stream.Errors channel to be closed. Is still open.")
}
case <-time.After(timeToWaitForEvent):
t.Error("Timed out waiting for stream.Errors channel to close")
}
}