Repository navigation
Expand file tree
/
Copy patherror_test.go
More file actions
104 lines (82 loc) · 2.62 KB
/
Copy patherror_test.go
File metadata and controls
104 lines (82 loc) · 2.62 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
// Package river_test is an external test package for river. It is separated
// from the internal package to ensure that no internal packages are relied on.
package river_test
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/riverqueue/river"
)
func TestUnknownJobKindError_As(t *testing.T) {
// This test isn't really necessary because we didn't have to write any code
// to make it pass, but it does demonstrate that we can successfully use
// errors.As with this custom error type.
t.Parallel()
t.Run("ReturnsTrueForAnotherUnregisteredKindError", func(t *testing.T) {
t.Parallel()
err1 := &river.UnknownJobKindError{Kind: "MyJobArgs"}
var err2 *river.UnknownJobKindError
require.ErrorAs(t, err1, &err2)
require.Equal(t, err1, err2)
require.Equal(t, err1.Kind, err2.Kind)
})
t.Run("ReturnsFalseForADifferentError", func(t *testing.T) {
t.Parallel()
var err *river.UnknownJobKindError
require.NotErrorAs(t, errors.New("some other error"), &err)
})
}
func TestUnknownJobKindError_Is(t *testing.T) {
t.Parallel()
t.Run("ReturnsTrueForAnotherUnregisteredKindError", func(t *testing.T) {
t.Parallel()
err1 := &river.UnknownJobKindError{Kind: "MyJobArgs"}
require.ErrorIs(t, err1, &river.UnknownJobKindError{})
})
t.Run("ReturnsFalseForADifferentError", func(t *testing.T) {
t.Parallel()
err1 := &river.UnknownJobKindError{Kind: "MyJobArgs"}
require.NotErrorIs(t, err1, errors.New("some other error"))
})
}
func TestJobCancel(t *testing.T) {
t.Parallel()
t.Run("ErrorsIsReturnsTrueForAnotherErrorOfSameType", func(t *testing.T) {
t.Parallel()
err1 := river.JobCancel(errors.New("some message"))
require.ErrorIs(t, err1, river.JobCancel(errors.New("another message")))
})
t.Run("ErrorsIsReturnsFalseForADifferentErrorType", func(t *testing.T) {
t.Parallel()
err1 := river.JobCancel(errors.New("some message"))
require.NotErrorIs(t, err1, &river.UnknownJobKindError{Kind: "MyJobArgs"})
})
}
func TestJobSnooze(t *testing.T) {
t.Parallel()
t.Run("PanicsForNegativeDuration", func(t *testing.T) {
t.Parallel()
for _, duration := range []time.Duration{
-time.Nanosecond,
-time.Second,
} {
require.PanicsWithValue(t, "JobSnooze: duration must be >= 0", func() {
_ = river.JobSnooze(duration)
})
}
})
t.Run("AcceptsNonNegativeDuration", func(t *testing.T) {
t.Parallel()
for _, duration := range []time.Duration{
0,
time.Second,
5 * time.Minute,
} {
err := river.JobSnooze(duration)
var snoozeErr *river.JobSnoozeError
require.ErrorAs(t, err, &snoozeErr)
require.Equal(t, duration, snoozeErr.Duration)
}
})
}