-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevil.go
More file actions
106 lines (91 loc) · 2.25 KB
/
Copy pathevil.go
File metadata and controls
106 lines (91 loc) · 2.25 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package EvilGo
import (
"math/rand"
"time"
)
var randInstance *rand.Rand
// init to load rander funcs.
func init() {
randInstance = rand.New(rand.NewSource(time.Now().Unix()))
}
// EvilConfig enable to control the behavior of the EvilGo. When Evil start, you can't disable in anywhere.
type EvilConfig struct {
BlockTimeConfig BlockTimeConfig
ReplacePrintfConfig ReplacePrintfConfig
}
type ReplacePrintfConfig struct {
Weight int
Value string
Duration time.Duration
}
type BlockTimeConfig struct {
Weight int
BlockTime time.Time
BlockDuration time.Duration
}
type EvilOptions func(e *EvilConfig)
func SetBlockTimeWeight(weight int) EvilOptions {
return func(e *EvilConfig) {
e.BlockTimeConfig.Weight = weight
}
}
func SetReplacePrintlnWeight(weight int) EvilOptions {
return func(e *EvilConfig) {
e.ReplacePrintfConfig.Weight = weight
}
}
func SetBlockTimeDuration(d time.Duration) EvilOptions {
return func(e *EvilConfig) {
e.BlockTimeConfig.BlockDuration = d
}
}
func SetReplacePrintlnDuration(d time.Duration) EvilOptions {
return func(e *EvilConfig) {
e.ReplacePrintfConfig.Duration = d
}
}
func SetBlockTime(t time.Time) EvilOptions {
return func(e *EvilConfig) {
e.BlockTimeConfig.BlockTime = t
}
}
func SetReplacePrintlnValue(value string) EvilOptions {
return func(e *EvilConfig) {
e.ReplacePrintfConfig.Value = value
}
}
// Evil will start the rand bugs function. Because the golang load model, the developers need invoke this function by
// hand.
func Evil(options ...EvilOptions) {
config := &EvilConfig{
BlockTimeConfig: BlockTimeConfig{
Weight: 1,
BlockTime: time.Date(0, 0, 0, 0, 0, 0, 0, time.Local),
BlockDuration: 5 * time.Second,
},
ReplacePrintfConfig: ReplacePrintfConfig{
Weight: 1,
Value: "Evil for go is inject ;-)",
Duration: 5 * time.Second,
},
}
for _, option := range options {
option(config)
}
go func() {
for {
time.Sleep(1 * time.Second)
// block time
BlockTimeRandom(
config.BlockTimeConfig.Weight,
config.BlockTimeConfig.BlockTime,
config.BlockTimeConfig.BlockDuration,
)
EvilPrintf(
config.ReplacePrintfConfig.Weight,
config.ReplacePrintfConfig.Value,
config.ReplacePrintfConfig.Duration,
)
}
}()
}