Skip to content

Commit

Permalink
http2: add test for dail error should respect to timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Taction committed Jun 4, 2024
1 parent 022530c commit f91b074
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions http2/clientconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ package http2
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"os"
"reflect"
"runtime"
"slices"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -81,6 +84,56 @@ func TestTestClientConn(t *testing.T) {
rt.wantBody(nil)
}

// TestConnectTimeout tests that a request does not exceed request timeout + dial timeout
func TestConnectTimeout(t *testing.T) {
tr := &Transport{
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
// mock a net dialler with 1s timeout, encountering network issue
// keeping dialing until timeout
var dialer = net.Dialer{Timeout: time.Duration(-1)}
select {
case <-time.After(time.Second):
case <-ctx.Done():
}
return dialer.DialContext(ctx, network, addr)
},
AllowHTTP: true,
}

var sg sync.WaitGroup
parentCtx, cancel := context.WithCancel(context.Background())
defer cancel()

for j := 0; j < 2; j++ {
sg.Add(1)
go func() {
for i := 0; i < 10000; i++ {
sg.Add(1)
go func() {
ctx, _ := context.WithTimeout(parentCtx, time.Second)
req, err := http.NewRequestWithContext(ctx, "GET", "http://127.0.0.1:80", nil)
if err != nil {
t.Errorf("NewRequest: %v", err)
}

start := time.Now()
tr.RoundTrip(req)
duration := time.Since(start)
// duration should not exceed request timeout + dial timeout
if duration > 2*time.Second {
t.Errorf("RoundTrip took %s; want <2s", duration.String())
}
sg.Done()
}()
time.Sleep(1 * time.Millisecond)
}
sg.Done()
}()
}

sg.Wait()
}

// A testClientConn allows testing ClientConn.RoundTrip against a fake server.
//
// A test using testClientConn consists of:
Expand Down

0 comments on commit f91b074

Please sign in to comment.