-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
207 lines (180 loc) · 4.86 KB
/
Copy patherrors_test.go
File metadata and controls
207 lines (180 loc) · 4.86 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
package senna
import (
"errors"
"strings"
"testing"
"time"
)
func TestRetryableError_Error(t *testing.T) {
t.Parallel()
job := NewJob("test_job", nil)
cause := errors.New("temporary failure")
retryIn := 5 * time.Second
err := &RetryableError{
Job: job,
Cause: cause,
RetryIn: retryIn,
}
msg := err.Error()
if msg == "" {
t.Error("error message should not be empty")
}
if !strings.Contains(msg, job.ID) {
t.Errorf("error message should contain job ID, got: %s", msg)
}
if !strings.Contains(msg, "temporary failure") {
t.Errorf("error message should contain cause, got: %s", msg)
}
if !strings.Contains(msg, "5s") {
t.Errorf("error message should contain retry duration, got: %s", msg)
}
}
func TestRetryableError_Unwrap(t *testing.T) {
t.Parallel()
cause := errors.New("underlying error")
err := &RetryableError{
Job: NewJob("test", nil),
Cause: cause,
}
unwrapped := err.Unwrap()
if unwrapped != cause {
t.Errorf("expected unwrapped error to be cause, got: %v", unwrapped)
}
if !errors.Is(err, cause) {
t.Error("errors.Is should return true for wrapped cause")
}
}
func TestMaxRetriesExceededError_Error(t *testing.T) {
t.Parallel()
job := NewJob("failing_job", nil)
job.RetryCount = 5
cause := errors.New("persistent failure")
err := &MaxRetriesExceededError{
Job: job,
Cause: cause,
}
msg := err.Error()
if msg == "" {
t.Error("error message should not be empty")
}
if !strings.Contains(msg, job.ID) {
t.Errorf("error message should contain job ID, got: %s", msg)
}
if !strings.Contains(msg, "5") {
t.Errorf("error message should contain retry count, got: %s", msg)
}
if !strings.Contains(msg, "persistent failure") {
t.Errorf("error message should contain cause, got: %s", msg)
}
}
func TestMaxRetriesExceededError_Unwrap(t *testing.T) {
t.Parallel()
cause := errors.New("underlying error")
err := &MaxRetriesExceededError{
Job: NewJob("test", nil),
Cause: cause,
}
unwrapped := err.Unwrap()
if unwrapped != cause {
t.Errorf("expected unwrapped error to be cause, got: %v", unwrapped)
}
if !errors.Is(err, cause) {
t.Error("errors.Is should return true for wrapped cause")
}
}
func TestJobNotFoundError_Error(t *testing.T) {
t.Parallel()
err := &JobNotFoundError{JobID: "job-123-abc"}
msg := err.Error()
if msg == "" {
t.Error("error message should not be empty")
}
if !strings.Contains(msg, "job-123-abc") {
t.Errorf("error message should contain job ID, got: %s", msg)
}
}
func TestQueuePausedError_Error(t *testing.T) {
t.Parallel()
err := &QueuePausedError{Queue: "critical"}
msg := err.Error()
if msg == "" {
t.Error("error message should not be empty")
}
if !strings.Contains(msg, "critical") {
t.Errorf("error message should contain queue name, got: %s", msg)
}
}
func TestDuplicateJobError_Error(t *testing.T) {
t.Parallel()
err := &DuplicateJobError{UniqueKey: "user:123:sync"}
msg := err.Error()
if msg == "" {
t.Error("error message should not be empty")
}
if !strings.Contains(msg, "user:123:sync") {
t.Errorf("error message should contain unique key, got: %s", msg)
}
}
func TestBatchNotFoundError_Error(t *testing.T) {
t.Parallel()
err := &BatchNotFoundError{BatchID: "batch-456-xyz"}
msg := err.Error()
if msg == "" {
t.Error("error message should not be empty")
}
if !strings.Contains(msg, "batch-456-xyz") {
t.Errorf("error message should contain batch ID, got: %s", msg)
}
}
func TestErrors_TypeAssertions(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err error
}{
{"RetryableError", &RetryableError{Job: NewJob("t", nil)}},
{"MaxRetriesExceededError", &MaxRetriesExceededError{Job: NewJob("t", nil)}},
{"JobNotFoundError", &JobNotFoundError{JobID: "x"}},
{"QueuePausedError", &QueuePausedError{Queue: "q"}},
{"DuplicateJobError", &DuplicateJobError{UniqueKey: "k"}},
{"BatchNotFoundError", &BatchNotFoundError{BatchID: "b"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.err.Error() == "" {
t.Errorf("%s.Error() should not be empty", tt.name)
}
})
}
}
func TestRetryableError_ErrorsAs(t *testing.T) {
t.Parallel()
original := &RetryableError{
Job: NewJob("test", nil),
Cause: errors.New("fail"),
RetryIn: time.Second,
}
var target *RetryableError
if !errors.As(original, &target) {
t.Error("errors.As should match RetryableError")
}
if target.RetryIn != time.Second {
t.Errorf("expected RetryIn 1s, got %v", target.RetryIn)
}
}
func TestMaxRetriesExceededError_ErrorsAs(t *testing.T) {
t.Parallel()
job := NewJob("test", nil)
job.RetryCount = 10
original := &MaxRetriesExceededError{
Job: job,
Cause: errors.New("fail"),
}
var target *MaxRetriesExceededError
if !errors.As(original, &target) {
t.Error("errors.As should match MaxRetriesExceededError")
}
if target.Job.RetryCount != 10 {
t.Errorf("expected RetryCount 10, got %d", target.Job.RetryCount)
}
}