-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathterminator_executor_test.go
114 lines (100 loc) · 2.63 KB
/
terminator_executor_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
package core
import (
"context"
"errors"
"testing"
"time"
)
var errGeneric = errors.New("Error")
func withError(ctx context.Context) (bool, error) {
time.Sleep(1 * time.Second)
return false, errGeneric
}
func withSuccess(ctx context.Context) (bool, error) {
time.Sleep(1 * time.Second)
return true, nil
}
func exceedMaxRetries(ctx context.Context) (bool, error) {
time.Sleep(1 * time.Second)
return false, nil
}
type terminatorExecutorTestCase struct {
name string
executor Executor
maxRetries int
interval time.Duration
checkTerminate func(ctx context.Context, exec Executor, result ResultWithValue) (terminated bool, err error)
expectedError error
expectedValue bool
}
func TestExecuteTerminatorWithCheck(t *testing.T) {
ct := func(ctx context.Context, exec Executor, result ResultWithValue) (terminated bool, err error) {
if exec.Name() == "ExceedMaxRetries" {
return false, err
}
return true, err
}
tests := []terminatorExecutorTestCase{
{
maxRetries: 3,
interval: 1 * time.Second,
checkTerminate: ct,
expectedValue: true,
expectedError: nil,
executor: NewStaticExecuteSimple(
DescriptorSpec{
Name: "Success",
Description: "Success",
},
withSuccess),
},
{
maxRetries: 3,
interval: 1 * time.Second,
checkTerminate: ct,
expectedValue: false,
expectedError: errGeneric,
executor: NewStaticExecuteSimple(
DescriptorSpec{
Name: "Error",
Description: "Error",
},
withError),
},
{
maxRetries: 3,
interval: 1 * time.Second,
checkTerminate: ct,
expectedValue: false,
expectedError: errGeneric,
executor: NewStaticExecuteSimple(
DescriptorSpec{
Name: "ExceedMaxRetries",
Description: "ExceedMaxRetries",
},
exceedMaxRetries),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
executor := &executeTerminatorWithCheck{
Executor: tc.executor,
maxRetries: tc.maxRetries,
interval: tc.interval,
checkTerminate: tc.checkTerminate,
}
ctx := context.Background()
parameters := make(map[string]interface{})
configs := make(map[string]interface{})
var result interface{} = false
exeRes, err := executor.ExecuteUntilTermination(ctx, parameters, configs)
resWV, hasValue := ResultAs[ResultWithValue](exeRes)
if hasValue {
result = resWV.Value()
}
if _, ok := err.(FailedTerminationError); ok == false && err != tc.expectedError && result != tc.expectedValue {
t.Errorf("expected err == %s, found: %s", tc.expectedError.Error(), err.Error())
}
})
}
}