Skip to content

Commit 8267c5f

Browse files
authored
Merge pull request #9 from vearne/develop
Develop
2 parents c41aac8 + 754c5a4 commit 8267c5f

File tree

6 files changed

+42
-7
lines changed

6 files changed

+42
-7
lines changed

config_files/autotest.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# my global config
22
global:
33
# worker_num: 5
4-
worker_num: 1
4+
worker_num: 5
55
# default: true
66
ignore_testcase_fail: true
77
debug: false
8+
# Timeout setting for each request
9+
request_timeout: 5s
810

911
logger:
1012
# 1. "info"

config_files/my_http_api.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- id: 1
2+
desc: "list all books"
23
request:
3-
# optional
44
method: "get"
55
url: "http://{{ HOST }}/api/books"
66
rules:
@@ -11,6 +11,7 @@
1111
Expected: "Effective Go"
1212

1313
- id: 2
14+
desc: "add a new book"
1415
request:
1516
# optional
1617
method: "post"
@@ -36,6 +37,7 @@
3637
type: integer
3738

3839
- id: 3
40+
desc: "modify the book3"
3941
# Depends on TestCase2
4042
# TestCase2 must be executed first
4143
dependOnIDs: [2]
@@ -60,6 +62,7 @@
6062
Expected: "book3_author-2"
6163

6264
- id: 4
65+
desc: "delete the book1"
6366
request:
6467
# optional
6568
method: "delete"
@@ -71,6 +74,9 @@
7174
expectedStatus: 204
7275

7376
- id: 5
77+
desc: "try to get book1"
78+
# Delay for 5 seconds before executing
79+
delay: 5s
7480
dependOnIDs: [4]
7581
request:
7682
# optional

internal/command/http_automate.go

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func HttpAutomateTest(httpTestCases map[string][]*config.TestCase) {
3333
successCount := 0
3434
failedCount := 0
3535
for filePath := range httpTestCases {
36+
// if ignore_testcase_fail is false and some testcases have failed.
37+
if resource.TerminationFlag.Load() {
38+
break
39+
}
40+
3641
info := HandleSingleFile(workerNum, filePath)
3742
finishCount += info.Total
3843
successCount += info.SuccessCount
@@ -102,6 +107,11 @@ func HandleSingleFile(workerNum int, filePath string) *ResultInfo {
102107
successCount++
103108
} else {
104109
failedCount++
110+
// terminate subsequent testcases
111+
if !resource.GlobalConfig.Global.IgnoreTestCaseFail {
112+
resource.TerminationFlag.Store(true)
113+
break
114+
}
105115
}
106116

107117
stateGroup.SetState(tcResult.ID, tcResult.State)

internal/command/http_call.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/vearne/zaplog"
1111
"go.uber.org/zap"
1212
"strings"
13+
"time"
1314
)
1415

1516
type HttpTestCaseResult struct {
@@ -56,6 +57,11 @@ func (m *HttpTestCallable) Call(ctx context.Context) *executor.GPResult {
5657
}
5758
}
5859

60+
if m.testcase.Delay > 0 {
61+
zaplog.Debug("sleep", zap.Any("delay", m.testcase.Delay))
62+
time.Sleep(m.testcase.Delay)
63+
}
64+
5965
zaplog.Debug("before render()", zap.Any("request", m.testcase.Request))
6066
req, err := render(m.testcase.Request)
6167
tcResult.Request = req
@@ -69,7 +75,10 @@ func (m *HttpTestCallable) Call(ctx context.Context) *executor.GPResult {
6975
return &r
7076
}
7177

72-
in := resource.RestyClient.R()
78+
ctx, cancel := context.WithTimeout(context.Background(), resource.GlobalConfig.Global.RequestTimeout)
79+
defer cancel()
80+
81+
in := resource.RestyClient.R().SetContext(ctx)
7382
for _, item := range req.Headers {
7483
strList := strings.Split(item, ":")
7584
in.SetHeader(strings.TrimSpace(strList[0]), strings.TrimSpace(strList[1]))

internal/config/config.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package config
22

33
import (
44
"github.com/vearne/autotest/internal/rule"
5+
"time"
56
)
67

78
type AutoTestConfig struct {
89
Global struct {
9-
WorkerNum int `yaml:"worker_num"`
10-
IgnoreTestCaseFail bool `yaml:"ignore_testcase_fail"`
11-
Debug bool `yaml:"debug"`
10+
WorkerNum int `yaml:"worker_num"`
11+
IgnoreTestCaseFail bool `yaml:"ignore_testcase_fail"`
12+
Debug bool `yaml:"debug"`
13+
RequestTimeout time.Duration `yaml:"request_timeout"`
1214
Logger struct {
1315
Level string `yaml:"level"`
1416
FilePath string `yaml:"filepath"`
@@ -20,7 +22,10 @@ type AutoTestConfig struct {
2022
}
2123

2224
type TestCase struct {
23-
ID uint64 `yaml:"id"`
25+
ID uint64 `yaml:"id"`
26+
Desc string `yaml:"desc"`
27+
// Delay for a while before executing
28+
Delay time.Duration `yaml:"delay,omitempty"`
2429
Request Request `yaml:"request"`
2530
OriginRules []map[string]any `yaml:"rules" json:"-"`
2631
DependOnIDs []uint64 `yaml:"dependOnIDs,omitempty"`

internal/resource/resource.go

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"strings"
1515
"sync"
16+
"sync/atomic"
1617
)
1718

1819
var GlobalConfig config.AutoTestConfig
@@ -22,10 +23,12 @@ var EnvVars map[string]string
2223
var CustomerVars sync.Map
2324

2425
var RestyClient *resty.Client
26+
var TerminationFlag atomic.Bool
2527

2628
func init() {
2729
EnvVars = make(map[string]string, 10)
2830
HttpTestCases = make(map[string][]*config.TestCase, 10)
31+
TerminationFlag.Store(false)
2932
}
3033

3134
func InitRestyClient(debug bool) {

0 commit comments

Comments
 (0)