-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesthelpers_test.go
More file actions
174 lines (143 loc) · 4.28 KB
/
testhelpers_test.go
File metadata and controls
174 lines (143 loc) · 4.28 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
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
package task_engine_test
import (
"context"
"errors"
"io"
"log/slog"
"time"
task_engine "github.com/ndizazzo/task-engine"
)
const (
StaticActionTime = 10 * time.Millisecond
LongActionTime = 500 * time.Millisecond
)
// TestAction is a simple test action that records execution and optionally fails.
type TestAction struct {
task_engine.BaseAction
Called bool
ShouldFail bool
}
func (a *TestAction) Execute(ctx context.Context) error {
a.Called = true
if a.ShouldFail {
return errors.New("simulated failure")
}
return nil
}
// DelayAction sleeps for a fixed duration.
type DelayAction struct {
task_engine.BaseAction
Delay time.Duration
}
func (a *DelayAction) Execute(ctx context.Context) error {
time.Sleep(a.Delay)
return nil
}
// BeforeExecuteFailingAction optionally fails in BeforeExecute.
type BeforeExecuteFailingAction struct {
task_engine.BaseAction
ShouldFailBefore bool
}
func (a *BeforeExecuteFailingAction) BeforeExecute(ctx context.Context) error {
if a.ShouldFailBefore {
return errors.New("simulated BeforeExecute failure")
}
return nil
}
func (a *BeforeExecuteFailingAction) Execute(ctx context.Context) error { return nil }
// AfterExecuteFailingAction optionally fails in AfterExecute.
type AfterExecuteFailingAction struct {
task_engine.BaseAction
ShouldFailAfter bool
}
func (a *AfterExecuteFailingAction) BeforeExecute(ctx context.Context) error { return nil }
func (a *AfterExecuteFailingAction) Execute(ctx context.Context) error { return nil }
// CancelAwareAction returns context error if canceled, otherwise completes after Delay.
type CancelAwareAction struct {
task_engine.BaseAction
Delay time.Duration
}
func (a *CancelAwareAction) Execute(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(a.Delay):
return nil
}
}
// testResultProvider is a minimal ResultProvider for tests.
type testResultProvider struct{ v interface{} }
func (p testResultProvider) GetResult() interface{} { return p.v }
func (p testResultProvider) GetError() error { return nil }
// mockActionWithOutput implements ActionInterface and produces a fixed output value.
type mockActionWithOutput struct {
task_engine.BaseAction
output interface{}
}
func (a *mockActionWithOutput) Execute(ctx context.Context) error { return nil }
func (a *mockActionWithOutput) GetOutput() interface{} { return a.output }
// NewDiscardLogger creates a new logger that discards all output.
func NewDiscardLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(io.Discard, nil))
}
var (
// DiscardLogger is a logger that discards all log output, useful for tests.
DiscardLogger = slog.New(slog.NewTextHandler(io.Discard, nil))
// noOpLogger is kept for backward compatibility with task_manager_test.go.
noOpLogger = DiscardLogger
PassingTestAction = &task_engine.Action[*TestAction]{
ID: "passing-action-1",
Wrapped: &TestAction{
BaseAction: task_engine.BaseAction{},
Called: false,
},
}
FailingTestAction = &task_engine.Action[*TestAction]{
ID: "failing-action-1",
Wrapped: &TestAction{
BaseAction: task_engine.BaseAction{},
ShouldFail: true,
},
}
LongRunningAction = &task_engine.Action[*DelayAction]{
ID: "long-running-action",
Wrapped: &DelayAction{
BaseAction: task_engine.BaseAction{},
Delay: LongActionTime,
},
}
BeforeExecuteFailingTestAction = &task_engine.Action[*BeforeExecuteFailingAction]{
ID: "before-execute-failing-action",
Wrapped: &BeforeExecuteFailingAction{
BaseAction: task_engine.BaseAction{},
ShouldFailBefore: true,
},
}
AfterExecuteFailingTestAction = &task_engine.Action[*AfterExecuteFailingAction]{
ID: "after-execute-failing-action",
Wrapped: &AfterExecuteFailingAction{
BaseAction: task_engine.BaseAction{},
ShouldFailAfter: true,
},
}
SingleAction = []task_engine.ActionWrapper{
PassingTestAction,
}
MultipleActionsSuccess = []task_engine.ActionWrapper{
PassingTestAction,
PassingTestAction,
}
MultipleActionsFailure = []task_engine.ActionWrapper{
PassingTestAction,
FailingTestAction,
}
LongRunningActions = []task_engine.ActionWrapper{
LongRunningAction,
}
ManyTasksForCancellation = []task_engine.ActionWrapper{
LongRunningAction,
PassingTestAction,
PassingTestAction,
LongRunningAction,
}
)