Skip to content

Commit df5f3b3

Browse files
committed
lib/netext/httpext: add MakeRequest timeout test
1 parent e6f11ac commit df5f3b3

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lib/netext/httpext/request_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"net/http/httptest"
1010
"net/url"
1111
"testing"
12+
"time"
1213

1314
"github.com/loadimpact/k6/lib"
1415
"github.com/loadimpact/k6/stats"
1516
"github.com/pkg/errors"
17+
"github.com/sirupsen/logrus"
1618
"github.com/stretchr/testify/assert"
1719
"github.com/stretchr/testify/require"
1820
)
@@ -139,6 +141,37 @@ func TestURL(t *testing.T) {
139141
})
140142
}
141143

144+
func TestMakeRequestTimeout(t *testing.T) {
145+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
146+
time.Sleep(100 * time.Millisecond)
147+
}))
148+
defer srv.Close()
149+
ctx, cancel := context.WithCancel(context.Background())
150+
defer cancel()
151+
samples := make(chan stats.SampleContainer, 10)
152+
logger := logrus.New()
153+
logger.Level = logrus.DebugLevel
154+
state := &lib.State{
155+
Options: lib.Options{RunTags: &stats.SampleTags{}},
156+
Transport: srv.Client().Transport,
157+
Samples: samples,
158+
Logger: logger,
159+
}
160+
ctx = lib.WithState(ctx, state)
161+
req, _ := http.NewRequest("GET", srv.URL, nil)
162+
var preq = &ParsedHTTPRequest{
163+
Req: req,
164+
URL: &URL{u: req.URL},
165+
Body: new(bytes.Buffer),
166+
Timeout: 10 * time.Millisecond,
167+
}
168+
169+
res, err := MakeRequest(ctx, preq)
170+
require.NoError(t, err)
171+
assert.NotNil(t, res)
172+
assert.Len(t, samples, 1)
173+
}
174+
142175
func BenchmarkWrapDecompressionError(b *testing.B) {
143176
err := errors.New("error")
144177
b.ResetTimer()

0 commit comments

Comments
 (0)