Skip to content

Commit de8e29b

Browse files
committed
Fix tests taking too long and switch to t.Cleanup
1 parent 17cf0fe commit de8e29b

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

autobahn_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -28,7 +28,6 @@ var excludedAutobahnCases = []string{
28

28

29
// We skip the tests related to requestMaxWindowBits as that is unimplemented due
29
// We skip the tests related to requestMaxWindowBits as that is unimplemented due
30
// to limitations in compress/flate. See https://github.com/golang/go/issues/3155
30
// to limitations in compress/flate. See https://github.com/golang/go/issues/3155
31-
// Same with klauspost/compress which doesn't allow adjusting the sliding window size.
32
"13.3.*", "13.4.*", "13.5.*", "13.6.*",
31
"13.3.*", "13.4.*", "13.5.*", "13.6.*",
33
}
32
}
34

33

@@ -41,6 +40,12 @@ func TestAutobahn(t *testing.T) {
41
t.SkipNow()
40
t.SkipNow()
42
}
41
}
43

42

43+
if os.Getenv("AUTOBAHN_FAST") != "" {
44+
excludedAutobahnCases = append(excludedAutobahnCases,
45+
"9.*", "13.*", "12.*",
46+
)
47+
}
48+
44
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*15)
49
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*15)
45
defer cancel()
50
defer cancel()
46

51

conn_test.go

Lines changed: 13 additions & 34 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -49,7 +49,6 @@ func TestConn(t *testing.T) {
49
CompressionMode: compressionMode(),
49
CompressionMode: compressionMode(),
50
CompressionThreshold: xrand.Int(9999),
50
CompressionThreshold: xrand.Int(9999),
51
})
51
})
52-
defer tt.cleanup()
53

52

54
tt.goEchoLoop(c2)
53
tt.goEchoLoop(c2)
55

54

@@ -67,16 +66,16 @@ func TestConn(t *testing.T) {
67
})
66
})
68

67

69
t.Run("badClose", func(t *testing.T) {
68
t.Run("badClose", func(t *testing.T) {
70-
tt, c1, _ := newConnTest(t, nil, nil)
69+
tt, c1, c2 := newConnTest(t, nil, nil)
71-
defer tt.cleanup()
70+
71+
c2.CloseRead(tt.ctx)
72

72

73
err := c1.Close(-1, "")
73
err := c1.Close(-1, "")
74
assert.Contains(t, err, "failed to marshal close frame: status code StatusCode(-1) cannot be set")
74
assert.Contains(t, err, "failed to marshal close frame: status code StatusCode(-1) cannot be set")
75
})
75
})
76

76

77
t.Run("ping", func(t *testing.T) {
77
t.Run("ping", func(t *testing.T) {
78
tt, c1, c2 := newConnTest(t, nil, nil)
78
tt, c1, c2 := newConnTest(t, nil, nil)
79-
defer tt.cleanup()
80

79

81
c1.CloseRead(tt.ctx)
80
c1.CloseRead(tt.ctx)
82
c2.CloseRead(tt.ctx)
81
c2.CloseRead(tt.ctx)
@@ -92,7 +91,6 @@ func TestConn(t *testing.T) {
92

91

93
t.Run("badPing", func(t *testing.T) {
92
t.Run("badPing", func(t *testing.T) {
94
tt, c1, c2 := newConnTest(t, nil, nil)
93
tt, c1, c2 := newConnTest(t, nil, nil)
95-
defer tt.cleanup()
96

94

97
c2.CloseRead(tt.ctx)
95
c2.CloseRead(tt.ctx)
98

96

@@ -105,7 +103,6 @@ func TestConn(t *testing.T) {
105

103

106
t.Run("concurrentWrite", func(t *testing.T) {
104
t.Run("concurrentWrite", func(t *testing.T) {
107
tt, c1, c2 := newConnTest(t, nil, nil)
105
tt, c1, c2 := newConnTest(t, nil, nil)
108-
defer tt.cleanup()
109

106

110
tt.goDiscardLoop(c2)
107
tt.goDiscardLoop(c2)
111

108

@@ -138,7 +135,6 @@ func TestConn(t *testing.T) {
138

135

139
t.Run("concurrentWriteError", func(t *testing.T) {
136
t.Run("concurrentWriteError", func(t *testing.T) {
140
tt, c1, _ := newConnTest(t, nil, nil)
137
tt, c1, _ := newConnTest(t, nil, nil)
141-
defer tt.cleanup()
142

138

143
_, err := c1.Writer(tt.ctx, websocket.MessageText)
139
_, err := c1.Writer(tt.ctx, websocket.MessageText)
144
assert.Success(t, err)
140
assert.Success(t, err)
@@ -152,7 +148,6 @@ func TestConn(t *testing.T) {
152

148

153
t.Run("netConn", func(t *testing.T) {
149
t.Run("netConn", func(t *testing.T) {
154
tt, c1, c2 := newConnTest(t, nil, nil)
150
tt, c1, c2 := newConnTest(t, nil, nil)
155-
defer tt.cleanup()
156

151

157
n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary)
152
n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary)
158
n2 := websocket.NetConn(tt.ctx, c2, websocket.MessageBinary)
153
n2 := websocket.NetConn(tt.ctx, c2, websocket.MessageBinary)
@@ -192,17 +187,14 @@ func TestConn(t *testing.T) {
192

187

193
t.Run("netConn/BadMsg", func(t *testing.T) {
188
t.Run("netConn/BadMsg", func(t *testing.T) {
194
tt, c1, c2 := newConnTest(t, nil, nil)
189
tt, c1, c2 := newConnTest(t, nil, nil)
195-
defer tt.cleanup()
196

190

197
n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary)
191
n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary)
198
n2 := websocket.NetConn(tt.ctx, c2, websocket.MessageText)
192
n2 := websocket.NetConn(tt.ctx, c2, websocket.MessageText)
199

193

194+
c2.CloseRead(tt.ctx)
200
errs := xsync.Go(func() error {
195
errs := xsync.Go(func() error {
201
_, err := n2.Write([]byte("hello"))
196
_, err := n2.Write([]byte("hello"))
202-
if err != nil {
197+
return err
203-
return err
204-
}
205-
return nil
206
})
198
})
207

199

208
_, err := ioutil.ReadAll(n1)
200
_, err := ioutil.ReadAll(n1)
@@ -218,7 +210,6 @@ func TestConn(t *testing.T) {
218

210

219
t.Run("wsjson", func(t *testing.T) {
211
t.Run("wsjson", func(t *testing.T) {
220
tt, c1, c2 := newConnTest(t, nil, nil)
212
tt, c1, c2 := newConnTest(t, nil, nil)
221-
defer tt.cleanup()
222

213

223
tt.goEchoLoop(c2)
214
tt.goEchoLoop(c2)
224

215

@@ -248,7 +239,6 @@ func TestConn(t *testing.T) {
248

239

249
t.Run("wspb", func(t *testing.T) {
240
t.Run("wspb", func(t *testing.T) {
250
tt, c1, c2 := newConnTest(t, nil, nil)
241
tt, c1, c2 := newConnTest(t, nil, nil)
251-
defer tt.cleanup()
252

242

253
tt.goEchoLoop(c2)
243
tt.goEchoLoop(c2)
254

244

@@ -305,8 +295,6 @@ func assertCloseStatus(exp websocket.StatusCode, err error) error {
305
type connTest struct {
295
type connTest struct {
306
t testing.TB
296
t testing.TB
307
ctx context.Context
297
ctx context.Context
308-
309-
doneFuncs []func()
310
}
298
}
311

299

312
func newConnTest(t testing.TB, dialOpts *websocket.DialOptions, acceptOpts *websocket.AcceptOptions) (tt *connTest, c1, c2 *websocket.Conn) {
300
func newConnTest(t testing.TB, dialOpts *websocket.DialOptions, acceptOpts *websocket.AcceptOptions) (tt *connTest, c1, c2 *websocket.Conn) {
@@ -317,38 +305,30 @@ func newConnTest(t testing.TB, dialOpts *websocket.DialOptions, acceptOpts *webs
317

305

318
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
306
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
319
tt = &connTest{t: t, ctx: ctx}
307
tt = &connTest{t: t, ctx: ctx}
320-
tt.appendDone(cancel)
308+
t.Cleanup(cancel)
321

309

322
c1, c2 = wstest.Pipe(dialOpts, acceptOpts)
310
c1, c2 = wstest.Pipe(dialOpts, acceptOpts)
323
if xrand.Bool() {
311
if xrand.Bool() {
324
c1, c2 = c2, c1
312
c1, c2 = c2, c1
325
}
313
}
326-
tt.appendDone(func() {
314+
t.Cleanup(func() {
327-
c2.Close(websocket.StatusInternalError, "")
315+
// We don't actually care whether this succeeds so we just run it in a separate goroutine to avoid
328-
c1.Close(websocket.StatusInternalError, "")
316+
// blocking the test shutting down.
317+
go c2.Close(websocket.StatusInternalError, "")
318+
go c1.Close(websocket.StatusInternalError, "")
329
})
319
})
330

320

331
return tt, c1, c2
321
return tt, c1, c2
332
}
322
}
333

323

334-
func (tt *connTest) appendDone(f func()) {
335-
tt.doneFuncs = append(tt.doneFuncs, f)
336-
}
337-
338-
func (tt *connTest) cleanup() {
339-
for i := len(tt.doneFuncs) - 1; i >= 0; i-- {
340-
tt.doneFuncs[i]()
341-
}
342-
}
343-
344
func (tt *connTest) goEchoLoop(c *websocket.Conn) {
324
func (tt *connTest) goEchoLoop(c *websocket.Conn) {
345
ctx, cancel := context.WithCancel(tt.ctx)
325
ctx, cancel := context.WithCancel(tt.ctx)
346

326

347
echoLoopErr := xsync.Go(func() error {
327
echoLoopErr := xsync.Go(func() error {
348
err := wstest.EchoLoop(ctx, c)
328
err := wstest.EchoLoop(ctx, c)
349
return assertCloseStatus(websocket.StatusNormalClosure, err)
329
return assertCloseStatus(websocket.StatusNormalClosure, err)
350
})
330
})
351-
tt.appendDone(func() {
331+
tt.t.Cleanup(func() {
352
cancel()
332
cancel()
353
err := <-echoLoopErr
333
err := <-echoLoopErr
354
if err != nil {
334
if err != nil {
@@ -370,7 +350,7 @@ func (tt *connTest) goDiscardLoop(c *websocket.Conn) {
370
}
350
}
371
}
351
}
372
})
352
})
373-
tt.appendDone(func() {
353+
tt.t.Cleanup(func() {
374
cancel()
354
cancel()
375
err := <-discardLoopErr
355
err := <-discardLoopErr
376
if err != nil {
356
if err != nil {
@@ -404,7 +384,6 @@ func BenchmarkConn(b *testing.B) {
404
}, &websocket.AcceptOptions{
384
}, &websocket.AcceptOptions{
405
CompressionMode: bc.mode,
385
CompressionMode: bc.mode,
406
})
386
})
407-
defer bb.cleanup()
408

387

409
bb.goEchoLoop(c2)
388
bb.goEchoLoop(c2)
410

389

0 commit comments

Comments
 (0)