Skip to content

Commit

Permalink
k6runner: use check context for http request (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
roobre authored Jun 11, 2024
1 parent d1ca970 commit 18992df
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
21 changes: 18 additions & 3 deletions internal/k6runner/k6runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,32 @@ func (r HttpRunner) WithLogger(logger *zerolog.Logger) Runner {
var ErrUnexpectedStatus = errors.New("unexpected status code")

func (r HttpRunner) Run(ctx context.Context, script []byte) (*RunResponse, error) {
req, err := json.Marshal(&RunRequest{
k6Timeout := getTimeout(ctx)

reqBody, err := json.Marshal(&RunRequest{
Script: script,
Settings: Settings{
Timeout: getTimeout(ctx).Milliseconds(),
Timeout: k6Timeout.Milliseconds(),
},
})
if err != nil {
return nil, fmt.Errorf("running script: %w", err)
}

resp, err := http.Post(r.url, "application/json", bytes.NewBuffer(req))
// The context above carries the check timeout, which will be eventually passed to k6 by the runner at the other end
// of this request. To account for network overhead, we create a different context with an extra second of timeout,
// which adds some grace time to account for the network/system latency of the http request.
reqCtx, cancel := context.WithTimeout(context.Background(), k6Timeout+time.Second)
defer cancel()

req, err := http.NewRequestWithContext(reqCtx, http.MethodPost, r.url, bytes.NewReader(reqBody))
if err != nil {
return nil, fmt.Errorf("building request: %w", err)
}

req.Header.Add("content-type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
r.logger.Error().Err(err).Msg("sending request")
return nil, fmt.Errorf("running script: %w", err)
Expand Down
17 changes: 16 additions & 1 deletion internal/k6runner/k6runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func TestScriptHTTPRun(t *testing.T) {
for _, tc := range []struct {
name string
response *RunResponse
delay time.Duration
statusCode int
expectSuccess bool
expectError error
Expand Down Expand Up @@ -249,6 +250,17 @@ func TestScriptHTTPRun(t *testing.T) {
expectSuccess: false,
expectError: ErrBuggyRunner,
},
{
name: "request timeout",
response: &RunResponse{
Metrics: testMetrics,
Logs: testLogs,
},
delay: 2 * time.Second,
statusCode: http.StatusInternalServerError,
expectSuccess: false,
expectError: context.DeadlineExceeded,
},
} {
tc := tc

Expand All @@ -257,6 +269,7 @@ func TestScriptHTTPRun(t *testing.T) {

mux := http.NewServeMux()
mux.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(tc.delay)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tc.statusCode)
_ = json.NewEncoder(w).Encode(tc.response)
Expand All @@ -268,7 +281,9 @@ func TestScriptHTTPRun(t *testing.T) {
script, err := NewScript([]byte("tee-hee"), runner)
require.NoError(t, err)

ctx, cancel := testhelper.Context(context.Background(), t)
baseCtx, baseCancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(baseCancel)
ctx, cancel := testhelper.Context(baseCtx, t)
t.Cleanup(cancel)

var (
Expand Down

0 comments on commit 18992df

Please sign in to comment.