-
Notifications
You must be signed in to change notification settings - Fork 38.6k
/
connection_test.go
333 lines (277 loc) · 7.89 KB
/
connection_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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package spdy
import (
"fmt"
"io"
"net"
"net/http"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/moby/spdystream"
"k8s.io/apimachinery/pkg/util/httpstream"
)
func runProxy(t *testing.T, backendUrl string, proxyUrl chan<- string, proxyDone chan<- struct{}, errCh chan<- error) {
listener, err := net.Listen("tcp4", "localhost:0")
if err != nil {
errCh <- err
return
}
defer listener.Close()
proxyUrl <- listener.Addr().String()
clientConn, err := listener.Accept()
if err != nil {
t.Errorf("proxy: error accepting client connection: %v", err)
return
}
backendConn, err := net.Dial("tcp4", backendUrl)
if err != nil {
t.Errorf("proxy: error dialing backend: %v", err)
return
}
defer backendConn.Close()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
io.Copy(backendConn, clientConn)
}()
go func() {
defer wg.Done()
io.Copy(clientConn, backendConn)
}()
wg.Wait()
proxyDone <- struct{}{}
}
func runServer(t *testing.T, backendUrl chan<- string, serverDone chan<- struct{}, errCh chan<- error) {
listener, err := net.Listen("tcp4", "localhost:0")
if err != nil {
errCh <- err
return
}
defer listener.Close()
backendUrl <- listener.Addr().String()
conn, err := listener.Accept()
if err != nil {
t.Errorf("server: error accepting connection: %v", err)
return
}
streamChan := make(chan httpstream.Stream)
replySentChan := make(chan (<-chan struct{}))
spdyConn, err := NewServerConnection(conn, func(stream httpstream.Stream, replySent <-chan struct{}) error {
streamChan <- stream
replySentChan <- replySent
return nil
})
if err != nil {
t.Errorf("server: error creating spdy connection: %v", err)
return
}
stream := <-streamChan
replySent := <-replySentChan
<-replySent
buf := make([]byte, 1)
_, err = stream.Read(buf)
if err != io.EOF {
t.Errorf("server: unexpected read error: %v", err)
return
}
<-spdyConn.CloseChan()
raw := spdyConn.(*connection).conn
if err := raw.Wait(15 * time.Second); err != nil {
t.Errorf("server: timed out waiting for connection closure: %v", err)
}
serverDone <- struct{}{}
}
func TestConnectionCloseIsImmediateThroughAProxy(t *testing.T) {
errCh := make(chan error)
serverDone := make(chan struct{}, 1)
backendUrlChan := make(chan string)
go runServer(t, backendUrlChan, serverDone, errCh)
var backendUrl string
select {
case err := <-errCh:
t.Fatalf("server: error listening: %v", err)
case backendUrl = <-backendUrlChan:
}
proxyDone := make(chan struct{}, 1)
proxyUrlChan := make(chan string)
go runProxy(t, backendUrl, proxyUrlChan, proxyDone, errCh)
var proxyUrl string
select {
case err := <-errCh:
t.Fatalf("error listening: %v", err)
case proxyUrl = <-proxyUrlChan:
}
conn, err := net.Dial("tcp4", proxyUrl)
if err != nil {
t.Fatalf("client: error connecting to proxy: %v", err)
}
spdyConn, err := NewClientConnection(conn)
if err != nil {
t.Fatalf("client: error creating spdy connection: %v", err)
}
if _, err := spdyConn.CreateStream(http.Header{}); err != nil {
t.Fatalf("client: error creating stream: %v", err)
}
spdyConn.Close()
raw := spdyConn.(*connection).conn
if err := raw.Wait(15 * time.Second); err != nil {
t.Fatalf("client: timed out waiting for connection closure: %v", err)
}
expired := time.NewTimer(15 * time.Second)
defer expired.Stop()
i := 0
for {
select {
case <-expired.C:
t.Fatalf("timed out waiting for proxy and/or server closure")
case <-serverDone:
i++
case <-proxyDone:
i++
}
if i == 2 {
break
}
}
}
func TestConnectionPings(t *testing.T) {
const pingPeriod = 10 * time.Millisecond
timeout := time.After(10 * time.Second)
// Set up server connection.
listener, err := net.Listen("tcp4", "localhost:0")
if err != nil {
t.Fatal(err)
}
defer listener.Close()
srvErr := make(chan error, 1)
go func() {
defer close(srvErr)
srvConn, err := listener.Accept()
if err != nil {
srvErr <- fmt.Errorf("server: error accepting connection: %v", err)
return
}
defer srvConn.Close()
spdyConn, err := spdystream.NewConnection(srvConn, true)
if err != nil {
srvErr <- fmt.Errorf("server: error creating spdy connection: %v", err)
return
}
var pingsSent int64
srvSPDYConn := newConnection(
spdyConn,
func(stream httpstream.Stream, replySent <-chan struct{}) error {
// Echo all the incoming data.
go io.Copy(stream, stream)
return nil
},
pingPeriod,
func() (time.Duration, error) {
atomic.AddInt64(&pingsSent, 1)
return 0, nil
})
defer srvSPDYConn.Close()
// Wait for the connection to close, to prevent defers from running
// early.
select {
case <-timeout:
srvErr <- fmt.Errorf("server: timeout waiting for connection to close")
return
case <-srvSPDYConn.CloseChan():
}
// Count pings sent by the server.
gotPings := atomic.LoadInt64(&pingsSent)
if gotPings < 1 {
t.Errorf("server: failed to send any pings (check logs)")
}
}()
// Set up client connection.
clConn, err := net.Dial("tcp4", listener.Addr().String())
if err != nil {
t.Fatalf("client: error connecting to proxy: %v", err)
}
defer clConn.Close()
start := time.Now()
clSPDYConn, err := NewClientConnection(clConn)
if err != nil {
t.Fatalf("client: error creating spdy connection: %v", err)
}
defer clSPDYConn.Close()
clSPDYStream, err := clSPDYConn.CreateStream(http.Header{})
if err != nil {
t.Fatalf("client: error creating stream: %v", err)
}
defer clSPDYStream.Close()
// Send some data both ways, to make sure pings don't interfere with
// regular messages.
in := "foo"
if _, err := fmt.Fprintln(clSPDYStream, in); err != nil {
t.Fatalf("client: error writing data to stream: %v", err)
}
var out string
if _, err := fmt.Fscanln(clSPDYStream, &out); err != nil {
t.Fatalf("client: error reading data from stream: %v", err)
}
if in != out {
t.Errorf("client: received data doesn't match sent data: got %q, want %q", out, in)
}
// Wait for at least 2 pings to get sent each way before closing the
// connection.
elapsed := time.Since(start)
if elapsed < 3*pingPeriod {
time.Sleep(3*pingPeriod - elapsed)
}
clSPDYConn.Close()
select {
case err, ok := <-srvErr:
if ok && err != nil {
t.Error(err)
}
case <-timeout:
t.Errorf("timed out waiting for server to exit")
}
}
type fakeStream struct{ id uint32 }
func (*fakeStream) Read(p []byte) (int, error) { return 0, nil }
func (*fakeStream) Write(p []byte) (int, error) { return 0, nil }
func (*fakeStream) Close() error { return nil }
func (*fakeStream) Reset() error { return nil }
func (*fakeStream) Headers() http.Header { return nil }
func (f *fakeStream) Identifier() uint32 { return f.id }
func TestConnectionRemoveStreams(t *testing.T) {
c := &connection{streams: make(map[uint32]httpstream.Stream)}
stream0 := &fakeStream{id: 0}
stream1 := &fakeStream{id: 1}
stream2 := &fakeStream{id: 2}
c.registerStream(stream0)
c.registerStream(stream1)
if len(c.streams) != 2 {
t.Fatalf("should have two streams, has %d", len(c.streams))
}
// not exists
c.RemoveStreams(stream2)
if len(c.streams) != 2 {
t.Fatalf("should have two streams, has %d", len(c.streams))
}
// remove all existing
c.RemoveStreams(stream0, stream1)
// remove nil stream should not crash
c.RemoveStreams(nil)
if len(c.streams) != 0 {
t.Fatalf("should not have any streams, has %d", len(c.streams))
}
}