@@ -17,10 +17,12 @@ import (
17
17
)
18
18
19
19
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
24
26
}
25
27
26
28
// CreateRunCommand returns the run command
@@ -40,6 +42,8 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
40
42
flags .StringVarP (& opt .pattern , "pattern" , "p" , "test-suite-*.yaml" ,
41
43
"The file pattern which try to execute the test cases" )
42
44
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" )
43
47
flags .Int64VarP (& opt .thread , "thread" , "" , 1 , "Threads of the execution" )
44
48
return
45
49
}
@@ -69,7 +73,7 @@ func (o *runOption) runSuiteWithDuration(suite string) (err error) {
69
73
// make sure having a valid timer
70
74
timeout = time .NewTicker (time .Second )
71
75
}
72
- errChannel := make (chan error , 10 )
76
+ errChannel := make (chan error , 10 * o . thread )
73
77
var wait sync.WaitGroup
74
78
75
79
for ! stop {
@@ -89,28 +93,32 @@ func (o *runOption) runSuiteWithDuration(suite string) (err error) {
89
93
stop = true
90
94
}
91
95
92
- go func (ch chan error ) {
96
+ go func (ch chan error , sem * semaphore.Weighted ) {
97
+ now := time .Now ()
93
98
defer sem .Release (1 )
94
99
defer wait .Done ()
100
+ defer func () {
101
+ fmt .Println ("routing end with" , time .Now ().Sub (now ))
102
+ }()
95
103
96
- ctx := getDefaultContext ()
97
- ch <- runSuite (suite , ctx )
98
- }(errChannel )
104
+ dataContext := getDefaultContext ()
105
+ ch <- o . runSuite (suite , dataContext , o . context )
106
+ }(errChannel , sem )
99
107
}
100
108
}
101
109
err = <- errChannel
102
110
wait .Wait ()
103
111
return
104
112
}
105
113
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 ) {
107
115
var testSuite * testing.TestSuite
108
116
if testSuite , err = testing .Parse (suite ); err != nil {
109
117
return
110
118
}
111
119
112
120
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 {
114
122
testSuite .API = result
115
123
testSuite .API = strings .TrimSuffix (testSuite .API , "/" )
116
124
} else {
@@ -125,10 +133,11 @@ func runSuite(suite string, ctx map[string]interface{}) (err error) {
125
133
126
134
setRelativeDir (suite , & testCase )
127
135
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 {
129
138
return
130
139
}
131
- ctx [testCase .Name ] = output
140
+ dataContext [testCase .Name ] = output
132
141
}
133
142
return
134
143
}
0 commit comments