@@ -17,10 +17,12 @@ import (
1717)
1818
1919type 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}
0 commit comments