@@ -6,7 +6,6 @@ package context
6
6
7
7
import (
8
8
"fmt"
9
- "internal/testenv"
10
9
"math/rand"
11
10
"runtime"
12
11
"strings"
@@ -230,64 +229,55 @@ func TestChildFinishesFirst(t *testing.T) {
230
229
}
231
230
}
232
231
233
- func testDeadline (c Context , wait time.Duration , t * testing.T ) {
232
+ func testDeadline (c Context , name string , failAfter time.Duration , t * testing.T ) {
234
233
select {
235
- case <- time .After (wait ):
236
- t .Fatalf ("context should have timed out" )
234
+ case <- time .After (failAfter ):
235
+ t .Fatalf ("%s: context should have timed out" , name )
237
236
case <- c .Done ():
238
237
}
239
238
if e := c .Err (); e != DeadlineExceeded {
240
- t .Errorf ("c.Err() == %v want %v" , e , DeadlineExceeded )
239
+ t .Errorf ("%s: c.Err() == %v; want %v" , name , e , DeadlineExceeded )
241
240
}
242
241
}
243
242
244
243
func TestDeadline (t * testing.T ) {
245
- if runtime .GOOS == "openbsd" {
246
- testenv .SkipFlaky (t , 15158 )
247
- }
248
- c , _ := WithDeadline (Background (), time .Now ().Add (100 * time .Millisecond ))
244
+ c , _ := WithDeadline (Background (), time .Now ().Add (50 * time .Millisecond ))
249
245
if got , prefix := fmt .Sprint (c ), "context.Background.WithDeadline(" ; ! strings .HasPrefix (got , prefix ) {
250
246
t .Errorf ("c.String() = %q want prefix %q" , got , prefix )
251
247
}
252
- testDeadline (c , 200 * time .Millisecond , t )
248
+ testDeadline (c , "WithDeadline" , time .Second , t )
253
249
254
- c , _ = WithDeadline (Background (), time .Now ().Add (100 * time .Millisecond ))
250
+ c , _ = WithDeadline (Background (), time .Now ().Add (50 * time .Millisecond ))
255
251
o := otherContext {c }
256
- testDeadline (o , 200 * time .Millisecond , t )
252
+ testDeadline (o , "WithDeadline+otherContext" , time .Second , t )
257
253
258
- c , _ = WithDeadline (Background (), time .Now ().Add (100 * time .Millisecond ))
254
+ c , _ = WithDeadline (Background (), time .Now ().Add (50 * time .Millisecond ))
259
255
o = otherContext {c }
260
- c , _ = WithDeadline (o , time .Now ().Add (300 * time .Millisecond ))
261
- testDeadline (c , 200 * time .Millisecond , t )
256
+ c , _ = WithDeadline (o , time .Now ().Add (4 * time .Second ))
257
+ testDeadline (c , "WithDeadline+otherContext+WithDeadline" , 2 * time .Second , t )
262
258
}
263
259
264
260
func TestTimeout (t * testing.T ) {
265
- if runtime .GOOS == "openbsd" {
266
- testenv .SkipFlaky (t , 15158 )
267
- }
268
- c , _ := WithTimeout (Background (), 100 * time .Millisecond )
261
+ c , _ := WithTimeout (Background (), 50 * time .Millisecond )
269
262
if got , prefix := fmt .Sprint (c ), "context.Background.WithDeadline(" ; ! strings .HasPrefix (got , prefix ) {
270
263
t .Errorf ("c.String() = %q want prefix %q" , got , prefix )
271
264
}
272
- testDeadline (c , 200 * time .Millisecond , t )
265
+ testDeadline (c , "WithTimeout" , time .Second , t )
273
266
274
- c , _ = WithTimeout (Background (), 100 * time .Millisecond )
267
+ c , _ = WithTimeout (Background (), 50 * time .Millisecond )
275
268
o := otherContext {c }
276
- testDeadline (o , 200 * time .Millisecond , t )
269
+ testDeadline (o , "WithTimeout+otherContext" , time .Second , t )
277
270
278
- c , _ = WithTimeout (Background (), 100 * time .Millisecond )
271
+ c , _ = WithTimeout (Background (), 50 * time .Millisecond )
279
272
o = otherContext {c }
280
- c , _ = WithTimeout (o , 300 * time .Millisecond )
281
- testDeadline (c , 200 * time .Millisecond , t )
273
+ c , _ = WithTimeout (o , 3 * time .Second )
274
+ testDeadline (c , "WithTimeout+otherContext+WithTimeout" , 2 * time .Second , t )
282
275
}
283
276
284
277
func TestCanceledTimeout (t * testing.T ) {
285
- if runtime .GOOS == "openbsd" {
286
- testenv .SkipFlaky (t , 15158 )
287
- }
288
- c , _ := WithTimeout (Background (), 200 * time .Millisecond )
278
+ c , _ := WithTimeout (Background (), time .Second )
289
279
o := otherContext {c }
290
- c , cancel := WithTimeout (o , 400 * time .Millisecond )
280
+ c , cancel := WithTimeout (o , 2 * time .Second )
291
281
cancel ()
292
282
time .Sleep (100 * time .Millisecond ) // let cancelation propagate
293
283
select {
@@ -398,9 +388,9 @@ func TestAllocs(t *testing.T) {
398
388
gccgoLimit : 8 ,
399
389
},
400
390
{
401
- desc : "WithTimeout(bg, 100 *time.Millisecond)" ,
391
+ desc : "WithTimeout(bg, 5 *time.Millisecond)" ,
402
392
f : func () {
403
- c , cancel := WithTimeout (bg , 100 * time .Millisecond )
393
+ c , cancel := WithTimeout (bg , 5 * time .Millisecond )
404
394
cancel ()
405
395
<- c .Done ()
406
396
},
@@ -414,7 +404,11 @@ func TestAllocs(t *testing.T) {
414
404
// TOOD(iant): Remove this when gccgo does do escape analysis.
415
405
limit = test .gccgoLimit
416
406
}
417
- if n := testing .AllocsPerRun (100 , test .f ); n > limit {
407
+ numRuns := 100
408
+ if testing .Short () {
409
+ numRuns = 10
410
+ }
411
+ if n := testing .AllocsPerRun (numRuns , test .f ); n > limit {
418
412
t .Errorf ("%s allocs = %f want %d" , test .desc , n , int (limit ))
419
413
}
420
414
}
@@ -494,9 +488,6 @@ func TestLayersTimeout(t *testing.T) {
494
488
}
495
489
496
490
func testLayers (t * testing.T , seed int64 , testTimeout bool ) {
497
- if runtime .GOOS == "openbsd" {
498
- testenv .SkipFlaky (t , 15158 )
499
- }
500
491
rand .Seed (seed )
501
492
errorf := func (format string , a ... interface {}) {
502
493
t .Errorf (fmt .Sprintf ("seed=%d: %s" , seed , format ), a ... )
@@ -549,7 +540,7 @@ func testLayers(t *testing.T, seed int64, testTimeout bool) {
549
540
if testTimeout {
550
541
select {
551
542
case <- ctx .Done ():
552
- case <- time .After (timeout + 100 * time .Millisecond ):
543
+ case <- time .After (timeout + time .Second ):
553
544
errorf ("ctx should have timed out" )
554
545
}
555
546
checkValues ("after timeout" )
0 commit comments