-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecovery_test.go
93 lines (78 loc) · 1.86 KB
/
recovery_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
package recovery
import (
"context"
"strings"
"testing"
"time"
"github.com/flc1125/go-cron/crontest/v4/logger"
"github.com/flc1125/go-cron/v4"
"github.com/stretchr/testify/assert"
)
type panicJob struct{}
func (p panicJob) Run(context.Context) error {
panic("YOLO")
}
func TestRecovery(t *testing.T) {
buf := logger.NewBuffer()
recovery := New(
WithLogger(logger.NewBufferLogger(buf)),
)
assert.NotPanics(t, func() {
_ = recovery(cron.JobFunc(func(context.Context) error {
panic("YOLO")
})).Run(context.Background())
})
assert.True(t, strings.Contains(buf.String(), "YOLO"))
}
func TestRecovery_FuncPanic(t *testing.T) {
buf := logger.NewBuffer()
c := cron.New(
cron.WithSeconds(),
cron.WithMiddleware(
New(
WithLogger(logger.NewBufferLogger(buf)),
),
),
)
c.Start()
defer c.Stop()
_, err := c.AddFunc("* * * * * ?", func(context.Context) error {
panic("YOLO")
})
assert.NoError(t, err)
time.Sleep(time.Second)
assert.True(t, strings.Contains(buf.String(), "YOLO"))
}
func TestRecovery_JobPanic(t *testing.T) {
buf := logger.NewBuffer()
c := cron.New(
cron.WithSeconds(),
cron.WithMiddleware(
New(
WithLogger(logger.NewBufferLogger(buf)),
),
),
)
c.Start()
defer c.Stop()
_, err := c.AddJob("* * * * * ?", panicJob{})
assert.NoError(t, err)
time.Sleep(time.Second)
assert.True(t, strings.Contains(buf.String(), "YOLO"))
}
func TestRecovery_ChainPanic(t *testing.T) {
t.Run("default panic exits job", func(*testing.T) {
assert.Panics(t, func() {
_ = cron.Chain()(panicJob{}).Run(context.Background())
})
})
t.Run("recovering job wrapper recovers", func(*testing.T) {
var buf logger.Buffer
assert.NotPanics(t, func() {
_ = cron.Chain(
New(WithLogger(logger.NewBufferLogger(&buf))),
)(panicJob{}).Run(context.Background())
})
assert.True(t, strings.Contains(buf.String(), "YOLO"))
})
}