@@ -49,7 +49,6 @@ func TestConn(t *testing.T) {
49
49
CompressionMode : compressionMode (),
50
50
CompressionThreshold : xrand .Int (9999 ),
51
51
})
52
- defer tt .cleanup ()
53
52
54
53
tt .goEchoLoop (c2 )
55
54
@@ -67,16 +66,16 @@ func TestConn(t *testing.T) {
67
66
})
68
67
69
68
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 )
72
72
73
73
err := c1 .Close (- 1 , "" )
74
74
assert .Contains (t , err , "failed to marshal close frame: status code StatusCode(-1) cannot be set" )
75
75
})
76
76
77
77
t .Run ("ping" , func (t * testing.T ) {
78
78
tt , c1 , c2 := newConnTest (t , nil , nil )
79
- defer tt .cleanup ()
80
79
81
80
c1 .CloseRead (tt .ctx )
82
81
c2 .CloseRead (tt .ctx )
@@ -92,7 +91,6 @@ func TestConn(t *testing.T) {
92
91
93
92
t .Run ("badPing" , func (t * testing.T ) {
94
93
tt , c1 , c2 := newConnTest (t , nil , nil )
95
- defer tt .cleanup ()
96
94
97
95
c2 .CloseRead (tt .ctx )
98
96
@@ -105,7 +103,6 @@ func TestConn(t *testing.T) {
105
103
106
104
t .Run ("concurrentWrite" , func (t * testing.T ) {
107
105
tt , c1 , c2 := newConnTest (t , nil , nil )
108
- defer tt .cleanup ()
109
106
110
107
tt .goDiscardLoop (c2 )
111
108
@@ -138,7 +135,6 @@ func TestConn(t *testing.T) {
138
135
139
136
t .Run ("concurrentWriteError" , func (t * testing.T ) {
140
137
tt , c1 , _ := newConnTest (t , nil , nil )
141
- defer tt .cleanup ()
142
138
143
139
_ , err := c1 .Writer (tt .ctx , websocket .MessageText )
144
140
assert .Success (t , err )
@@ -152,7 +148,6 @@ func TestConn(t *testing.T) {
152
148
153
149
t .Run ("netConn" , func (t * testing.T ) {
154
150
tt , c1 , c2 := newConnTest (t , nil , nil )
155
- defer tt .cleanup ()
156
151
157
152
n1 := websocket .NetConn (tt .ctx , c1 , websocket .MessageBinary )
158
153
n2 := websocket .NetConn (tt .ctx , c2 , websocket .MessageBinary )
@@ -192,17 +187,14 @@ func TestConn(t *testing.T) {
192
187
193
188
t .Run ("netConn/BadMsg" , func (t * testing.T ) {
194
189
tt , c1 , c2 := newConnTest (t , nil , nil )
195
- defer tt .cleanup ()
196
190
197
191
n1 := websocket .NetConn (tt .ctx , c1 , websocket .MessageBinary )
198
192
n2 := websocket .NetConn (tt .ctx , c2 , websocket .MessageText )
199
193
194
+ c2 .CloseRead (tt .ctx )
200
195
errs := xsync .Go (func () error {
201
196
_ , err := n2 .Write ([]byte ("hello" ))
202
- if err != nil {
203
- return err
204
- }
205
- return nil
197
+ return err
206
198
})
207
199
208
200
_ , err := ioutil .ReadAll (n1 )
@@ -218,7 +210,6 @@ func TestConn(t *testing.T) {
218
210
219
211
t .Run ("wsjson" , func (t * testing.T ) {
220
212
tt , c1 , c2 := newConnTest (t , nil , nil )
221
- defer tt .cleanup ()
222
213
223
214
tt .goEchoLoop (c2 )
224
215
@@ -248,7 +239,6 @@ func TestConn(t *testing.T) {
248
239
249
240
t .Run ("wspb" , func (t * testing.T ) {
250
241
tt , c1 , c2 := newConnTest (t , nil , nil )
251
- defer tt .cleanup ()
252
242
253
243
tt .goEchoLoop (c2 )
254
244
@@ -305,8 +295,6 @@ func assertCloseStatus(exp websocket.StatusCode, err error) error {
305
295
type connTest struct {
306
296
t testing.TB
307
297
ctx context.Context
308
-
309
- doneFuncs []func ()
310
298
}
311
299
312
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
306
ctx , cancel := context .WithTimeout (context .Background (), time .Second * 30 )
319
307
tt = & connTest {t : t , ctx : ctx }
320
- tt . appendDone (cancel )
308
+ t . Cleanup (cancel )
321
309
322
310
c1 , c2 = wstest .Pipe (dialOpts , acceptOpts )
323
311
if xrand .Bool () {
324
312
c1 , c2 = c2 , c1
325
313
}
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 , "" )
329
319
})
330
320
331
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
324
func (tt * connTest ) goEchoLoop (c * websocket.Conn ) {
345
325
ctx , cancel := context .WithCancel (tt .ctx )
346
326
347
327
echoLoopErr := xsync .Go (func () error {
348
328
err := wstest .EchoLoop (ctx , c )
349
329
return assertCloseStatus (websocket .StatusNormalClosure , err )
350
330
})
351
- tt .appendDone (func () {
331
+ tt .t . Cleanup (func () {
352
332
cancel ()
353
333
err := <- echoLoopErr
354
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
354
cancel ()
375
355
err := <- discardLoopErr
376
356
if err != nil {
@@ -404,7 +384,6 @@ func BenchmarkConn(b *testing.B) {
404
384
}, & websocket.AcceptOptions {
405
385
CompressionMode : bc .mode ,
406
386
})
407
- defer bb .cleanup ()
408
387
409
388
bb .goEchoLoop (c2 )
410
389
0 commit comments