@@ -95,6 +95,68 @@ func startH2cServer(t *testing.T) net.Listener {
95
95
return l
96
96
}
97
97
98
+ func TestIdleConnTimeout (t * testing.T ) {
99
+ for _ , test := range []struct {
100
+ idleConnTimeout time.Duration
101
+ wait time.Duration
102
+ baseTransport * http.Transport
103
+ wantConns int32
104
+ }{{
105
+ idleConnTimeout : 2 * time .Second ,
106
+ wait : 1 * time .Second ,
107
+ baseTransport : nil ,
108
+ wantConns : 1 ,
109
+ }, {
110
+ idleConnTimeout : 1 * time .Second ,
111
+ wait : 2 * time .Second ,
112
+ baseTransport : nil ,
113
+ wantConns : 5 ,
114
+ }, {
115
+ idleConnTimeout : 0 * time .Second ,
116
+ wait : 1 * time .Second ,
117
+ baseTransport : & http.Transport {
118
+ IdleConnTimeout : 2 * time .Second ,
119
+ },
120
+ wantConns : 1 ,
121
+ }} {
122
+ var gotConns int32
123
+
124
+ st := newServerTester (t , func (w http.ResponseWriter , r * http.Request ) {
125
+ io .WriteString (w , r .RemoteAddr )
126
+ }, optOnlyServer )
127
+ defer st .Close ()
128
+
129
+ tr := & Transport {
130
+ IdleConnTimeout : test .idleConnTimeout ,
131
+ TLSClientConfig : tlsConfigInsecure ,
132
+ }
133
+ defer tr .CloseIdleConnections ()
134
+
135
+ for i := 0 ; i < 5 ; i ++ {
136
+ req , _ := http .NewRequest ("GET" , st .ts .URL , http .NoBody )
137
+ trace := & httptrace.ClientTrace {
138
+ GotConn : func (connInfo httptrace.GotConnInfo ) {
139
+ if ! connInfo .Reused {
140
+ atomic .AddInt32 (& gotConns , 1 )
141
+ }
142
+ },
143
+ }
144
+ req = req .WithContext (httptrace .WithClientTrace (req .Context (), trace ))
145
+
146
+ _ , err := tr .RoundTrip (req )
147
+ if err != nil {
148
+ t .Fatalf ("%v" , err )
149
+ }
150
+
151
+ <- time .After (test .wait )
152
+ }
153
+
154
+ if gotConns != test .wantConns {
155
+ t .Errorf ("incorrect gotConns: %d != %d" , gotConns , test .wantConns )
156
+ }
157
+ }
158
+ }
159
+
98
160
func TestTransportH2c (t * testing.T ) {
99
161
l := startH2cServer (t )
100
162
defer l .Close ()
0 commit comments