forked from philippseith/signalr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serveroptions_test.go
416 lines (394 loc) · 13.7 KB
/
serveroptions_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
package signalr
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/go-kit/log"
"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type singleHub struct {
Hub
id string
}
func (s *singleHub) OnConnected(string) {
s.initUUID()
singleHubMsg <- s.id
}
func (s *singleHub) GetUUID() string {
s.initUUID()
singleHubMsg <- s.id
return s.id
}
func (s *singleHub) initUUID() {
if s.id == "" {
s.id = uuid.New().String()
}
}
var singleHubMsg = make(chan string, 100)
var _ = Describe("Server options", func() {
Describe("UseHub option", func() {
Context("When the UseHub option is used", func() {
It("should use the same hub instance on all invocations", func(done Done) {
server, err := NewServer(context.TODO(), UseHub(&singleHub{}))
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn1 := newTestingConnectionForServer()
Expect(conn1).NotTo(BeNil())
go func() { _ = server.Serve(conn1) }()
<-singleHubMsg
conn2 := newTestingConnectionForServer()
Expect(conn2).NotTo(BeNil())
go func() { _ = server.Serve(conn2) }()
<-singleHubMsg
// Call GetId twice. If different instances used the results are different
var uuid string
conn1.ClientSend(`{"type":1,"invocationId": "123","target":"getuuid"}`)
<-singleHubMsg
select {
case message := <-conn1.received:
Expect(message).To(BeAssignableToTypeOf(completionMessage{}))
uuid = fmt.Sprint(message.(completionMessage).Result)
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
conn1.ClientSend(`{"type":1,"invocationId": "456","target":"getuuid"}`)
<-singleHubMsg
select {
case message := <-conn1.received:
Expect(message).To(BeAssignableToTypeOf(completionMessage{}))
Expect(fmt.Sprint(message.(completionMessage).Result)).To(Equal(uuid))
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
// Even on another connection, the uuid should be the same
conn2.ClientSend(`{"type":1,"invocationId": "456","target":"getuuid"}`)
<-singleHubMsg
select {
case message := <-conn2.received:
Expect(message).To(BeAssignableToTypeOf(completionMessage{}))
Expect(fmt.Sprint(message.(completionMessage).Result)).To(Equal(uuid))
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
close(done)
}, 3.0)
})
Context("When UseHub is used on a client", func() {
It("should return an error", func(done Done) {
_, err := NewClient(context.TODO(), WithConnection(newTestingConnection()), testLoggerOption(), UseHub(&singleHub{}))
Expect(err).To(HaveOccurred())
close(done)
})
})
})
Describe("SimpleHubFactory option", func() {
Context("When the SimpleHubFactory option is used", func() {
It("should call the hub factory on each hub method invocation", func(done Done) {
server, err := NewServer(context.TODO(), SimpleHubFactory(&singleHub{}), testLoggerOption())
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnectionForServer()
Expect(conn).NotTo(BeNil())
go func() { _ = server.Serve(conn) }()
uuids := make(map[string]interface{})
uuid := <-singleHubMsg
if _, ok := uuids[uuid]; ok {
Fail("same hub called twice")
} else {
uuids[uuid] = nil
}
conn.ClientSend(`{"type":1,"invocationId": "123","target":"getuuid"}`)
select {
case uuid = <-singleHubMsg:
if _, ok := uuids[uuid]; ok {
Fail("same hub called twice")
} else {
uuids[uuid] = nil
}
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
conn.ClientSend(`{"type":1,"invocationId": "456","target":"getuuid"}`)
select {
case uuid = <-singleHubMsg:
if _, ok := uuids[uuid]; ok {
Fail("same hub called twice")
} else {
uuids[uuid] = nil
}
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
close(done)
}, 2.0)
})
Context("When SimpleHubFactory is used on a client", func() {
It("should return an error", func(done Done) {
_, err := NewClient(context.TODO(), WithConnection(newTestingConnection()), testLoggerOption(), SimpleHubFactory(&singleHub{}))
Expect(err).To(HaveOccurred())
close(done)
})
})
})
Describe("Logger option", func() {
Context("When the Logger option with debug false is used", func() {
It("calling a method correctly should log no events", func(done Done) {
cw := newChannelWriter()
server, err := NewServer(context.TODO(), UseHub(&invocationHub{}), Logger(log.NewLogfmtLogger(cw), false))
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnectionForServer()
Expect(conn).NotTo(BeNil())
go func() { _ = server.Serve(conn) }()
conn.ClientSend(`{"type":1,"invocationId": "123","target":"simple"}`)
<-invocationQueue
select {
case logEntry := <-cw.Chan():
Fail(fmt.Sprintf("log entry written: %v", string(logEntry)))
case <-time.After(1000 * time.Millisecond):
break
}
close(done)
}, 2.0)
})
Context("When the Logger option with debug true is used", func() {
It("calling a method correctly should log events", func(done Done) {
cw := newChannelWriter()
server, err := NewServer(context.TODO(), UseHub(&invocationHub{}), Logger(log.NewLogfmtLogger(cw), true))
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnectionForServer()
Expect(conn).NotTo(BeNil())
go func() { _ = server.Serve(conn) }()
conn.ClientSend(`{"type":1,"invocationId": "123","target":"simple"}`)
<-invocationQueue
select {
case <-cw.Chan():
break
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
close(done)
}, 2.0)
})
Context("When the Logger option with debug false is used", func() {
It("calling a method incorrectly should log events", func(done Done) {
cw := newChannelWriter()
server, err := NewServer(context.TODO(), UseHub(&invocationHub{}), Logger(log.NewLogfmtLogger(cw), false))
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnectionForServer()
Expect(conn).NotTo(BeNil())
go func() { _ = server.Serve(conn) }()
conn.ClientSend(`{"type":1,"invocationId": "123","target":"sumple is simple with typo"}`)
select {
case <-cw.Chan():
break
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
close(done)
}, 2.0)
})
Context("When no option which sets the hub type is used, NewServer", func() {
It("should return an error", func(done Done) {
_, err := NewServer(context.TODO(), testLoggerOption())
Expect(err).NotTo(BeNil())
close(done)
})
})
Context("When an option returns an error, NewServer", func() {
It("should return an error", func(done Done) {
_, err := NewServer(context.TODO(), func(Party) error { return errors.New("bad option") })
Expect(err).NotTo(BeNil())
close(done)
})
})
})
Describe("EnableDetailedErrors option", func() {
Context("When the EnableDetailedErrors option false is used, calling a method which panics", func() {
It("should return a completion, which contains only the panic", func(done Done) {
server, err := NewServer(context.TODO(), UseHub(&invocationHub{}), testLoggerOption())
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnectionForServer()
Expect(conn).NotTo(BeNil())
go func() { _ = server.Serve(conn) }()
conn.ClientSend(`{"type":1,"invocationId": "ppp","target":"Panic"}`)
<-invocationQueue
select {
case m := <-conn.ReceiveChan():
Expect(m).To(BeAssignableToTypeOf(completionMessage{}))
cm := m.(completionMessage)
Expect(cm.Error).To(Equal("Don't panic!\n"))
case <-time.After(100 * time.Millisecond):
Fail("timed out")
}
close(done)
}, 1.0)
})
Context("When the EnableDetailedErrors option true is used, calling a method which panics", func() {
It("should return a completion, which contains only the panic", func(done Done) {
server, err := NewServer(context.TODO(), UseHub(&invocationHub{}), EnableDetailedErrors(true), testLoggerOption())
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnectionForServer()
Expect(conn).NotTo(BeNil())
go func() { _ = server.Serve(conn) }()
conn.ClientSend(`{"type":1,"invocationId": "ppp","target":"Panic"}`)
<-invocationQueue
select {
case m := <-conn.ReceiveChan():
Expect(m).To(BeAssignableToTypeOf(completionMessage{}))
cm := m.(completionMessage)
Expect(cm.Error).NotTo(Equal("Don't panic!\n"))
case <-time.After(100 * time.Millisecond):
Fail("timed out")
}
close(done)
}, 1.0)
})
})
Describe("TimeoutInterval option", func() {
Context("When the TimeoutInterval has expired without any client message", func() {
It("the connection should be closed", func(done Done) {
server, err := NewServer(context.TODO(), UseHub(&invocationHub{}), TimeoutInterval(100*time.Millisecond), testLoggerOption())
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnectionForServer()
Expect(conn).NotTo(BeNil())
go func() { _ = server.Serve(conn) }()
time.Sleep(200 * time.Millisecond)
conn.ClientSend(`{"type":1,"invocationId": "timeout100","target":"Simple"}`)
select {
case m := <-conn.ReceiveChan():
Expect(m).To(BeAssignableToTypeOf(closeMessage{}))
cm := m.(closeMessage)
Expect(cm.Error).NotTo(BeNil())
case <-time.After(200 * time.Millisecond):
Fail("timed out")
case <-invocationQueue:
Fail("hub method invoked")
}
close(done)
}, 2.0)
})
})
Describe("KeepAliveInterval option", func() {
Context("When the KeepAliveInterval has expired without any server message", func() {
It("a ping should have been sent", func(done Done) {
server, err := NewServer(context.TODO(), UseHub(&invocationHub{}), KeepAliveInterval(200*time.Millisecond), testLoggerOption())
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnection()
go func() { _ = server.Serve(conn) }()
// Send initial Handshake
conn.ClientSend(`{"protocol": "json","version": 1}`)
// Handshake response
hr, _ := conn.ClientReceive()
Expect(hr).To(Equal("{}"))
for i := 0; i < 5; i++ {
// Wait for ping
hmc := make(chan interface{}, 1)
go func() {
m, _ := conn.ClientReceive()
hmc <- m
}()
select {
case m := <-hmc:
Expect(strings.TrimRight(m.(string), "\n")).To(Equal("{\"type\":6}"))
case <-time.After(600 * time.Millisecond):
Fail("timed out")
}
}
server.cancel()
close(done)
}, 2.0)
})
})
Describe("StreamBufferCapacity option", func() {
Context("When the StreamBufferCapacity is 0", func() {
It("should return an error", func(done Done) {
_, err := NewServer(context.TODO(), UseHub(&singleHub{}), StreamBufferCapacity(0), testLoggerOption())
Expect(err).NotTo(BeNil())
close(done)
})
})
})
Describe("MaximumReceiveMessageSize option", func() {
Context("When the MaximumReceiveMessageSize is 0", func() {
It("should return an error", func(done Done) {
_, err := NewServer(context.TODO(), UseHub(&singleHub{}), MaximumReceiveMessageSize(0), testLoggerOption())
Expect(err).NotTo(BeNil())
close(done)
})
})
})
Describe("HTTPTransports option", func() {
Context("When HTTPTransports is one of WebSockets, ServerSentEvents or both", func() {
It("should set these transports", func(done Done) {
s, err := NewServer(context.TODO(), UseHub(&singleHub{}), HTTPTransports(TransportWebSockets), testLoggerOption())
Expect(err).NotTo(HaveOccurred())
Expect(s.availableTransports()).To(ContainElement(TransportWebSockets))
close(done)
})
It("should set these transports", func(done Done) {
s, err := NewServer(context.TODO(), UseHub(&singleHub{}), HTTPTransports(TransportServerSentEvents), testLoggerOption())
Expect(err).NotTo(HaveOccurred())
Expect(s.availableTransports()).To(ContainElement(TransportServerSentEvents))
close(done)
})
It("should set these transports", func(done Done) {
s, err := NewServer(context.TODO(), UseHub(&singleHub{}), HTTPTransports(TransportServerSentEvents, TransportWebSockets), testLoggerOption())
Expect(err).NotTo(HaveOccurred())
Expect(s.availableTransports()).To(ContainElement(TransportWebSockets))
Expect(s.availableTransports()).To(ContainElement(TransportServerSentEvents))
close(done)
})
})
Context("When HTTPTransports is none of WebSockets, ServerSentEvents", func() {
It("should return an error", func(done Done) {
_, err := NewServer(context.TODO(), UseHub(&singleHub{}), HTTPTransports("WebTransport"), testLoggerOption())
Expect(err).To(HaveOccurred())
close(done)
})
})
Context("When HTTPTransports is used on a client", func() {
It("should return an error", func(done Done) {
_, err := NewClient(context.TODO(), WithConnection(newTestingConnection()), HTTPTransports(TransportServerSentEvents), testLoggerOption())
Expect(err).To(HaveOccurred())
close(done)
})
})
})
})
type channelWriter struct {
channel chan []byte
}
func (c *channelWriter) Write(p []byte) (n int, err error) {
c.channel <- p
return len(p), nil
}
func (c *channelWriter) Chan() chan []byte {
return c.channel
}
func newChannelWriter() *channelWriter {
return &channelWriter{make(chan []byte, 100)}
}
// type mapLogger struct {
// c chan bool
// m map[string]string
// }
//
// func (m *mapLogger) Log(keyvals ...interface{}) error {
// m.m = make(map[string]string)
// for i := 0; i < len(keyvals); i += 2 {
// m.m[keyvals[i].(string)] = keyvals[i+1].(string)
// }
// m.c <- true
// return nil
// }