forked from philippseith/signalr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhubcontext_test.go
341 lines (317 loc) · 9.42 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package signalr
import (
"context"
"fmt"
"net"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type contextHub struct {
Hub
}
func (c *contextHub) OnConnected(string) {
}
func (c *contextHub) CallAll() {
c.Clients().All().Send("clientFunc")
}
func (c *contextHub) CallCaller() {
c.Clients().Caller().Send("clientFunc")
}
func (c *contextHub) CallClient(connectionID string) {
c.Clients().Client(connectionID).Send("clientFunc")
}
func (c *contextHub) BuildGroup(connectionID1 string, connectionID2 string) {
c.Groups().AddToGroup("local", connectionID1)
c.Groups().AddToGroup("local", connectionID2)
}
func (c *contextHub) RemoveFromGroup(connectionID string) {
c.Groups().RemoveFromGroup("local", connectionID)
}
func (c *contextHub) CallGroup() {
c.Clients().Group("local").Send("clientFunc")
}
func (c *contextHub) AddItem(key string, value interface{}) {
c.Items().Store(key, value)
}
func (c *contextHub) GetItem(key string) interface{} {
if item, ok := c.Items().Load(key); ok {
return item
}
return nil
}
func (c *contextHub) TestConnectionID() {
}
func (c *contextHub) Abort() {
c.Hub.Abort()
}
type SimpleReceiver struct {
ch chan struct{}
}
func (sr *SimpleReceiver) ClientFunc() {
close(sr.ch)
}
func makeTCPServerAndClients(ctx context.Context, clientCount int) (Server, []Client, []*SimpleReceiver, []Connection, []Connection, error) {
server, err := NewServer(ctx, SimpleHubFactory(&contextHub{}), testLoggerOption())
if err != nil {
return nil, nil, nil, nil, nil, err
}
cliConn := make([]Connection, clientCount)
srvConn := make([]Connection, clientCount)
receiver := make([]*SimpleReceiver, clientCount)
client := make([]Client, clientCount)
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return nil, nil, nil, nil, nil, err
}
for i := 0; i < clientCount; i++ {
listener, err := net.ListenTCP("tcp", addr)
go func(i int) {
for {
tcpConn, _ := listener.Accept()
conn := NewNetConnection(ctx, tcpConn)
conn.SetConnectionID(fmt.Sprint(i))
srvConn[i] = conn
go func() { _ = server.Serve(conn) }()
break
}
}(i)
tcpConn, err := net.Dial("tcp",
fmt.Sprintf("localhost:%v", listener.Addr().(*net.TCPAddr).Port))
if err != nil {
return nil, nil, nil, nil, nil, err
}
cliConn[i] = NewNetConnection(ctx, tcpConn)
receiver[i] = &SimpleReceiver{ch: make(chan struct{})}
client[i], err = NewClient(ctx, WithConnection(cliConn[i]), WithReceiver(receiver[i]), TransferFormat("Text"), testLoggerOption())
if err != nil {
return nil, nil, nil, nil, nil, err
}
client[i].Start()
select {
case err := <-client[i].WaitForState(ctx, ClientConnected):
if err != nil {
return nil, nil, nil, nil, nil, err
}
case <-ctx.Done():
return nil, nil, nil, nil, nil, ctx.Err()
}
}
return server, client, receiver, srvConn, cliConn, nil
}
func makePipeClientsAndReceivers() ([]Client, []*SimpleReceiver, context.CancelFunc) {
cliConn := make([]*pipeConnection, 3)
srvConn := make([]*pipeConnection, 3)
for i := 0; i < 3; i++ {
cliConn[i], srvConn[i] = newClientServerConnections()
cliConn[i].SetConnectionID(fmt.Sprint(i))
srvConn[i].SetConnectionID(fmt.Sprint(i))
}
ctx, cancel := context.WithCancel(context.Background())
server, _ := NewServer(ctx, SimpleHubFactory(&contextHub{}), testLoggerOption())
var wg sync.WaitGroup
wg.Add(3)
for i := 0; i < 3; i++ {
go func(i int) {
wg.Done()
_ = server.Serve(srvConn[i])
}(i)
}
wg.Wait()
client := make([]Client, 3)
receiver := make([]*SimpleReceiver, 3)
for i := 0; i < 3; i++ {
receiver[i] = &SimpleReceiver{ch: make(chan struct{})}
client[i], _ = NewClient(ctx, WithConnection(cliConn[i]), WithReceiver(receiver[i]), testLoggerOption())
client[i].Start()
<-client[i].WaitForState(ctx, ClientConnected)
}
return client, receiver, cancel
}
var _ = Describe("HubContext", func() {
for i := 0; i < 10; i++ {
Context("Clients().All()", func() {
It("should invoke all clients", func() {
client, receiver, cancel := makePipeClientsAndReceivers()
r := <-client[0].Invoke("CallAll")
Expect(r.Error).NotTo(HaveOccurred())
result := 0
for result < 3 {
select {
case <-receiver[0].ch:
result++
case <-receiver[1].ch:
result++
case <-receiver[2].ch:
result++
case <-time.After(2 * time.Second):
Fail("timeout waiting for clients getting results")
}
}
cancel()
})
})
Context("Clients().Caller()", func() {
It("should invoke only the caller", func() {
client, receiver, cancel := makePipeClientsAndReceivers()
r := <-client[0].Invoke("CallCaller")
Expect(r.Error).NotTo(HaveOccurred())
select {
case <-receiver[0].ch:
case <-receiver[1].ch:
Fail("Wrong client received message")
case <-receiver[2].ch:
Fail("Wrong client received message")
case <-time.After(2 * time.Second):
Fail("timeout waiting for clients getting results")
}
cancel()
})
})
Context("Clients().Client()", func() {
It("should invoke only the client which was addressed", func() {
client, receiver, cancel := makePipeClientsAndReceivers()
r := <-client[0].Invoke("CallClient", "1")
Expect(r.Error).NotTo(HaveOccurred())
select {
case <-receiver[0].ch:
Fail("Wrong client received message")
case <-receiver[1].ch:
case <-receiver[2].ch:
Fail("Wrong client received message")
case <-time.After(2 * time.Second):
Fail("timeout waiting for clients getting results")
}
cancel()
})
})
}
})
func TestGroupShouldInvokeOnlyTheClientsInTheGroup(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, client, receiver, srvConn, _, err := makeTCPServerAndClients(ctx, 3)
assert.NoError(t, err)
select {
case ir := <-client[0].Invoke("buildgroup", srvConn[1].ConnectionID(), srvConn[2].ConnectionID()):
assert.NoError(t, ir.Error)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
select {
case ir := <-client[0].Invoke("callgroup"):
assert.NoError(t, ir.Error)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
gotCalled := 0
select {
case <-receiver[0].ch:
assert.Fail(t, "client 1 received message for client 2, 3")
case <-receiver[1].ch:
gotCalled++
case <-receiver[2].ch:
gotCalled++
case <-time.After(100 * time.Millisecond):
if gotCalled < 2 {
assert.Fail(t, "timeout without client 2 and 3 got called")
}
}
}
func TestRemoveClientsShouldRemoveClientsFromTheGroup(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, client, receiver, srvConn, _, err := makeTCPServerAndClients(ctx, 3)
assert.NoError(t, err)
select {
case ir := <-client[0].Invoke("buildgroup", srvConn[1].ConnectionID(), srvConn[2].ConnectionID()):
assert.NoError(t, ir.Error)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
select {
case ir := <-client[0].Invoke("removefromgroup", srvConn[2].ConnectionID()):
assert.NoError(t, ir.Error)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
select {
case ir := <-client[0].Invoke("callgroup"):
assert.NoError(t, ir.Error)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
gotCalled := false
select {
case <-receiver[0].ch:
assert.Fail(t, "client 1 received message for client 2")
case <-receiver[1].ch:
gotCalled = true
case <-receiver[2].ch:
assert.Fail(t, "client 3 received message for client 2")
case <-time.After(100 * time.Millisecond):
if !gotCalled {
assert.Fail(t, "timeout without client 3 got called")
}
}
}
func TestItemsShouldHoldItemsConnectionWise(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, client, _, _, _, err := makeTCPServerAndClients(ctx, 2)
assert.NoError(t, err)
select {
case ir := <-client[0].Invoke("additem", "first", 1):
assert.NoError(t, ir.Error)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
select {
case ir := <-client[0].Invoke("getitem", "first"):
assert.NoError(t, ir.Error)
assert.Equal(t, ir.Value, 1.0)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
select {
case ir := <-client[1].Invoke("getitem", "first"):
assert.NoError(t, ir.Error)
assert.Equal(t, ir.Value, nil)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
}
func TestAbortShouldAbortTheConnectionOfTheCurrentCaller(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, client, _, _, _, err := makeTCPServerAndClients(ctx, 2)
assert.NoError(t, err)
select {
case ir := <-client[0].Invoke("abort"):
assert.Error(t, ir.Error)
select {
case err := <-client[0].WaitForState(ctx, ClientClosed):
assert.NoError(t, err)
case <-time.After(500 * time.Millisecond):
assert.Fail(t, "timeout waiting for client close")
}
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
select {
case ir := <-client[1].Invoke("additem", "first", 2):
assert.NoError(t, ir.Error)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
select {
case ir := <-client[1].Invoke("getitem", "first"):
assert.NoError(t, ir.Error)
assert.Equal(t, ir.Value, 2.0)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "timeout in invoke")
}
}