-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguard_test.go
More file actions
215 lines (168 loc) · 5.23 KB
/
Copy pathguard_test.go
File metadata and controls
215 lines (168 loc) · 5.23 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
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
215
package plumber_test
import (
"context"
"fmt"
"time"
"github.com/cenk1cenk2/plumber/v7"
plumbertests "github.com/cenk1cenk2/plumber/v7/tests"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type guardCase struct {
job func(*bool) plumber.Job
assert func(bool)
}
var _ = Describe("guards", func() {
DescribeTable("should guard jobs",
func(ctx SpecContext, tc guardCase) {
handled := false
Expect(tc.job(&handled)(ctx)).To(Succeed())
if tc.assert != nil {
tc.assert(handled)
}
},
Entry("ignore panic", guardCase{
job: func(_ *bool) plumber.Job {
return plumber.GuardIgnorePanic(plumber.CreateJob(func() error {
panic("ignored")
}))
},
}),
Entry("handle panic", guardCase{
job: func(handled *bool) plumber.Job {
return plumber.GuardOnPanic(plumber.CreateJob(func() error {
panic("handled")
}), func() {
*handled = true
})
},
assert: func(handled bool) {
Expect(handled).To(BeTrue())
},
}),
Entry("resume failed job", guardCase{
job: func(_ *bool) plumber.Job {
return plumber.GuardResume(plumber.CreateJob(func() error {
return fmt.Errorf("failed")
}))
},
}),
Entry("timeout successful job", guardCase{
job: func(_ *bool) plumber.Job {
return plumber.GuardTimeout(plumber.CreateJob(func() error {
return nil
}), time.Millisecond)
},
}),
)
Describe("panic", func() {
It("should turn the panic into an error", func(ctx SpecContext) {
err := plumber.GuardPanic(plumber.CreateJob(func() error {
panic("exploded")
}))(ctx)
Expect(err).To(MatchError("Job has panicked: exploded"))
})
It("should keep the error of the panic wrapped", func(ctx SpecContext) {
inner := fmt.Errorf("exploded")
err := plumber.GuardPanic(plumber.CreateJob(func() error {
panic(inner)
}))(ctx)
Expect(err).To(MatchError(inner))
})
})
Describe("timeout", func() {
It("should stop waiting for the job when the time is out", func(ctx SpecContext) {
err := plumber.GuardTimeout(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}, time.Millisecond)(ctx)
Expect(err).To(MatchError(context.DeadlineExceeded))
})
It("should call the handler when the time is out", func(ctx SpecContext) {
handled := false
err := plumber.GuardOnTimeout(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}, func() {
handled = true
}, time.Millisecond)(ctx)
Expect(err).To(MatchError(context.DeadlineExceeded))
Expect(handled).To(BeTrue())
})
It("should return the error of the flow when it is cancelled before the time is out", func(ctx SpecContext) {
cancelled, cancel := context.WithCancel(ctx)
go func() {
time.Sleep(5 * time.Millisecond)
cancel()
}()
err := plumber.GuardTimeout(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}, time.Hour)(cancelled)
Expect(err).To(MatchError(context.Canceled))
})
})
Describe("ignore cancel", func() {
It("should pass through the errors that are not caused by cancellation", func(ctx SpecContext) {
err := plumber.GuardIgnoreCancel(plumber.CreateJob(func() error {
return fmt.Errorf("failed")
}))(ctx)
Expect(err).To(MatchError("failed"))
})
It("should swallow the errors that do not even wrap the cancellation", func(ctx SpecContext) {
cancelled, cancel := context.WithCancel(ctx)
cancel()
Expect(plumber.GuardIgnoreCancel(plumber.CreateJob(func() error {
return fmt.Errorf("signal: killed")
}))(cancelled)).To(Succeed())
})
It("should stay interruptible", func(ctx SpecContext) {
cancelled, cancel := context.WithCancel(ctx)
go func() {
time.Sleep(5 * time.Millisecond)
cancel()
}()
Expect(plumber.GuardIgnoreCancel(func(ctx context.Context) error {
<-ctx.Done()
return fmt.Errorf("signal: killed")
})(cancelled)).To(Succeed())
})
})
Describe("resume", func() {
It("should log and swallow every error", func(ctx SpecContext) {
log, capture := plumbertests.NewCaptureLogger()
Expect(plumber.GuardResume(plumber.CreateJob(func() error {
return fmt.Errorf("failed")
}), log)(ctx)).To(Succeed())
Expect(capture.Messages()).To(ContainElement("Job has failed, resuming the flow: failed"))
})
})
Describe("always", func() {
It("should run the job even when the flow is already cancelled", func(ctx SpecContext) {
cancelled, cancel := context.WithCancel(ctx)
cancel()
ran := false
Expect(plumber.GuardAlways(func(ctx context.Context) error {
ran = ctx.Err() == nil
return nil
})(cancelled)).To(Succeed())
Expect(ran).To(BeTrue())
})
It("should pass through the errors of the job", func(ctx SpecContext) {
cancelled, cancel := context.WithCancel(ctx)
cancel()
err := plumber.GuardAlways(plumber.CreateJob(func() error {
return fmt.Errorf("failed")
}))(cancelled)
Expect(err).To(MatchError("failed"))
})
It("should swallow the errors that are caused by the grace period running out", func(ctx SpecContext) {
cancelled, cancel := context.WithCancel(ctx)
cancel()
Expect(plumber.GuardAlways(func(ctx context.Context) error {
<-ctx.Done()
return fmt.Errorf("signal: killed")
}, time.Millisecond)(cancelled)).To(Succeed())
})
})
})