-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy patherrors_test.go
More file actions
214 lines (198 loc) · 5.46 KB
/
errors_test.go
File metadata and controls
214 lines (198 loc) · 5.46 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
208
209
210
211
212
213
214
package gorums
import (
"context"
"errors"
"testing"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestQuorumCallErrorIs(t *testing.T) {
tests := []struct {
name string
err error
target error
want bool
}{
{
name: "SameCauseError",
err: QuorumCallError{cause: ErrIncomplete},
target: ErrIncomplete,
want: true,
},
{
name: "SameCauseQCError",
err: QuorumCallError{cause: ErrIncomplete},
target: QuorumCallError{cause: ErrIncomplete},
want: true,
},
{
name: "DifferentError",
err: QuorumCallError{cause: ErrIncomplete},
target: errors.New("incomplete call"),
want: false,
},
{
name: "DifferentQCError",
err: QuorumCallError{cause: ErrIncomplete},
target: QuorumCallError{cause: errors.New("incomplete call")},
want: false,
},
{
name: "ContextCanceled",
err: QuorumCallError{cause: context.Canceled},
target: context.Canceled,
want: true,
},
{
name: "ContextCanceledQC",
err: QuorumCallError{cause: context.Canceled},
target: QuorumCallError{cause: context.Canceled},
want: true,
},
{
name: "ContextDeadlineExceeded",
err: QuorumCallError{cause: context.DeadlineExceeded},
target: context.DeadlineExceeded,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := errors.Is(tt.err, tt.target); got != tt.want {
t.Errorf("QuorumCallError.Is(%v, %v) = %v, want %v", tt.err, tt.target, got, tt.want)
}
})
}
}
func TestQuorumCallErrorAccessors(t *testing.T) {
tests := []struct {
name string
qcErr QuorumCallError
wantCause error
wantNodeErrors int
}{
{
name: "NoErrors",
qcErr: QuorumCallError{
cause: ErrIncomplete,
errors: nil,
},
wantCause: ErrIncomplete,
wantNodeErrors: 0,
},
{
name: "SingleError",
qcErr: QuorumCallError{
cause: ErrIncomplete,
errors: []nodeError{
{nodeID: 1, cause: status.Error(codes.Unavailable, "node down")},
},
},
wantCause: ErrIncomplete,
wantNodeErrors: 1,
},
{
name: "MultipleErrors",
qcErr: QuorumCallError{
cause: ErrIncomplete,
errors: []nodeError{
{nodeID: 1, cause: status.Error(codes.Unavailable, "node down")},
{nodeID: 3, cause: status.Error(codes.DeadlineExceeded, "timeout")},
{nodeID: 5, cause: status.Error(codes.Unavailable, "connection refused")},
},
},
wantCause: ErrIncomplete,
wantNodeErrors: 3,
},
{
name: "SendFailure",
qcErr: QuorumCallError{
cause: ErrSendFailure,
errors: []nodeError{
{nodeID: 2, cause: errors.New("send failed")},
},
},
wantCause: ErrSendFailure,
wantNodeErrors: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.qcErr.Cause(); got != tt.wantCause {
t.Errorf("QuorumCallError.Cause() = %v, want %v", got, tt.wantCause)
}
if got := tt.qcErr.NodeErrors(); got != tt.wantNodeErrors {
t.Errorf("QuorumCallError.NodeErrors() = %d, want %d", got, tt.wantNodeErrors)
}
})
}
}
func TestQuorumCallErrorUnwrap(t *testing.T) {
unavailableErr := status.Error(codes.Unavailable, "node down")
timeoutErr := status.Error(codes.DeadlineExceeded, "timeout")
connectionErr := errors.New("connection refused")
qcErr := QuorumCallError{
cause: ErrIncomplete,
errors: []nodeError{
{nodeID: 1, cause: unavailableErr},
{nodeID: 3, cause: timeoutErr},
{nodeID: 5, cause: connectionErr},
},
}
// Test Unwrap returns all node error causes
unwrapped := qcErr.Unwrap()
if len(unwrapped) != 3 {
t.Fatalf("Unwrap() returned %d errors, want 3", len(unwrapped))
}
// Verify the unwrapped errors are the node error causes
if unwrapped[0] != unavailableErr {
t.Errorf("Unwrap()[0] = %v, want %v", unwrapped[0], unavailableErr)
}
if unwrapped[1] != timeoutErr {
t.Errorf("Unwrap()[1] = %v, want %v", unwrapped[1], timeoutErr)
}
if unwrapped[2] != connectionErr {
t.Errorf("Unwrap()[2] = %v, want %v", unwrapped[2], connectionErr)
}
// Test errors.Is with the cause (handled by Is() method)
if !errors.Is(qcErr, ErrIncomplete) {
t.Error("errors.Is(qcErr, ErrIncomplete) = false, want true")
}
// Test errors.Is with wrapped node errors (handled by Unwrap() method)
if !errors.Is(qcErr, unavailableErr) {
t.Error("errors.Is(qcErr, unavailableErr) = false, want true")
}
if !errors.Is(qcErr, timeoutErr) {
t.Error("errors.Is(qcErr, timeoutErr) = false, want true")
}
if !errors.Is(qcErr, connectionErr) {
t.Error("errors.Is(qcErr, connectionErr) = false, want true")
}
// Test errors.Is with unrelated error
if errors.Is(qcErr, ErrSendFailure) {
t.Error("errors.Is(qcErr, ErrSendFailure) = true, want false")
}
}
// customError is a custom error type for testing errors.As
type customError struct {
msg string
}
func (e customError) Error() string { return e.msg }
func TestQuorumCallErrorUnwrapWithAs(t *testing.T) {
customErr := customError{msg: "custom node error"}
qcErr := QuorumCallError{
cause: ErrIncomplete,
errors: []nodeError{
{nodeID: 1, cause: customErr},
{nodeID: 2, cause: status.Error(codes.Unavailable, "down")},
},
}
// Test errors.As can find the custom error in wrapped errors
var target customError
if !errors.As(qcErr, &target) {
t.Fatal("errors.As(qcErr, &customError) = false, want true")
}
if target.msg != "custom node error" {
t.Errorf("extracted customError.msg = %q, want %q", target.msg, "custom node error")
}
}