forked from davidfowl/signalr-ports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvocation_test.go
253 lines (229 loc) · 8.16 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
package signalr
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"strings"
"time"
)
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() {
Context("When invoked by the client", func() {
It("should be invoked and return a completion", func() {
conn := connect(&invocationHub{})
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(""))
})
})
})
Describe("Non blocking invocation", func() {
Context("When invoked by the client", func() {
It("should be invoked and return no completion", func() {
conn := connect(&invocationHub{})
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):
}
})
})
})
Describe("Invalid invocation", func() {
Context("When an invalid invocation message is sent", func() {
It("close the connection with error", func() {
conn := connect(&invocationHub{})
// 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")
}
})
})
})
Describe("Invalid json", func() {
Context("when invalid json is received", func() {
It("should close the connection with an error", func() {
conn := connect(&invocationHub{})
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")
}
})
})
})
Describe("SimpleInt invocation", func() {
Context("When invoked by the client", func() {
It("should be invoked on the server, get an int and return an int", func() {
conn := connect(&invocationHub{})
var value int
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(""))
})
})
})
Describe("SimpleInt invocation with invalid argument", func() {
Context("When invoked by the client with an invalid argument", func() {
It("should not be invoked on the server and return an error", func() {
conn := connect(&invocationHub{})
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"))
})
})
})
Describe("SimpleFloat invocation", func() {
Context("When invoked by the client", func() {
It("should be invoked on the server, get a float and return a two floats", func() {
conn := connect(&invocationHub{})
var value float64
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(""))
})
})
})
Describe("SimpleString invocation", func() {
Context("When invoked by the client", func() {
It("should be invoked on the server, get two strings and return a string", func() {
conn := connect(&invocationHub{})
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(""))
})
})
})
Describe("Async invocation", func() {
Context("When invoked by the client", func() {
It("should be invoked on the server and return true asynchronously", func() {
conn := connect(&invocationHub{})
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(""))
})
})
})
Describe("Async invocation with buggy server method which returns a closed channel", func() {
Context("When invoked by the client", func() {
It("should be invoked on the server and return an error", func() {
conn := connect(&invocationHub{})
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())
})
})
})
Describe("Panic in invoked func", func() {
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() {
conn := connect(&invocationHub{})
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(""))
})
})
})
Describe("Missing method invocation", func() {
Context("When a missing server method invoked by the client", func() {
It("should return an error", func() {
conn := connect(&invocationHub{})
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))
})
})
})
})