-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_test.go
145 lines (127 loc) · 3.75 KB
/
stream_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
package sse
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type ResponseRecorderWrapper struct {
*httptest.ResponseRecorder
closer chan bool
}
func (r ResponseRecorderWrapper) CloseNotify() <-chan bool {
return r.closer
}
func TestStream(t *testing.T) {
var events = []struct {
obj Event
raw string
}{
{
obj: Event{
Type: "test",
Message: "this message\nhas two lines",
},
raw: "event:test\ndata:this message\ndata:has two lines\n\n",
}, {
obj: Event{Message: "No type defaults to 'message'"},
raw: "event:message\ndata:No type defaults to 'message'\n\n",
},
}
for _, event := range events {
stream := NewStream()
recorder := ResponseRecorderWrapper{
ResponseRecorder: httptest.NewRecorder(),
closer: make(chan bool),
}
go stream.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
go func() {
time.Sleep(time.Second * 2) // Wait for data
recorder.closer <- true
}()
stream.Send(event.obj)
assert.Equal(t, "text/event-stream", recorder.Result().Header.Get("content-type"))
assert.Equal(t, event.raw, recorder.Body.String())
}
}
func TestStream_Flushed(t *testing.T) {
stream := NewStream()
recorder := ResponseRecorderWrapper{
ResponseRecorder: httptest.NewRecorder(),
closer: make(chan bool),
}
go func() {
time.Sleep(time.Millisecond * 500) // Wait for data
recorder.closer <- true
}()
stream.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
assert.True(t, recorder.Flushed, "Stream should be flushed on initial connection")
assert.Equal(t, "text/event-stream", recorder.Result().Header.Get("content-type"))
assert.Equal(t, http.StatusOK, recorder.Result().StatusCode)
}
func TestStream_Ping(t *testing.T) {
stream := NewStream()
recorder := ResponseRecorderWrapper{
ResponseRecorder: httptest.NewRecorder(),
closer: make(chan bool),
}
go func() {
stream.Ping()
time.Sleep(time.Millisecond * 500) // Wait for data
recorder.closer <- true
}()
stream.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
assert.Equal(t, ":ping\n\n", recorder.Body.String())
}
func TestStream_Close(t *testing.T) {
recorder := ResponseRecorderWrapper{
ResponseRecorder: httptest.NewRecorder(),
closer: make(chan bool),
}
unit := NewStream()
responseNotifier := make(chan struct{})
go func() {
unit.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
responseNotifier <- struct{}{}
}()
unit.Ping()
unit.Close()
go require.NotPanics(t, unit.Ping, "Should not panic after close")
select {
case <-time.NewTimer(time.Second * 10).C:
t.Fatal("Stream did not close connection")
case <-responseNotifier:
}
}
func TestStream_SendCloseDeadlock(t *testing.T) {
recorder := ResponseRecorderWrapper{
ResponseRecorder: httptest.NewRecorder(),
closer: make(chan bool),
}
unit := NewStream()
go unit.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
unit.Ping()
unit.Close()
unit.Ping()
unit.Ping()
unit.Ping()
}
func TestStream_MultiClose(t *testing.T) {
unit := NewStream()
require.NotPanics(t, unit.Close)
require.NotPanics(t, unit.Close)
}
func TestStream_MultiServeHTTP(t *testing.T) {
recorder := ResponseRecorderWrapper{
ResponseRecorder: httptest.NewRecorder(),
closer: make(chan bool),
}
unit := NewStream()
go require.NotPanics(t, func() { unit.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil)) })
time.Sleep(time.Second) // Give go routine time to start
require.Panics(t, func() { unit.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil)) },
"Second call to ServeHTTP should panic!")
require.NotPanics(t, unit.Close)
}