Skip to content

Commit 2c49281

Browse files
committed
feat: support to set the request timeout
1 parent 8564fc1 commit 2c49281

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

cmd/run.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import (
1717
)
1818

1919
type runOption struct {
20-
pattern string
21-
duration time.Duration
22-
thread int64
23-
context context.Context
20+
pattern string
21+
duration time.Duration
22+
requestTimeout time.Duration
23+
requestIgnoreError bool
24+
thread int64
25+
context context.Context
2426
}
2527

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

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

92-
go func(ch chan error) {
96+
go func(ch chan error, sem *semaphore.Weighted) {
97+
now := time.Now()
9398
defer sem.Release(1)
9499
defer wait.Done()
100+
defer func() {
101+
fmt.Println("routing end with", time.Now().Sub(now))
102+
}()
95103

96-
ctx := getDefaultContext()
97-
ch <- runSuite(suite, ctx)
98-
}(errChannel)
104+
dataContext := getDefaultContext()
105+
ch <- o.runSuite(suite, dataContext, o.context)
106+
}(errChannel, sem)
99107
}
100108
}
101109
err = <-errChannel
102110
wait.Wait()
103111
return
104112
}
105113

106-
func runSuite(suite string, ctx map[string]interface{}) (err error) {
114+
func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, ctx context.Context) (err error) {
107115
var testSuite *testing.TestSuite
108116
if testSuite, err = testing.Parse(suite); err != nil {
109117
return
110118
}
111119

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

126134
setRelativeDir(suite, &testCase)
127135
var output interface{}
128-
if output, err = runner.RunTestCase(&testCase, ctx); err != nil {
136+
ctxWithTimeout, _ := context.WithTimeout(ctx, o.requestTimeout)
137+
if output, err = runner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError {
129138
return
130139
}
131-
ctx[testCase.Name] = output
140+
dataContext[testCase.Name] = output
132141
}
133142
return
134143
}

cmd/run_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package cmd
22

33
import (
4+
"context"
45
"net/http"
56
"testing"
7+
"time"
68

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

4749
tt.prepare()
4850
ctx := getDefaultContext()
49-
err := runSuite(tt.suiteFile, ctx)
51+
opt := &runOption{requestTimeout: 30 * time.Second}
52+
53+
err := opt.runSuite(tt.suiteFile, ctx, context.TODO())
5054
assert.Equal(t, tt.hasError, err != nil, err)
5155
})
5256
}

pkg/runner/simple.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package runner
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -11,7 +12,6 @@ import (
1112
"os"
1213
"reflect"
1314
"strings"
14-
"time"
1515

1616
"github.com/andreyvit/diff"
1717
"github.com/antonmedv/expr"
@@ -22,7 +22,7 @@ import (
2222
)
2323

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

40-
client := http.Client{
41-
Timeout: time.Second * 30,
42-
}
40+
client := http.Client{}
4341
var requestBody io.Reader
4442
if testcase.Request.Body != "" {
4543
requestBody = bytes.NewBufferString(testcase.Request.Body)
@@ -51,7 +49,7 @@ func RunTestCase(testcase *testing.TestCase, ctx interface{}) (output interface{
5149
requestBody = bytes.NewBufferString(string(data))
5250
}
5351

54-
if err = testcase.Request.Render(ctx); err != nil {
52+
if err = testcase.Request.Render(dataContext); err != nil {
5553
return
5654
}
5755

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

7876
var request *http.Request
79-
if request, err = http.NewRequest(testcase.Request.Method, testcase.Request.API, requestBody); err != nil {
77+
if request, err = http.NewRequestWithContext(ctx, testcase.Request.Method, testcase.Request.API, requestBody); err != nil {
8078
return
8179
}
8280

pkg/runner/simple_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package runner
22

33
import (
4+
"context"
45
"errors"
56
"net/http"
67
"testing"
@@ -365,7 +366,7 @@ func TestTestCase(t *testing.T) {
365366
if tt.prepare != nil {
366367
tt.prepare()
367368
}
368-
output, err := RunTestCase(tt.testCase, tt.ctx)
369+
output, err := RunTestCase(tt.testCase, tt.ctx, context.TODO())
369370
tt.verify(t, output, err)
370371
})
371372
}

0 commit comments

Comments
 (0)