@@ -10,13 +10,16 @@ package http2
10
10
import (
11
11
"bytes"
12
12
"context"
13
+ "crypto/tls"
13
14
"fmt"
14
15
"io"
16
+ "net"
15
17
"net/http"
16
18
"os"
17
19
"reflect"
18
20
"runtime"
19
21
"slices"
22
+ "sync"
20
23
"sync/atomic"
21
24
"testing"
22
25
"time"
@@ -81,6 +84,56 @@ func TestTestClientConn(t *testing.T) {
81
84
rt .wantBody (nil )
82
85
}
83
86
87
+ // TestConnectTimeout tests that a request does not exceed request timeout + dial timeout
88
+ func TestConnectTimeout (t * testing.T ) {
89
+ tr := & Transport {
90
+ DialTLSContext : func (ctx context.Context , network , addr string , cfg * tls.Config ) (net.Conn , error ) {
91
+ // mock a net dialler with 1s timeout, encountering network issue
92
+ // keeping dialing until timeout
93
+ var dialer = net.Dialer {Timeout : time .Duration (- 1 )}
94
+ select {
95
+ case <- time .After (time .Second ):
96
+ case <- ctx .Done ():
97
+ }
98
+ return dialer .DialContext (ctx , network , addr )
99
+ },
100
+ AllowHTTP : true ,
101
+ }
102
+
103
+ var sg sync.WaitGroup
104
+ parentCtx , cancel := context .WithCancel (context .Background ())
105
+ defer cancel ()
106
+
107
+ for j := 0 ; j < 2 ; j ++ {
108
+ sg .Add (1 )
109
+ go func () {
110
+ for i := 0 ; i < 10000 ; i ++ {
111
+ sg .Add (1 )
112
+ go func () {
113
+ ctx , _ := context .WithTimeout (parentCtx , time .Second )
114
+ req , err := http .NewRequestWithContext (ctx , "GET" , "http://127.0.0.1:80" , nil )
115
+ if err != nil {
116
+ t .Errorf ("NewRequest: %v" , err )
117
+ }
118
+
119
+ start := time .Now ()
120
+ tr .RoundTrip (req )
121
+ duration := time .Since (start )
122
+ // duration should not exceed request timeout + dial timeout
123
+ if duration > 2 * time .Second {
124
+ t .Errorf ("RoundTrip took %s; want <2s" , duration .String ())
125
+ }
126
+ sg .Done ()
127
+ }()
128
+ time .Sleep (1 * time .Millisecond )
129
+ }
130
+ sg .Done ()
131
+ }()
132
+ }
133
+
134
+ sg .Wait ()
135
+ }
136
+
84
137
// A testClientConn allows testing ClientConn.RoundTrip against a fake server.
85
138
//
86
139
// A test using testClientConn consists of:
0 commit comments