-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrecover_test.go
88 lines (72 loc) · 1.84 KB
/
recover_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
package recover
import (
"context"
"errors"
"fmt"
"testing"
"github.com/lileio/pubsub/v2"
"github.com/lileio/pubsub/v2/providers/memory"
"github.com/lileio/pubsub/v2/test"
"github.com/stretchr/testify/assert"
)
type TestSubscriber struct {
JSON bool
T *testing.T
}
func (ts *TestSubscriber) PanicWithError(ctx context.Context, t *test.Account, msg *pubsub.Msg) error {
assert.True(ts.T, len(msg.Data) > 0)
panic(errors.New("this is an error"))
}
func (ts *TestSubscriber) PanicWithString(ctx context.Context, t *test.Account, msg *pubsub.Msg) error {
assert.True(ts.T, len(msg.Data) > 0)
panic("this is a panic")
}
func (ts *TestSubscriber) PanicUnknown(ctx context.Context, t *test.Account, msg *pubsub.Msg) error {
assert.True(ts.T, len(msg.Data) > 0)
panic(struct{}{})
}
func (ts *TestSubscriber) Setup(c *pubsub.Client) {
c.On(pubsub.HandlerOptions{
Topic: "with_error",
Name: "test",
Handler: ts.PanicWithError,
JSON: ts.JSON,
})
c.On(pubsub.HandlerOptions{
Topic: "with_string",
Name: "test",
Handler: ts.PanicWithString,
JSON: ts.JSON,
})
c.On(pubsub.HandlerOptions{
Topic: "with_unknown",
Name: "test",
Handler: ts.PanicUnknown,
JSON: ts.JSON,
})
}
func TestRecoverMiddleware(t *testing.T) {
m1 := Middleware{}
m := &memory.MemoryProvider{
ErrorHandler: func(err error) {
fmt.Printf("err = %+v\n", err)
assert.NotNil(t, err)
},
}
c := &pubsub.Client{
ServiceName: "test",
Provider: m,
Middleware: []pubsub.Middleware{m1},
}
ps := test.Account{
Name: "smth",
}
err := c.Publish(context.Background(), "with_error", &ps, false)
assert.Nil(t, err)
err = c.Publish(context.Background(), "with_string", &ps, false)
assert.Nil(t, err)
err = c.Publish(context.Background(), "with_unknown", &ps, false)
assert.Nil(t, err)
ts := TestSubscriber{T: t}
ts.Setup(c)
}