forked from philippseith/signalr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invocation_test.go
387 lines (361 loc) · 11.2 KB
/
invocation_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
package signalr
import (
"fmt"
"strings"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var invocationQueue = make(chan string, 20)
type invocationHub struct {
Hub
}
func (i *invocationHub) Simple() {
invocationQueue <- "Simple()"
}
func (i *invocationHub) SimpleInt(value int) int {
invocationQueue <- fmt.Sprintf("SimpleInt(%v)", value)
return value + 1
}
func (i *invocationHub) SimpleFloat(value float64) (float64, float64) {
invocationQueue <- fmt.Sprintf("SimpleFloat(%v)", value)
return value * 10.0, value * 100.0
}
func (i *invocationHub) SimpleString(value1 string, value2 string) string {
invocationQueue <- fmt.Sprintf("SimpleString(%v, %v)", value1, value2)
return strings.ToLower(value1 + value2)
}
func (i *invocationHub) Async() chan bool {
r := make(chan bool)
go func() {
defer close(r)
r <- true
}()
invocationQueue <- "Async()"
return r
}
func (i *invocationHub) AsyncClosedChan() chan bool {
r := make(chan bool)
close(r)
invocationQueue <- "AsyncClosedChan()"
return r
}
func (i *invocationHub) Panic() {
invocationQueue <- "Panic()"
panic("Don't panic!")
}
var _ = Describe("Invocation", func() {
Describe("Simple invocation", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When invoked by the client", func() {
It("should be invoked and return a completion", func(done Done) {
conn.ClientSend(`{"type":1,"invocationId": "123","target":"simple"}`)
Expect(<-invocationQueue).To(Equal("Simple()"))
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.InvocationID).To(Equal("123"))
Expect(recv.Result).To(BeNil())
Expect(recv.Error).To(Equal(""))
close(done)
}, 2.0)
})
Context("When invoked by the client two times in one frame", func() {
It("should be invoked and return a completion", func(done Done) {
conn.ClientSend(`{"type":1,"invocationId": "123","target":"simple"}`)
conn.ClientSend(`{"type":1,"invocationId": "123","target":"simple"}`)
Expect(<-invocationQueue).To(Equal("Simple()"))
Expect(<-invocationQueue).To(Equal("Simple()"))
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.InvocationID).To(Equal("123"))
Expect(recv.Result).To(BeNil())
Expect(recv.Error).To(Equal(""))
close(done)
}, 2.0)
})
})
Describe("Non blocking invocation", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When invoked by the client", func() {
It("should be invoked and return no completion", func(done Done) {
conn.ClientSend(`{"type":1,"target":"simple"}`)
Expect(<-invocationQueue).To(Equal("Simple()"))
select {
case message := <-conn.received:
if _, ok := message.(completionMessage); ok {
Fail("received completion ")
}
case <-time.After(1000 * time.Millisecond):
}
close(done)
}, 2.0)
})
})
Describe("Invalid invocation", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When an invalid invocation message is sent", func() {
It("close the connection with error", func(done Done) {
// Invalid. invocationId should be a string
conn.ClientSend(`{"type":1,"invocationId":1}`)
select {
case message := <-conn.received:
Expect(message).To(BeAssignableToTypeOf(closeMessage{}))
Expect(message.(closeMessage).Error).NotTo(BeNil())
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
close(done)
}, 2.0)
})
})
Describe("Invalid json", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("when invalid json is received", func() {
It("should close the connection with an error", func(done Done) {
conn.ClientSend(`{"type":1,"invocationId": "4444","target":"simpleint", arguments[CanNotParse]}`)
select {
case message := <-conn.received:
Expect(message).To(BeAssignableToTypeOf(closeMessage{}))
Expect(message.(closeMessage).Error).NotTo(BeNil())
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
close(done)
}, 2.0)
})
})
Describe("SimpleInt invocation", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When invoked by the client", func() {
It("should be invoked on the server, get an int and return an int", func(done Done) {
var value = 314
conn.ClientSend(fmt.Sprintf(
`{"type":1,"invocationId": "666","target":"simpleint","arguments":[%v]}`, value))
Expect(<-invocationQueue).To(Equal(fmt.Sprintf("SimpleInt(%v)", value)))
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.InvocationID).To(Equal("666"))
Expect(recv.Result).To(Equal(float64(value + 1))) // json makes all numbers float64
Expect(recv.Error).To(Equal(""))
close(done)
}, 2.0)
})
})
Describe("SimpleInt invocation with invalid argument", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When invoked by the client with an invalid argument", func() {
It("should not be invoked on the server and return an error", func(done Done) {
conn.ClientSend(
`{"type":1,"invocationId": "555","target":"simpleint","arguments":["CantParse"]}`)
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.Error).NotTo(Equal(""))
Expect(recv.InvocationID).To(Equal("555"))
close(done)
}, 2.0)
})
})
Describe("SimpleFloat invocation", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When invoked by the client", func() {
It("should be invoked on the server, get a float and return a two floats", func(done Done) {
var value = 3.1415
conn.ClientSend(fmt.Sprintf(
`{"type":1,"invocationId": "8087","target":"simplefloat","arguments":[%v]}`, value))
Expect(<-invocationQueue).To(Equal(fmt.Sprintf("SimpleFloat(%v)", value)))
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.InvocationID).To(Equal("8087"))
Expect(recv.Result).To(Equal([]interface{}{value * 10.0, value * 100.0}))
Expect(recv.Error).To(Equal(""))
close(done)
}, 2.0)
})
})
Describe("SimpleString invocation", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When invoked by the client", func() {
It("should be invoked on the server, get two strings and return a string", func(done Done) {
value1 := "Camel"
value2 := "Cased"
conn.ClientSend(fmt.Sprintf(
`{"type":1,"invocationId": "6502","target":"simplestring","arguments":["%v", "%v"]}`, value1, value2))
Expect(<-invocationQueue).To(Equal(fmt.Sprintf("SimpleString(%v, %v)", value1, value2)))
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.InvocationID).To(Equal("6502"))
Expect(recv.Result).To(Equal(strings.ToLower(value1 + value2)))
Expect(recv.Error).To(Equal(""))
close(done)
}, 2.0)
})
})
Describe("Async invocation", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When invoked by the client", func() {
It("should be invoked on the server and return true asynchronously", func(done Done) {
conn.ClientSend(`{"type":1,"invocationId": "mfg","target":"async"}`)
Expect(<-invocationQueue).To(Equal("Async()"))
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.InvocationID).To(Equal("mfg"))
Expect(recv.Result).To(Equal(true))
Expect(recv.Error).To(Equal(""))
close(done)
}, 2.0)
})
})
Describe("Async invocation with buggy server method which returns a closed channel", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When invoked by the client", func() {
It("should be invoked on the server and return an error", func(done Done) {
conn.ClientSend(`{"type":1,"invocationId": "ouch","target":"asyncclosedchan"}`)
Expect(<-invocationQueue).To(Equal("AsyncClosedChan()"))
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.InvocationID).To(Equal("ouch"))
Expect(recv.Result).To(BeNil())
Expect(recv.Error).NotTo(BeNil())
close(done)
}, 2.0)
})
})
Describe("Panic in invoked func", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When a func is invoked by the client and panics", func() {
It("should be invoked on the server and return an error but no result", func(done Done) {
conn.ClientSend(`{"type":1,"invocationId": "???","target":"panic"}`)
Expect(<-invocationQueue).To(Equal("Panic()"))
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.InvocationID).To(Equal("???"))
Expect(recv.Result).To(BeNil())
Expect(recv.Error).NotTo(Equal(""))
close(done)
}, 2.0)
})
})
Describe("Missing method invocation", func() {
var server Server
var conn *testingConnection
BeforeEach(func(done Done) {
server, conn = connect(&invocationHub{})
close(done)
})
AfterEach(func(done Done) {
server.cancel()
close(done)
})
Context("When a missing server method invoked by the client", func() {
It("should return an error", func(done Done) {
conn.ClientSend(`{"type":1,"invocationId": "0000","target":"missing"}`)
recv := (<-conn.received).(completionMessage)
Expect(recv).NotTo(BeNil())
Expect(recv.InvocationID).To(Equal("0000"))
Expect(recv.Result).To(BeNil())
Expect(recv.Error).NotTo(BeNil())
Expect(len(recv.Error)).To(BeNumerically(">", 0))
close(done)
}, 2.0)
})
})
})