forked from philippseith/signalr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testingconnection_test.go
251 lines (226 loc) · 5.69 KB
/
testingconnection_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
package signalr
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/onsi/ginkgo"
)
type testingConnection struct {
timeout time.Duration
connectionID string
srvWriter io.Writer
srvReader io.Reader
cliWriter io.Writer
cliReader io.Reader
received chan interface{}
cnMutex sync.Mutex
connected bool
cliSendChan chan string
srvSendChan chan []byte
failRead string
failWrite string
failMx sync.Mutex
}
func (t *testingConnection) Context() context.Context {
return context.TODO()
}
var connNum = 0
var connNumMx sync.Mutex
func (t *testingConnection) SetTimeout(timeout time.Duration) {
t.timeout = timeout
}
func (t *testingConnection) Timeout() time.Duration {
return t.timeout
}
func (t *testingConnection) ConnectionID() string {
connNumMx.Lock()
defer connNumMx.Unlock()
if t.connectionID == "" {
connNum++
t.connectionID = fmt.Sprintf("test%v", connNum)
}
return t.connectionID
}
func (t *testingConnection) SetConnectionID(id string) {
t.connectionID = id
}
func (t *testingConnection) Read(b []byte) (n int, err error) {
if fr := t.FailRead(); fr != "" {
defer func() { t.SetFailRead("") }()
return 0, errors.New(fr)
}
timer := make(<-chan time.Time)
if t.Timeout() > 0 {
timer = time.After(t.Timeout())
}
nch := make(chan int)
go func() {
n, _ := t.srvReader.Read(b)
nch <- n
}()
select {
case n := <-nch:
return n, nil
case <-timer:
return 0, fmt.Errorf("timeout %v", t.Timeout())
}
}
func (t *testingConnection) Write(b []byte) (n int, err error) {
if fw := t.FailWrite(); fw != "" {
defer func() { t.SetFailWrite("") }()
return 0, errors.New(fw)
}
t.srvSendChan <- b
return len(b), nil
}
func (t *testingConnection) Connected() bool {
t.cnMutex.Lock()
defer t.cnMutex.Unlock()
return t.connected
}
func (t *testingConnection) SetConnected(connected bool) {
t.cnMutex.Lock()
defer t.cnMutex.Unlock()
t.connected = connected
}
func (t *testingConnection) FailRead() string {
defer t.failMx.Unlock()
t.failMx.Lock()
return t.failRead
}
func (t *testingConnection) FailWrite() string {
defer t.failMx.Unlock()
t.failMx.Lock()
return t.failWrite
}
func (t *testingConnection) SetFailRead(fail string) {
defer t.failMx.Unlock()
t.failMx.Lock()
t.failRead = fail
}
func (t *testingConnection) SetFailWrite(fail string) {
defer t.failMx.Unlock()
t.failMx.Lock()
t.failWrite = fail
}
// newTestingConnectionForServer builds a testingConnection with an sent (but not yet received) handshake for testing a server
func newTestingConnectionForServer() *testingConnection {
conn := newTestingConnection()
// client receive loop
go receiveLoop(conn)()
// Send initial Handshake
conn.ClientSend(`{"protocol": "json","version": 1}`)
conn.SetConnected(true)
return conn
}
func newTestingConnection() *testingConnection {
cliReader, srvWriter := io.Pipe()
srvReader, cliWriter := io.Pipe()
conn := testingConnection{
srvWriter: srvWriter,
srvReader: srvReader,
cliWriter: cliWriter,
cliReader: cliReader,
received: make(chan interface{}, 20),
cliSendChan: make(chan string, 20),
srvSendChan: make(chan []byte, 20),
timeout: time.Second * 5,
}
// client send loop
go func() {
for {
_, _ = conn.cliWriter.Write(append([]byte(<-conn.cliSendChan), 30))
}
}()
// server send loop
go func() {
for {
_, _ = conn.srvWriter.Write(<-conn.srvSendChan)
}
}()
return &conn
}
func (t *testingConnection) ClientSend(message string) {
t.cliSendChan <- message
}
func (t *testingConnection) ClientReceive() (string, error) {
var buf bytes.Buffer
var data = make([]byte, 1<<15) // 32K
var nn int
for {
if message, err := buf.ReadString(30); err != nil {
buf.Write(data[:nn])
if n, err := t.cliReader.Read(data[nn:]); err == nil {
buf.Write(data[nn : nn+n])
nn = nn + n
} else {
return "", err
}
} else {
return message[:len(message)-1], nil
}
}
}
func (t *testingConnection) ReceiveChan() chan interface{} {
return t.received
}
type clientReceiver interface {
ClientReceive() (string, error)
ReceiveChan() chan interface{}
SetConnected(bool)
}
func receiveLoop(conn clientReceiver) func() {
return func() {
defer ginkgo.GinkgoRecover()
errorHandler := func(err error) { ginkgo.Fail(fmt.Sprintf("received invalid message from server %v", err.Error())) }
for {
if message, err := conn.ClientReceive(); err == nil {
var hubMessage hubMessage
if err = json.Unmarshal([]byte(message), &hubMessage); err == nil {
switch hubMessage.Type {
case 1, 4:
var invocationMessage invocationMessage
if err = json.Unmarshal([]byte(message), &invocationMessage); err == nil {
conn.ReceiveChan() <- invocationMessage
} else {
errorHandler(err)
}
case 2:
var jsonStreamItemMessage jsonStreamItemMessage
if err = json.Unmarshal([]byte(message), &jsonStreamItemMessage); err == nil {
conn.ReceiveChan() <- streamItemMessage{
Type: jsonStreamItemMessage.Type,
InvocationID: jsonStreamItemMessage.InvocationID,
Item: jsonStreamItemMessage.Item,
}
} else {
errorHandler(err)
}
case 3:
var completionMessage completionMessage
if err = json.Unmarshal([]byte(message), &completionMessage); err == nil {
conn.ReceiveChan() <- completionMessage
} else {
errorHandler(err)
}
case 7:
var closeMessage closeMessage
if err = json.Unmarshal([]byte(message), &closeMessage); err == nil {
conn.SetConnected(false)
conn.ReceiveChan() <- closeMessage
} else {
errorHandler(err)
}
}
} else {
errorHandler(err)
}
}
}
}
}