@@ -463,74 +463,93 @@ func TestMuxRedirectLeadingSlashes(t *testing.T) {
463
463
func TestServerTimeouts (t * testing.T ) {
464
464
setParallel (t )
465
465
defer afterTest (t )
466
+ // Try three times, with increasing timeouts.
467
+ tries := []time.Duration {250 * time .Millisecond , 500 * time .Millisecond , 1 * time .Second }
468
+ for i , timeout := range tries {
469
+ err := testServerTimeouts (timeout )
470
+ if err == nil {
471
+ return
472
+ }
473
+ t .Logf ("failed at %v: %v" , timeout , err )
474
+ if i != len (tries )- 1 {
475
+ t .Logf ("retrying at %v ..." , tries [i + 1 ])
476
+ }
477
+ }
478
+ t .Fatal ("all attempts failed" )
479
+ }
480
+
481
+ func testServerTimeouts (timeout time.Duration ) error {
466
482
reqNum := 0
467
483
ts := httptest .NewUnstartedServer (HandlerFunc (func (res ResponseWriter , req * Request ) {
468
484
reqNum ++
469
485
fmt .Fprintf (res , "req=%d" , reqNum )
470
486
}))
471
- ts .Config .ReadTimeout = 250 * time . Millisecond
472
- ts .Config .WriteTimeout = 250 * time . Millisecond
487
+ ts .Config .ReadTimeout = timeout
488
+ ts .Config .WriteTimeout = timeout
473
489
ts .Start ()
474
490
defer ts .Close ()
475
491
476
492
// Hit the HTTP server successfully.
477
493
c := ts .Client ()
478
494
r , err := c .Get (ts .URL )
479
495
if err != nil {
480
- t . Fatalf ("http Get #1: %v" , err )
496
+ return fmt . Errorf ("http Get #1: %v" , err )
481
497
}
482
498
got , err := ioutil .ReadAll (r .Body )
483
499
expected := "req=1"
484
500
if string (got ) != expected || err != nil {
485
- t .Errorf ("Unexpected response for request #1; got %q ,%v; expected %q, nil" ,
501
+ return fmt .Errorf ("Unexpected response for request #1; got %q ,%v; expected %q, nil" ,
486
502
string (got ), err , expected )
487
503
}
488
504
489
505
// Slow client that should timeout.
490
506
t1 := time .Now ()
491
507
conn , err := net .Dial ("tcp" , ts .Listener .Addr ().String ())
492
508
if err != nil {
493
- t . Fatalf ("Dial: %v" , err )
509
+ return fmt . Errorf ("Dial: %v" , err )
494
510
}
495
511
buf := make ([]byte , 1 )
496
512
n , err := conn .Read (buf )
497
513
conn .Close ()
498
514
latency := time .Since (t1 )
499
515
if n != 0 || err != io .EOF {
500
- t .Errorf ("Read = %v, %v, wanted %v, %v" , n , err , 0 , io .EOF )
516
+ return fmt .Errorf ("Read = %v, %v, wanted %v, %v" , n , err , 0 , io .EOF )
501
517
}
502
- if latency < 200 * time .Millisecond /* fudge from 250 ms above */ {
503
- t .Errorf ("got EOF after %s, want >= %s" , latency , 200 * time .Millisecond )
518
+ minLatency := timeout / 5 * 4
519
+ if latency < minLatency {
520
+ return fmt .Errorf ("got EOF after %s, want >= %s" , latency , minLatency )
504
521
}
505
522
506
523
// Hit the HTTP server successfully again, verifying that the
507
524
// previous slow connection didn't run our handler. (that we
508
525
// get "req=2", not "req=3")
509
526
r , err = c .Get (ts .URL )
510
527
if err != nil {
511
- t . Fatalf ("http Get #2: %v" , err )
528
+ return fmt . Errorf ("http Get #2: %v" , err )
512
529
}
513
530
got , err = ioutil .ReadAll (r .Body )
531
+ r .Body .Close ()
514
532
expected = "req=2"
515
533
if string (got ) != expected || err != nil {
516
- t .Errorf ("Get #2 got %q, %v, want %q, nil" , string (got ), err , expected )
534
+ return fmt .Errorf ("Get #2 got %q, %v, want %q, nil" , string (got ), err , expected )
517
535
}
518
536
519
537
if ! testing .Short () {
520
538
conn , err := net .Dial ("tcp" , ts .Listener .Addr ().String ())
521
539
if err != nil {
522
- t . Fatalf ( " Dial: %v" , err )
540
+ return fmt . Errorf ( "long Dial: %v" , err )
523
541
}
524
542
defer conn .Close ()
525
543
go io .Copy (ioutil .Discard , conn )
526
544
for i := 0 ; i < 5 ; i ++ {
527
545
_ , err := conn .Write ([]byte ("GET / HTTP/1.1\r \n Host: foo\r \n \r \n " ))
528
546
if err != nil {
529
- t . Fatalf ("on write %d: %v" , i , err )
547
+ return fmt . Errorf ("on write %d: %v" , i , err )
530
548
}
531
- time .Sleep (ts . Config . ReadTimeout / 2 )
549
+ time .Sleep (timeout / 2 )
532
550
}
533
551
}
552
+ return nil
534
553
}
535
554
536
555
// Test that the HTTP/2 server handles Server.WriteTimeout (Issue 18437)
0 commit comments