-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
214 lines (189 loc) · 6.85 KB
/
command_test.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package circuit
import (
"context"
"errors"
"fmt"
"testing"
"time"
)
func TestCommand_workflow(t *testing.T) {
t.Parallel()
// 功能函数。
run := func(ctx context.Context, i interface{}) (interface{}, error) {
param := i.(int)
param++
if param > 5000 {
return nil, errors.New("more then 5000")
}
return param, nil
}
// 降级函数。
fallback := func(ctx context.Context, i interface{}, e error) (interface{}, error) {
return nil, fmt.Errorf("fallback: %w", e)
}
// 初始化Command。
command := NewCommand("test", run, WithCommandFallback(fallback))
defer command.Close()
for i := 0; i < 10000; i++ {
r, err := command.Execute(i)
// 前5000正常。
if i < 5000 {
if err != nil {
t.Errorf("Command.Execute() got = %v, wantErr %v", err, nil)
}
if r.(int) != i+1 {
t.Errorf("Command.Execute() got = %v, want %v", r, i+1)
}
continue
}
// 后5000错误。
if err == nil || err.Error() != "fallback: more then 5000" {
t.Errorf("Command.Execute() got = %v, wantErr %v", err, "fallback: more then 5000")
}
}
// 再一个熔断。
if _, err := command.Execute(10001); err == nil || err.Error() != "fallback: test: open: command: unavailable" {
t.Errorf("Command.Execute() got = %v, want %v", err, "fallback: test: open: command: unavailable")
}
// 熔断中,正常的也熔断。
if _, err := command.Execute(1); err == nil || err.Error() != "fallback: test: open: command: unavailable" {
t.Errorf("Command.Execute() got = %v, want %v", err, "fallback: test: open: command: unavailable")
}
time.Sleep(5 * time.Second)
// 进入半熔断了,放入一个错误的。
if _, err := command.Execute(10001); err == nil || err.Error() != "fallback: more then 5000" {
t.Errorf("Command.Execute() got = %v, want %v", err, "fallback: more then 5000")
}
// 由于刚放了个错误的进行半熔断测试,又恢复熔断了。
if _, err := command.Execute(1); err == nil || err.Error() != "fallback: test: open: command: unavailable" {
t.Errorf("Command.Execute() got = %v, want %v", err, "fallback: test: open: command: unavailable")
}
time.Sleep(5 * time.Second)
// 进入半熔断了,放入一个正常的。
if _, err := command.Execute(1); err != nil {
t.Errorf("Command.Execute() got = %v, want %v", err, nil)
}
// 恢复了。
if _, err := command.Execute(2); err != nil {
t.Errorf("Command.Execute() got = %v, want %v", err, nil)
}
}
// TestCommand_withtimeout_workflow 由于加入超时机制后,将放入独立的goroutine中运行,执行流程与原本有区别,故独立测试一份。
func TestCommand_withtimeout_workflow(t *testing.T) {
t.Parallel()
// 功能函数。
run := func(ctx context.Context, i interface{}) (interface{}, error) {
param := i.(int)
param++
if param > 5000 {
return nil, errors.New("more then 5000")
}
return param, nil
}
// 降级函数。
fallback := func(ctx context.Context, i interface{}, e error) (interface{}, error) {
return nil, fmt.Errorf("fallback: %w", e)
}
// 初始化Command。
command := NewCommand("test", run, WithCommandFallback(fallback), WithCommandTimeout(time.Second*2))
defer command.Close()
for i := 0; i < 10000; i++ {
r, err := command.Execute(i)
// 前5000正常。
if i < 5000 {
if err != nil {
t.Errorf("Command.Execute() got = %v, wantErr %v", err, nil)
}
if r.(int) != i+1 {
t.Errorf("Command.Execute() got = %v, want %v", r, i+1)
}
continue
}
// 后5000错误。
if err == nil || err.Error() != "fallback: more then 5000" {
t.Errorf("Command.Execute() got = %v, wantErr %v", err, "fallback: more then 5000")
}
}
// 再一个熔断。
if _, err := command.Execute(10001); err == nil || err.Error() != "fallback: test: open: command: unavailable" {
t.Errorf("Command.Execute() got = %v, want %v", err, "fallback: test: open: command: unavailable")
}
// 熔断中,正常的也熔断。
if _, err := command.Execute(1); err == nil || err.Error() != "fallback: test: open: command: unavailable" {
t.Errorf("Command.Execute() got = %v, want %v", err, "fallback: test: open: command: unavailable")
}
time.Sleep(5 * time.Second)
// 进入半熔断了,放入一个错误的。
if _, err := command.Execute(10001); err == nil || err.Error() != "fallback: more then 5000" {
t.Errorf("Command.Execute() got = %v, want %v", err, "fallback: more then 5000")
}
// 由于刚放了个错误的进行半熔断测试,又恢复熔断了。
if _, err := command.Execute(1); err == nil || err.Error() != "fallback: test: open: command: unavailable" {
t.Errorf("Command.Execute() got = %v, want %v", err, "fallback: test: open: command: unavailable")
}
time.Sleep(5 * time.Second)
// 进入半熔断了,放入一个正常的。
if _, err := command.Execute(1); err != nil {
t.Errorf("Command.Execute() got = %v, want %v", err, nil)
}
// 恢复了。
if _, err := command.Execute(2); err != nil {
t.Errorf("Command.Execute() got = %v, want %v", err, nil)
}
}
func TestCommand_timeout(t *testing.T) {
t.Parallel()
// 功能函数。
run := func(ctx context.Context, i interface{}) (interface{}, error) {
time.Sleep(time.Second * time.Duration(i.(int)))
return i, nil
}
// 初始化Command。
command := NewCommand("test", run,
WithCommandTimeout(time.Second*2))
defer command.Close()
// 还没超时。
if _, err := command.Execute(1); err != nil {
t.Errorf("Command.Execute() got = %v, want nil", err)
}
// 超过默认超时。
if _, err := command.Execute(3); !errors.Is(err, ErrTimeout) {
t.Errorf("Command.Execute() got = %v, want nil", err)
}
// 测试下传入的超时。
startTime := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if _, err := command.ContextExecute(ctx, 2); !errors.Is(err, ErrTimeout) {
t.Errorf("Command.ContextExecute() got = %v, want %v", err, ErrTimeout)
}
// 此时应该时间过去1秒左右,允许一点时差。
if time.Since(startTime) > time.Second+time.Millisecond*100 {
t.Errorf("Command.ContextExecute() got = %v, want less than %v", time.Since(startTime), time.Second+time.Millisecond*100)
}
}
func TestCommand_fallback_timeout(t *testing.T) {
t.Parallel()
// 功能函数。
run := func(ctx context.Context, i interface{}) (interface{}, error) {
return i, errors.New("must err")
}
// 降级函数。
fallback := func(ctx context.Context, i interface{}, e error) (interface{}, error) {
time.Sleep(time.Second * time.Duration(i.(int)))
return i, nil
}
// 初始化Command。
command := NewCommand("test", run,
WithCommandFallback(fallback),
WithCommandTimeout(time.Second*2))
defer command.Close()
// 还没超时。
if _, err := command.Execute(1); err != nil {
t.Errorf("Command.Execute() got = %v, want nil", err)
}
// 超过默认超时。
if _, err := command.Execute(3); !errors.Is(err, ErrTimeout) {
t.Errorf("Command.Execute() got = %v, want nil", err)
}
}