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

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ var excludedAutobahnCases = []string{
2828

2929
// We skip the tests related to requestMaxWindowBits as that is unimplemented due
3030
// 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.
3231
"13.3.*", "13.4.*", "13.5.*", "13.6.*",
3332
}
3433

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

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

conn_test.go

+13-34
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func TestConn(t *testing.T) {
4949
CompressionMode: compressionMode(),
5050
CompressionThreshold: xrand.Int(9999),
5151
})
52-
defer tt.cleanup()
5352

5453
tt.goEchoLoop(c2)
5554

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

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

7373
err := c1.Close(-1, "")
7474
assert.Contains(t, err, "failed to marshal close frame: status code StatusCode(-1) cannot be set")
7575
})
7676

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

8180
c1.CloseRead(tt.ctx)
8281
c2.CloseRead(tt.ctx)
@@ -92,7 +91,6 @@ func TestConn(t *testing.T) {
9291

9392
t.Run("badPing", func(t *testing.T) {
9493
tt, c1, c2 := newConnTest(t, nil, nil)
95-
defer tt.cleanup()
9694

9795
c2.CloseRead(tt.ctx)
9896

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

106104
t.Run("concurrentWrite", func(t *testing.T) {
107105
tt, c1, c2 := newConnTest(t, nil, nil)
108-
defer tt.cleanup()
109106

110107
tt.goDiscardLoop(c2)
111108

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

139136
t.Run("concurrentWriteError", func(t *testing.T) {
140137
tt, c1, _ := newConnTest(t, nil, nil)
141-
defer tt.cleanup()
142138

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

153149
t.Run("netConn", func(t *testing.T) {
154150
tt, c1, c2 := newConnTest(t, nil, nil)
155-
defer tt.cleanup()
156151

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

193188
t.Run("netConn/BadMsg", func(t *testing.T) {
194189
tt, c1, c2 := newConnTest(t, nil, nil)
195-
defer tt.cleanup()
196190

197191
n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary)
198192
n2 := websocket.NetConn(tt.ctx, c2, websocket.MessageText)
199193

194+
c2.CloseRead(tt.ctx)
200195
errs := xsync.Go(func() error {
201196
_, err := n2.Write([]byte("hello"))
202-
if err != nil {
203-
return err
204-
}
205-
return nil
197+
return err
206198
})
207199

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

219211
t.Run("wsjson", func(t *testing.T) {
220212
tt, c1, c2 := newConnTest(t, nil, nil)
221-
defer tt.cleanup()
222213

223214
tt.goEchoLoop(c2)
224215

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

249240
t.Run("wspb", func(t *testing.T) {
250241
tt, c1, c2 := newConnTest(t, nil, nil)
251-
defer tt.cleanup()
252242

253243
tt.goEchoLoop(c2)
254244

@@ -305,8 +295,6 @@ func assertCloseStatus(exp websocket.StatusCode, err error) error {
305295
type connTest struct {
306296
t testing.TB
307297
ctx context.Context
308-
309-
doneFuncs []func()
310298
}
311299

312300
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
317305

318306
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
319307
tt = &connTest{t: t, ctx: ctx}
320-
tt.appendDone(cancel)
308+
t.Cleanup(cancel)
321309

322310
c1, c2 = wstest.Pipe(dialOpts, acceptOpts)
323311
if xrand.Bool() {
324312
c1, c2 = c2, c1
325313
}
326-
tt.appendDone(func() {
327-
c2.Close(websocket.StatusInternalError, "")
328-
c1.Close(websocket.StatusInternalError, "")
314+
t.Cleanup(func() {
315+
// We don't actually care whether this succeeds so we just run it in a separate goroutine to avoid
316+
// blocking the test shutting down.
317+
go c2.Close(websocket.StatusInternalError, "")
318+
go c1.Close(websocket.StatusInternalError, "")
329319
})
330320

331321
return tt, c1, c2
332322
}
333323

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-
344324
func (tt *connTest) goEchoLoop(c *websocket.Conn) {
345325
ctx, cancel := context.WithCancel(tt.ctx)
346326

347327
echoLoopErr := xsync.Go(func() error {
348328
err := wstest.EchoLoop(ctx, c)
349329
return assertCloseStatus(websocket.StatusNormalClosure, err)
350330
})
351-
tt.appendDone(func() {
331+
tt.t.Cleanup(func() {
352332
cancel()
353333
err := <-echoLoopErr
354334
if err != nil {
@@ -370,7 +350,7 @@ func (tt *connTest) goDiscardLoop(c *websocket.Conn) {
370350
}
371351
}
372352
})
373-
tt.appendDone(func() {
353+
tt.t.Cleanup(func() {
374354
cancel()
375355
err := <-discardLoopErr
376356
if err != nil {
@@ -404,7 +384,6 @@ func BenchmarkConn(b *testing.B) {
404384
}, &websocket.AcceptOptions{
405385
CompressionMode: bc.mode,
406386
})
407-
defer bb.cleanup()
408387

409388
bb.goEchoLoop(c2)
410389

0 commit comments

Comments
 (0)