-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain_test.go
More file actions
176 lines (156 loc) · 4.15 KB
/
Copy pathchain_test.go
File metadata and controls
176 lines (156 loc) · 4.15 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
175
176
package pulse_test
import (
"context"
"errors"
"strings"
"testing"
pulse "algoryn.io/pulse"
)
var ctx = context.Background()
func ok(code int) pulse.Scenario {
return func(_ context.Context) (int, error) { return code, nil }
}
func fail(code int, err error) pulse.Scenario {
return func(_ context.Context) (int, error) { return code, err }
}
// -- Chain --
func TestChainRunsAllSteps(t *testing.T) {
var order []int
step := func(n int) pulse.Scenario {
return func(_ context.Context) (int, error) {
order = append(order, n)
return 200, nil
}
}
code, err := pulse.Sequence(step(1), step(2), step(3))(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
if len(order) != 3 || order[0] != 1 || order[1] != 2 || order[2] != 3 {
t.Fatalf("unexpected execution order: %v", order)
}
}
func TestChainStopsOnFirstError(t *testing.T) {
sentinel := errors.New("step failed")
var called bool
third := func(_ context.Context) (int, error) {
called = true
return 200, nil
}
code, err := pulse.Sequence(ok(200), fail(500, sentinel), third)(ctx)
if !errors.Is(err, sentinel) {
t.Fatalf("expected sentinel error, got %v", err)
}
if code != 500 {
t.Fatalf("expected status from failing step (500), got %d", code)
}
if called {
t.Fatal("third step must not run after a failure")
}
}
func TestChainReturnsLastStatusCode(t *testing.T) {
code, err := pulse.Sequence(ok(200), ok(201), ok(204))(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if code != 204 {
t.Fatalf("expected last step status 204, got %d", code)
}
}
func TestChainSingleStep(t *testing.T) {
code, err := pulse.Sequence(ok(202))(ctx)
if err != nil || code != 202 {
t.Fatalf("expected (202, nil), got (%d, %v)", code, err)
}
}
func TestChainPanicsOnEmpty(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("expected panic for empty steps")
}
}()
pulse.Sequence()
}
func TestChainRespectsContextCancellation(t *testing.T) {
cctx, cancel := context.WithCancel(ctx)
cancel()
scenario := pulse.Sequence(func(c context.Context) (int, error) {
return 0, c.Err()
})
_, err := scenario(cctx)
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected context.Canceled, got %v", err)
}
}
// -- Flow --
func TestFlowRunsAllSteps(t *testing.T) {
var order []string
named := func(name string) pulse.Step {
return pulse.Step{Name: name, Do: func(_ context.Context) (int, error) {
order = append(order, name)
return 200, nil
}}
}
_, err := pulse.Flow(named("login"), named("fetch"), named("logout"))(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if strings.Join(order, ",") != "login,fetch,logout" {
t.Fatalf("unexpected order: %v", order)
}
}
func TestFlowWrapsErrorWithStepName(t *testing.T) {
sentinel := errors.New("unauthorized")
_, err := pulse.Flow(
pulse.Step{Name: "login", Do: fail(401, sentinel)},
pulse.Step{Name: "fetch", Do: ok(200)},
)(ctx)
if !errors.Is(err, sentinel) {
t.Fatalf("expected wrapped sentinel, got %v", err)
}
if !strings.Contains(err.Error(), "login") {
t.Fatalf("error should contain step name, got: %v", err)
}
}
func TestFlowStopsOnFirstError(t *testing.T) {
var fetchCalled bool
_, err := pulse.Flow(
pulse.Step{Name: "login", Do: fail(500, errors.New("server error"))},
pulse.Step{Name: "fetch", Do: func(_ context.Context) (int, error) {
fetchCalled = true
return 200, nil
}},
)(ctx)
if err == nil {
t.Fatal("expected error, got nil")
}
if fetchCalled {
t.Fatal("fetch must not run after login failure")
}
}
func TestFlowUnnamedStepDoesNotWrapError(t *testing.T) {
sentinel := errors.New("bare error")
_, err := pulse.Flow(pulse.Step{Do: fail(500, sentinel)})(ctx)
if err.Error() != sentinel.Error() {
t.Fatalf("unnamed step should not wrap error, got: %v", err)
}
}
func TestFlowPanicsOnEmpty(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("expected panic for empty steps")
}
}()
pulse.Flow()
}
func TestFlowPanicsOnNilDo(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("expected panic for nil Do")
}
}()
pulse.Flow(pulse.Step{Name: "bad", Do: nil})
}