forked from donovanhide/eventsource
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver_test.go
316 lines (259 loc) · 8.09 KB
/
server_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
package eventsource
import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testServerRepository struct{}
func (r *testServerRepository) Replay(channel, id string) chan Event {
out := make(chan Event, 1)
var fakeID string
if id == "" {
fakeID = "replayed-from-start"
} else {
fakeID = "replayed-from-" + id
}
out <- &publication{id: fakeID, data: "example"}
close(out)
return out
}
func TestNewServerHandlerRespondsAfterClose(t *testing.T) {
server := NewServer()
httpServer := httptest.NewServer(server.Handler("test"))
defer httpServer.Close()
server.Close()
responses := make(chan *http.Response)
go func() {
resp, err := http.Get(httpServer.URL)
if err != nil {
t.Errorf("Unexpected error %s", err)
}
responses <- resp
}()
select {
case resp := <-responses:
if resp.StatusCode != 200 {
t.Errorf("Received StatusCode %d, want 200", resp.StatusCode)
}
case <-time.After(250 * time.Millisecond):
t.Errorf("Did not receive response in time")
}
}
func TestServerHandlerReceivesPublishedEvents(t *testing.T) {
channel := "test"
server := NewServer()
httpServer := httptest.NewServer(server.Handler(channel))
defer httpServer.Close()
resp1, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp1.Body.Close()
resp2, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp2.Body.Close()
event := &publication{data: "my-event"}
ackCh := server.PublishWithAcknowledgment([]string{channel}, event)
<-ackCh
server.Close()
expected := "data: my-event\n\n"
body1, err := ioutil.ReadAll(resp1.Body)
require.NoError(t, err)
body2, err := ioutil.ReadAll(resp2.Body)
require.NoError(t, err)
assert.Equal(t, expected, string(body1))
assert.Equal(t, expected, string(body2))
}
func TestServerHandlerReceivesPublishedComments(t *testing.T) {
channel := "test"
server := NewServer()
httpServer := httptest.NewServer(server.Handler(channel))
defer httpServer.Close()
resp1, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp1.Body.Close()
resp2, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp2.Body.Close()
server.PublishComment([]string{channel}, "my comment")
event := &publication{data: "my-event"}
ackCh := server.PublishWithAcknowledgment([]string{channel}, event)
<-ackCh
server.Close()
expected := ":my comment\ndata: my-event\n\n"
body1, err := ioutil.ReadAll(resp1.Body)
require.NoError(t, err)
body2, err := ioutil.ReadAll(resp2.Body)
require.NoError(t, err)
assert.Equal(t, expected, string(body1))
assert.Equal(t, expected, string(body2))
}
func TestServerHandlerCanReceiveEventsFromRepository(t *testing.T) {
channel := "test"
repo := &testServerRepository{}
t.Run("events are not replayed if ReplayAll is false and Last-Event-Id is not specified", func(t *testing.T) {
server := NewServer()
server.Register(channel, repo)
httpServer := httptest.NewServer(server.Handler(channel))
defer httpServer.Close()
resp, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp.Body.Close()
server.Close()
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
assert.Len(t, body, 0)
})
t.Run("all events are replayed if ReplayAll is true", func(t *testing.T) {
server := NewServer()
server.ReplayAll = true
server.Register(channel, repo)
httpServer := httptest.NewServer(server.Handler(channel))
defer httpServer.Close()
resp, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp.Body.Close()
server.Close()
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "id: replayed-from-start\ndata: example\n\n", string(body))
})
t.Run("events are replayed selectively if ReplayAll is false and Last-Event-Id is specified", func(t *testing.T) {
server := NewServer()
server.Register(channel, repo)
httpServer := httptest.NewServer(server.Handler(channel))
defer httpServer.Close()
req, err := http.NewRequest("GET", httpServer.URL, nil)
require.NoError(t, err)
req.Header.Set("Last-Event-Id", "some-id")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
server.Close()
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "id: replayed-from-some-id\ndata: example\n\n", string(body))
})
t.Run("repository is no longer used after being unregistered", func(t *testing.T) {
server := NewServer()
server.ReplayAll = true
server.Register(channel, repo)
server.Unregister(channel, false)
httpServer := httptest.NewServer(server.Handler(channel))
defer httpServer.Close()
resp, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp.Body.Close()
server.Close()
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
assert.Len(t, body, 0)
})
}
func TestServerCanDisconnectClientsWhenUnregisteringRepository(t *testing.T) {
channel := "test"
repo := &testServerRepository{}
server := NewServer()
server.Register(channel, repo)
httpServer := httptest.NewServer(server.Handler(channel))
defer httpServer.Close()
resp1, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp1.Body.Close()
resp2, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp2.Body.Close()
event1 := &publication{data: "my-event1"}
ackCh := server.PublishWithAcknowledgment([]string{channel}, event1)
<-ackCh
server.Unregister(channel, true)
event2 := &publication{data: "my-event2"}
ackCh = server.PublishWithAcknowledgment([]string{channel}, event2)
<-ackCh
server.Close()
expected := "data: my-event1\n\n"
body1, err := ioutil.ReadAll(resp1.Body)
require.NoError(t, err)
body2, err := ioutil.ReadAll(resp2.Body)
require.NoError(t, err)
assert.Equal(t, expected, string(body1))
assert.Equal(t, expected, string(body2))
}
func TestServerHandlerHasNoMaxConnectionTimeByDefault(t *testing.T) {
server := NewServer()
defer server.Close()
httpServer := httptest.NewServer(server.Handler("test"))
defer httpServer.Close()
resp, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp.Body.Close()
readCh := make(chan []byte)
go func() {
bytes, _ := ioutil.ReadAll(resp.Body)
readCh <- bytes
}()
select {
case <-readCh:
assert.Fail(t, "Unexpectedly got end of response")
case <-time.After(time.Millisecond * 400):
break
}
}
func TestServerHandlerCanEnforceMaxConnectionTime(t *testing.T) {
server := NewServer()
server.MaxConnTime = time.Millisecond * 200
defer server.Close()
httpServer := httptest.NewServer(server.Handler("test"))
defer httpServer.Close()
startTime := time.Now()
resp, err := http.Get(httpServer.URL)
require.NoError(t, err)
defer resp.Body.Close()
readCh := make(chan []byte)
go func() {
bytes, _ := ioutil.ReadAll(resp.Body)
readCh <- bytes
}()
select {
case <-readCh:
assert.GreaterOrEqual(t, time.Now().Sub(startTime).Milliseconds(), int64(200))
case <-time.After(time.Millisecond * 400):
assert.Fail(t, "Timed out without response being closed")
}
}
func TestServerHandlerExitsIfClientClosesConnection(t *testing.T) {
server := NewServer()
defer server.Close()
serverHandler := server.Handler("test")
startedCh := make(chan struct{}, 1)
endedCh := make(chan struct{}, 1)
testHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
startedCh <- struct{}{}
serverHandler.ServeHTTP(w, req)
endedCh <- struct{}{}
})
httpServer := httptest.NewServer(testHandler)
defer httpServer.Close()
req, err := http.NewRequest("GET", httpServer.URL, nil)
require.NoError(t, err)
ctx, canceller := context.WithCancel(context.Background())
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
require.NoError(t, err)
defer resp.Body.Close()
select {
case <-startedCh: // handler has received the request and is blocking for events
break
case <-time.After(time.Second):
require.Fail(t, "timed out waiting for handler to start")
}
canceller() // causes the socket to be closed
select {
case <-endedCh:
break
case <-time.After(time.Second):
require.Fail(t, "timed out waiting for handler to end")
}
}