-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgun_sleep_mock.go
67 lines (61 loc) · 1.61 KB
/
gun_sleep_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package wasp
import (
"math/rand"
"time"
)
// MockGunConfig configures a mock gun
type MockGunConfig struct {
// FailRatio in percentage, 0-100
FailRatio int
// TimeoutRatio in percentage, 0-100
TimeoutRatio int
// CallSleep time spent waiting inside a call
CallSleep time.Duration
// InternalStop break the test immediately
InternalStop bool
}
// MockGun is a mock gun
type MockGun struct {
cfg *MockGunConfig
Data []string
}
// NewMockGun create a mock gun
func NewMockGun(cfg *MockGunConfig) *MockGun {
return &MockGun{
cfg: cfg,
Data: make([]string, 0),
}
}
// Call implements example gun call, assertions on response bodies should be done here
func (m *MockGun) Call(l *Generator) *Response {
if m.cfg.InternalStop {
l.Stop()
}
time.Sleep(m.cfg.CallSleep)
if m.cfg.FailRatio > 0 && m.cfg.FailRatio <= 100 {
//nolint
r := rand.Intn(100)
if r <= m.cfg.FailRatio {
return &Response{Data: "failedCallData", Error: "error", Failed: true}
}
}
if m.cfg.TimeoutRatio > 0 && m.cfg.TimeoutRatio <= 100 {
//nolint
r := rand.Intn(100)
if r <= m.cfg.TimeoutRatio {
time.Sleep(m.cfg.CallSleep + 20*time.Millisecond)
}
}
return &Response{Data: "successCallData"}
}
func convertResponsesData(g *Generator) ([]string, []*Response, []*Response) {
g.responsesData.okDataMu.Lock()
defer g.responsesData.okDataMu.Unlock()
g.responsesData.failResponsesMu.Lock()
defer g.responsesData.failResponsesMu.Unlock()
ok := make([]string, 0)
for _, d := range g.responsesData.OKData.Data {
ok = append(ok, d.(string))
}
return ok, g.responsesData.OKResponses.Data, g.responsesData.FailResponses.Data
}