-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
83 lines (58 loc) · 1.79 KB
/
middleware_test.go
File metadata and controls
83 lines (58 loc) · 1.79 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
package panictoerror
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/riverqueue/river/rivershared/baseservice"
"github.com/riverqueue/river/rivershared/riversharedtest"
"github.com/riverqueue/river/rivertype"
)
func TestMiddleware(t *testing.T) {
t.Parallel()
ctx := t.Context()
type testBundle struct{}
setupConfig := func(t *testing.T, config *MiddlewareConfig) (*Middleware, *testBundle) {
t.Helper()
return baseservice.Init(
riversharedtest.BaseServiceArchetype(t),
NewMiddleware(config),
), &testBundle{}
}
setup := func(t *testing.T) (*Middleware, *testBundle) {
t.Helper()
return setupConfig(t, nil)
}
t.Run("NoError", func(t *testing.T) {
t.Parallel()
middleware, _ := setup(t)
require.NoError(t, middleware.Work(ctx, &rivertype.JobRow{}, func(context.Context) error { return nil }))
})
t.Run("InnerMiddlewareReturnsError", func(t *testing.T) {
t.Parallel()
middleware, _ := setup(t)
expectedErr := errors.New("my error")
require.ErrorIs(t, middleware.Work(ctx, &rivertype.JobRow{}, func(context.Context) error {
return expectedErr
}), expectedErr)
})
t.Run("PanicReturnedAsError", func(t *testing.T) {
t.Parallel()
middleware, _ := setup(t)
err := middleware.Work(ctx, &rivertype.JobRow{}, func(context.Context) error {
panic("my panic")
})
var panicErr *PanicError
require.ErrorAs(t, err, &panicErr)
require.Equal(t, "my panic", panicErr.Cause)
t.Log(panicErr.Error())
// Looking for this function to be the top of trace (i.e. we skipped all
// the internal frames that were in there).
require.Contains(t, panicErr.Trace[0].Function, "TestMiddleware")
})
}
func TestPanicErrorIs(t *testing.T) {
t.Parallel()
err := &PanicError{}
require.ErrorIs(t, err, &PanicError{})
}