Skip to content

feat: support to set the request timeout #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
)

type runOption struct {
pattern string
duration time.Duration
thread int64
context context.Context
pattern string
duration time.Duration
requestTimeout time.Duration
requestIgnoreError bool
thread int64
context context.Context
}

// CreateRunCommand returns the run command
Expand All @@ -40,6 +42,8 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
flags.StringVarP(&opt.pattern, "pattern", "p", "test-suite-*.yaml",
"The file pattern which try to execute the test cases")
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
flags.BoolVarP(&opt.requestIgnoreError, "request-ignore-error", "", false, "Indicate if ignore the request error")
flags.Int64VarP(&opt.thread, "thread", "", 1, "Threads of the execution")
return
}
Expand Down Expand Up @@ -69,7 +73,7 @@ func (o *runOption) runSuiteWithDuration(suite string) (err error) {
// make sure having a valid timer
timeout = time.NewTicker(time.Second)
}
errChannel := make(chan error, 10)
errChannel := make(chan error, 10*o.thread)
var wait sync.WaitGroup

for !stop {
Expand All @@ -89,28 +93,32 @@ func (o *runOption) runSuiteWithDuration(suite string) (err error) {
stop = true
}

go func(ch chan error) {
go func(ch chan error, sem *semaphore.Weighted) {
now := time.Now()
defer sem.Release(1)
defer wait.Done()
defer func() {
fmt.Println("routing end with", time.Now().Sub(now))
}()

ctx := getDefaultContext()
ch <- runSuite(suite, ctx)
}(errChannel)
dataContext := getDefaultContext()
ch <- o.runSuite(suite, dataContext, o.context)
}(errChannel, sem)
}
}
err = <-errChannel
wait.Wait()
return
}

func runSuite(suite string, ctx map[string]interface{}) (err error) {
func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, ctx context.Context) (err error) {
var testSuite *testing.TestSuite
if testSuite, err = testing.Parse(suite); err != nil {
return
}

var result string
if result, err = render.Render("base api", testSuite.API, ctx); err == nil {
if result, err = render.Render("base api", testSuite.API, dataContext); err == nil {
testSuite.API = result
testSuite.API = strings.TrimSuffix(testSuite.API, "/")
} else {
Expand All @@ -125,10 +133,11 @@ func runSuite(suite string, ctx map[string]interface{}) (err error) {

setRelativeDir(suite, &testCase)
var output interface{}
if output, err = runner.RunTestCase(&testCase, ctx); err != nil {
ctxWithTimeout, _ := context.WithTimeout(ctx, o.requestTimeout)
if output, err = runner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError {
return
}
ctx[testCase.Name] = output
dataContext[testCase.Name] = output
}
return
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"context"
"net/http"
"testing"
"time"

"github.com/h2non/gock"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -46,7 +48,9 @@ func TestRunSuite(t *testing.T) {

tt.prepare()
ctx := getDefaultContext()
err := runSuite(tt.suiteFile, ctx)
opt := &runOption{requestTimeout: 30 * time.Second}

err := opt.runSuite(tt.suiteFile, ctx, context.TODO())
assert.Equal(t, tt.hasError, err != nil, err)
})
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/runner/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -11,7 +12,6 @@ import (
"os"
"reflect"
"strings"
"time"

"github.com/andreyvit/diff"
"github.com/antonmedv/expr"
Expand All @@ -22,7 +22,7 @@ import (
)

// RunTestCase runs the test case
func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{}, err error) {
func RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx context.Context) (output interface{}, err error) {
fmt.Printf("start to run: '%s'\n", testcase.Name)
if err = doPrepare(testcase); err != nil {
err = fmt.Errorf("failed to prepare, error: %v", err)
Expand All @@ -37,9 +37,7 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
}
}()

client := http.Client{
Timeout: time.Second * 30,
}
client := http.Client{}
var requestBody io.Reader
if testcase.Request.Body != "" {
requestBody = bytes.NewBufferString(testcase.Request.Body)
Expand All @@ -51,7 +49,7 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
requestBody = bytes.NewBufferString(string(data))
}

if err = testcase.Request.Render(ctx); err != nil {
if err = testcase.Request.Render(dataContext); err != nil {
return
}

Expand All @@ -76,7 +74,7 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
}

var request *http.Request
if request, err = http.NewRequest(testcase.Request.Method, testcase.Request.API, requestBody); err != nil {
if request, err = http.NewRequestWithContext(ctx, testcase.Request.Method, testcase.Request.API, requestBody); err != nil {
return
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/runner/simple_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"context"
"errors"
"net/http"
"testing"
Expand Down Expand Up @@ -365,7 +366,7 @@ func TestTestCase(t *testing.T) {
if tt.prepare != nil {
tt.prepare()
}
output, err := RunTestCase(tt.testCase, tt.ctx)
output, err := RunTestCase(tt.testCase, tt.ctx, context.TODO())
tt.verify(t, output, err)
})
}
Expand Down