forked from davidfowl/signalr-ports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubcontext_test.go
320 lines (298 loc) · 9.95 KB
/
hubcontext_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
package signalr
import (
"fmt"
"github.com/go-kit/kit/log"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"os"
"strings"
"time"
)
type contextHub struct {
Hub
}
var hubContextOnConnectMsg = make(chan string, 1)
func (c *contextHub) OnConnected(connectionID string) {
hubContextOnConnectMsg <- connectionID
}
func (c *contextHub) CallAll() {
c.Clients().All().Send("clientFunc")
hubContextInvocationQueue <- "CallAll()"
}
func (c *contextHub) CallCaller() {
c.Clients().Caller().Send("clientFunc")
hubContextInvocationQueue <- "CallCaller()"
}
func (c *contextHub) CallClient(connectionID string) {
c.Clients().Client(connectionID).Send("clientFunc")
hubContextInvocationQueue <- "CallClient()"
}
func (c *contextHub) BuildGroup(connectionID1 string, connectionID2 string) {
c.Groups().AddToGroup("local", connectionID1)
c.Groups().AddToGroup("local", connectionID2)
hubContextInvocationQueue <- "BuildGroup()"
}
func (c *contextHub) RemoveFromGroup(connectionID string) {
c.Groups().RemoveFromGroup("local", connectionID)
hubContextInvocationQueue <- "RemoveFromGroup()"
}
func (c *contextHub) CallGroup() {
c.Clients().Group("local").Send("clientFunc")
hubContextInvocationQueue <- "CallGroup()"
}
func (c *contextHub) AddItem(key string, value interface{}) {
c.Items()[key] = value
hubContextInvocationQueue <- "AddItem()"
}
func (c *contextHub) GetItem(key string) interface{} {
hubContextInvocationQueue <- "GetItem()"
return c.Items()[key]
}
var hubContextInvocationQueue = make(chan string, 10)
func connectMany() []*testingConnection {
server, err := NewServer(SimpleHubFactory(&contextHub{}),
Logger(log.NewLogfmtLogger(os.Stderr), false))
if err != nil {
Fail(err.Error())
return nil
}
conns := make([]*testingConnection, 3)
for i := 0; i < 3; i++ {
conns[i] = newTestingConnection()
go server.Run(conns[i])
// Ensure to return all connection with connected hubs
<-hubContextOnConnectMsg
}
return conns
}
var _ = Describe("HubContext", func() {
Context("Clients().All()", func() {
It("should invoke all clients", func() {
conns := connectMany()
conns[0].ClientSend(`{"type":1,"invocationId": "123","target":"callall"}`)
callCount := make(chan int, 1)
callCount <- 0
done := make(chan bool)
go func(conns []*testingConnection, callCount chan int, done chan bool) {
msg := <-conns[0].received
if _, ok := msg.(completionMessage); ok {
msg = <-conns[0].received
expectInvocation(msg, callCount, done, 3)
} else {
expectInvocation(msg, callCount, done, 3)
}
}(conns, callCount, done)
go func(conns []*testingConnection, callCount chan int, done chan bool) {
msg := <-conns[1].received
expectInvocation(msg, callCount, done, 3)
}(conns, callCount, done)
go func(conns []*testingConnection, callCount chan int, done chan bool) {
msg := <-conns[2].received
expectInvocation(msg, callCount, done, 3)
}(conns, callCount, done)
Expect(<-hubContextInvocationQueue).To(Equal("CallAll()"))
select {
case <-done:
break
case <-time.After(3000 * time.Millisecond):
Fail("timed out")
}
})
})
Context("Clients().Caller()", func() {
It("should invoke only the caller", func() {
conns := connectMany()
conns[0].ClientSend(`{"type":1,"invocationId": "123","target":"callcaller"}`)
done := make(chan bool)
callCount := make(chan int, 1)
callCount <- 0
go func(conns []*testingConnection, done chan bool) {
msg := <-conns[0].received
if _, ok := msg.(completionMessage); ok {
msg = <-conns[0].received
expectInvocation(msg, callCount, done, 1)
} else {
expectInvocation(msg, callCount, done, 1)
}
}(conns, done)
go func(conns []*testingConnection) {
msg := <-conns[1].received
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("non caller received %v", msg))
}
}(conns)
go func(conns []*testingConnection) {
msg := <-conns[2].received
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("non caller received %v", msg))
}
}(conns)
Expect(<-hubContextInvocationQueue).To(Equal("CallCaller()"))
select {
case <-done:
break
case <-time.After(3000 * time.Millisecond):
Fail("timed out")
}
})
})
Context("Clients().Client()", func() {
It("should invoke only the client which was addressed", func() {
conns := connectMany()
conns[0].ClientSend(fmt.Sprintf(`{"type":1,"invocationId": "123","target":"callclient","arguments":["%v"]}`, conns[2].ConnectionID()))
done := make(chan bool)
go func(conns []*testingConnection) {
msg := <-conns[0].received
if _, ok := msg.(completionMessage); ok {
msg = <-conns[0].received
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("wrong client received %v", msg))
}
} else {
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("wrong client received %v", msg))
}
}
}(conns)
go func(conns []*testingConnection) {
msg := <-conns[1].received
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("wrong client received %v", msg))
}
}(conns)
go func(conns []*testingConnection, done chan bool) {
msg := <-conns[2].received
Expect(msg).To(BeAssignableToTypeOf(invocationMessage{}))
Expect(strings.ToLower(msg.(invocationMessage).Target)).To(Equal("clientfunc"))
done <- true
}(conns, done)
Expect(<-hubContextInvocationQueue).To(Equal("CallClient()"))
select {
case <-done:
break
case <-time.After(3000 * time.Millisecond):
Fail("timed out")
}
})
})
Context("Clients().Group()", func() {
It("should invoke only the clients in the group", func() {
conns := connectMany()
conns[0].ClientSend(fmt.Sprintf(`{"type":1,"invocationId": "123","target":"buildgroup","arguments":["%v","%v"]}`, conns[1].ConnectionID(), conns[2].ConnectionID()))
<-hubContextInvocationQueue
<-conns[0].received
conns[0].ClientSend(`{"type":1,"invocationId": "123","target":"callgroup"}`)
callCount := make(chan int, 1)
callCount <- 0
done := make(chan bool)
go func(conns []*testingConnection) {
msg := <-conns[0].received
if _, ok := msg.(completionMessage); ok {
msg = <-conns[0].received
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("wrong client received %v", msg))
}
} else {
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("wrong client received %v", msg))
}
}
}(conns)
go func(conns []*testingConnection) {
msg := <-conns[1].received
expectInvocation(msg, callCount, done, 2)
}(conns)
go func(conns []*testingConnection, done chan bool) {
msg := <-conns[2].received
expectInvocation(msg, callCount, done, 2)
}(conns, done)
Expect(<-hubContextInvocationQueue).To(Equal("CallGroup()"))
select {
case <-done:
break
case <-time.After(3000 * time.Millisecond):
Fail("timed out")
}
})
})
Context("RemoveFromGroup should remove clients from the group", func() {
It("should invoke only the clients in the group", func() {
conns := connectMany()
conns[0].ClientSend(fmt.Sprintf(`{"type":1,"invocationId": "123","target":"buildgroup","arguments":["%v","%v"]}`, conns[1].ConnectionID(), conns[2].ConnectionID()))
Expect(<-hubContextInvocationQueue).To(Equal("BuildGroup()"))
<-conns[0].received
conns[2].ClientSend(fmt.Sprintf(`{"type":1,"invocationId": "123","target":"removefromgroup","arguments":["%v"]}`, conns[2].ConnectionID()))
Expect(<-hubContextInvocationQueue).To(Equal("RemoveFromGroup()"))
<-conns[2].received
// Now only conns[1] should be invoked
conns[0].ClientSend(`{"type":1,"invocationId": "123","target":"callgroup"}`)
callCount := make(chan int, 1)
callCount <- 0
done := make(chan bool)
go func(conns []*testingConnection) {
msg := <-conns[0].received
if _, ok := msg.(completionMessage); ok {
msg = <-conns[0].received
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("wrong client received %v", msg))
}
} else {
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("wrong client received %v", msg))
}
}
}(conns)
go func(conns []*testingConnection) {
msg := <-conns[1].received
expectInvocation(msg, callCount, done, 1)
}(conns)
go func(conns []*testingConnection, done chan bool) {
msg := <-conns[2].received
if _, ok := msg.(completionMessage); ok {
Fail(fmt.Sprintf("wrong client received %v", msg))
}
}(conns, done)
Expect(<-hubContextInvocationQueue).To(Equal("CallGroup()"))
select {
case <-done:
break
case <-time.After(3000 * time.Millisecond):
Fail("timed out")
}
})
})
Context("Items()", func() {
It("should hold Items connection wise", func() {
conns := connectMany()
conns[0].ClientSend(`{"type":1,"invocationId": "123","target":"additem","arguments":["first",1]}`)
// Wait for execution
Expect(<-hubContextInvocationQueue).To(Equal("AddItem()"))
// Read completion
<-conns[0].received
conns[0].ClientSend(`{"type":1,"invocationId": "123","target":"getitem","arguments":["first"]}`)
// Wait for execution
Expect(<-hubContextInvocationQueue).To(Equal("GetItem()"))
msg := <-conns[0].received
Expect(msg).To(BeAssignableToTypeOf(completionMessage{}))
Expect(msg.(completionMessage).Result).To(Equal(float64(1)))
// Ask on other connection
conns[1].ClientSend(`{"type":1,"invocationId": "123","target":"getitem","arguments":["first"]}`)
// Wait for execution
Expect(<-hubContextInvocationQueue).To(Equal("GetItem()"))
msg = <-conns[1].received
Expect(msg).To(BeAssignableToTypeOf(completionMessage{}))
Expect(msg.(completionMessage).Result).To(BeNil())
})
})
})
func expectInvocation(msg interface{}, callCount chan int, done chan bool, doneCount int) {
Expect(msg).To(BeAssignableToTypeOf(invocationMessage{}))
Expect(strings.ToLower(msg.(invocationMessage).Target)).To(Equal("clientfunc"))
d := <-callCount
d++
if d == doneCount {
done <- true
} else {
callCount <- d
}
}