Skip to content

Commit 15e1b25

Browse files
committed
http2: speed up TestTransportRetryHasLimit
This test takes ~30s to execute with real timers, making it almost 50% of the total execution time of the http2 tests. Use a fake timer in tests. Change-Id: I750237c7d3b8f7b87881b8a0a8aff2bf4a3cdd9f Reviewed-on: https://go-review.googlesource.com/c/net/+/446375 Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Damien Neil <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 93ec86b commit 15e1b25

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

http2/transport.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,15 @@ func authorityAddr(scheme string, authority string) (addr string) {
502502
return net.JoinHostPort(host, port)
503503
}
504504

505+
var retryBackoffHook func(time.Duration) *time.Timer
506+
507+
func backoffNewTimer(d time.Duration) *time.Timer {
508+
if retryBackoffHook != nil {
509+
return retryBackoffHook(d)
510+
}
511+
return time.NewTimer(d)
512+
}
513+
505514
// RoundTripOpt is like RoundTrip, but takes options.
506515
func (t *Transport) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Response, error) {
507516
if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) {
@@ -527,11 +536,14 @@ func (t *Transport) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Res
527536
}
528537
backoff := float64(uint(1) << (uint(retry) - 1))
529538
backoff += backoff * (0.1 * mathrand.Float64())
539+
d := time.Second * time.Duration(backoff)
540+
timer := backoffNewTimer(d)
530541
select {
531-
case <-time.After(time.Second * time.Duration(backoff)):
542+
case <-timer.C:
532543
t.vlogf("RoundTrip retrying after failure: %v", err)
533544
continue
534545
case <-req.Context().Done():
546+
timer.Stop()
535547
err = req.Context().Err()
536548
}
537549
}

http2/transport_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,6 +3881,12 @@ func TestTransportRetryHasLimit(t *testing.T) {
38813881
if testing.Short() {
38823882
t.Skip("skipping long test in short mode")
38833883
}
3884+
retryBackoffHook = func(d time.Duration) *time.Timer {
3885+
return time.NewTimer(0) // fires immediately
3886+
}
3887+
defer func() {
3888+
retryBackoffHook = nil
3889+
}()
38843890
clientDone := make(chan struct{})
38853891
ct := newClientTester(t)
38863892
ct.client = func() error {

0 commit comments

Comments
 (0)