-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathingress_error_test.go
More file actions
326 lines (269 loc) · 7.49 KB
/
Copy pathingress_error_test.go
File metadata and controls
326 lines (269 loc) · 7.49 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package serverconn
import (
"context"
"errors"
"fmt"
"testing"
"github.com/lightninglabs/wavelength/baselib/actor"
mailboxconn "github.com/lightninglabs/wavelength/mailbox/conn"
mailboxpb "github.com/lightninglabs/wavelength/mailbox/pb"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// mailboxClientStub is a configurable MailboxServiceClient test double.
type mailboxClientStub struct {
sendFn func(
ctx context.Context, in *mailboxpb.SendRequest,
opts ...grpc.CallOption,
) (*mailboxpb.SendResponse, error)
pullFn func(
ctx context.Context, in *mailboxpb.PullRequest,
opts ...grpc.CallOption,
) (*mailboxpb.PullResponse, error)
ackFn func(
ctx context.Context, in *mailboxpb.AckUpToRequest,
opts ...grpc.CallOption,
) (*mailboxpb.AckUpToResponse, error)
}
// Send executes the configured send function.
func (s *mailboxClientStub) Send(ctx context.Context, in *mailboxpb.SendRequest,
opts ...grpc.CallOption) (*mailboxpb.SendResponse, error) {
if s.sendFn != nil {
return s.sendFn(ctx, in, opts...)
}
return &mailboxpb.SendResponse{
Status: &mailboxpb.Status{
Ok: true,
},
}, nil
}
// Pull executes the configured pull function.
func (s *mailboxClientStub) Pull(ctx context.Context, in *mailboxpb.PullRequest,
opts ...grpc.CallOption) (*mailboxpb.PullResponse, error) {
if s.pullFn != nil {
return s.pullFn(ctx, in, opts...)
}
return &mailboxpb.PullResponse{
Status: &mailboxpb.Status{
Ok: true,
},
}, nil
}
// AckUpTo executes the configured ack function.
func (s *mailboxClientStub) AckUpTo(ctx context.Context,
in *mailboxpb.AckUpToRequest, opts ...grpc.CallOption) (
*mailboxpb.AckUpToResponse, error) {
if s.ackFn != nil {
return s.ackFn(ctx, in, opts...)
}
return &mailboxpb.AckUpToResponse{
Status: &mailboxpb.Status{
Ok: true,
},
}, nil
}
// checkpointLoadStore allows overriding LoadCheckpoint behavior for tests.
type checkpointLoadStore struct {
*memCheckpointStore
loadErr error
loadCheckpoint *actor.Checkpoint
}
// LoadCheckpoint returns an injected error/checkpoint when configured.
func (s *checkpointLoadStore) LoadCheckpoint(ctx context.Context,
actorID string) (*actor.Checkpoint, error) {
if s.loadErr != nil {
return nil, s.loadErr
}
if s.loadCheckpoint != nil {
return s.loadCheckpoint, nil
}
return s.memCheckpointStore.LoadCheckpoint(ctx, actorID)
}
// checkpointSaveStore allows overriding SaveCheckpoint behavior for tests.
type checkpointSaveStore struct {
*memCheckpointStore
saveErr error
}
// SaveCheckpoint returns an injected error when configured.
func (s *checkpointSaveStore) SaveCheckpoint(
ctx context.Context, params actor.CheckpointParams,
) error {
if s.saveErr != nil {
return s.saveErr
}
return s.memCheckpointStore.SaveCheckpoint(ctx, params)
}
// newErrorPathActor builds a connector actor with defaults and test overrides.
func newErrorPathActor(
edge mailboxpb.MailboxServiceClient,
store actor.DeliveryStore,
) *ServerConnectionActor {
cfg := DefaultConnectorConfig()
cfg.Edge = edge
cfg.Store = store
cfg.LocalMailboxID = "client-1"
cfg.RemoteMailboxID = "server-1"
cfg.ArkProtocolVersion = 1
return NewServerConnectionActor(cfg)
}
// TestPullBatch_StatusFailure verifies pullBatch wraps non-OK status responses.
func TestPullBatch_StatusFailure(t *testing.T) {
t.Parallel()
edge := &mailboxClientStub{
pullFn: func(ctx context.Context, in *mailboxpb.PullRequest,
opts ...grpc.CallOption) (*mailboxpb.PullResponse,
error) {
return &mailboxpb.PullResponse{
Status: &mailboxpb.Status{
Ok: false,
Code: "TEMPORARY",
Message: "pull failed",
},
}, nil
},
}
actor := newErrorPathActor(edge, newMemCheckpointStore())
_, _, err := actor.pullBatch(t.Context(), 0)
require.Error(t, err)
var stErr *mailboxconn.StatusError
require.ErrorAs(t, err, &stErr)
require.Equal(t, "Pull", stErr.Op)
require.Contains(t, stErr.Error(), "TEMPORARY")
}
// TestAckRemote_StatusFailure verifies ackRemote wraps non-OK status responses.
func TestAckRemote_StatusFailure(t *testing.T) {
t.Parallel()
edge := &mailboxClientStub{
ackFn: func(ctx context.Context, in *mailboxpb.AckUpToRequest,
opts ...grpc.CallOption) (*mailboxpb.AckUpToResponse,
error) {
return &mailboxpb.AckUpToResponse{
Status: &mailboxpb.Status{
Ok: false,
Code: "INTERNAL",
Message: "ack failed",
},
}, nil
},
}
actor := newErrorPathActor(edge, newMemCheckpointStore())
err := actor.ackRemote(t.Context(), 1)
require.Error(t, err)
var stErr *mailboxconn.StatusError
require.ErrorAs(t, err, &stErr)
require.Equal(t, "AckUpTo", stErr.Op)
require.Contains(t, stErr.Error(), "ack failed")
}
// TestIsIngressShutdownErr verifies expected shutdown cancellation is
// distinguished from retryable ingress failures.
func TestIsIngressShutdownErr(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cancel bool
err error
want bool
}{
{
name: "nil error",
err: nil,
want: false,
},
{
name: "local context canceled",
cancel: true,
err: context.Canceled,
want: true,
},
{
name: "grpc canceled",
err: status.Error(codes.Canceled, "context canceled"),
want: false,
},
{
name: "ctx canceled after transport error",
cancel: true,
err: errors.New("transport closed"),
want: true,
},
{
name: "retryable transport error",
err: errors.New("temporary transport failure"),
want: false,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
if test.cancel {
cancel()
}
require.Equal(
t, test.want,
isIngressShutdownErr(ctx, test.err),
)
})
}
}
// TestLoadCheckpoint_Errors verifies loadCheckpoint surfaces store/decode
// failures.
func TestLoadCheckpoint_Errors(t *testing.T) {
t.Parallel()
edge := &mailboxClientStub{}
loadErrActor := newErrorPathActor(
edge, &checkpointLoadStore{
memCheckpointStore: newMemCheckpointStore(),
loadErr: fmt.Errorf("load failed"),
},
)
_, err := loadErrActor.loadCheckpoint(t.Context())
require.ErrorContains(t, err, "load failed")
decodeErrActor := newErrorPathActor(
edge, &checkpointLoadStore{
memCheckpointStore: newMemCheckpointStore(),
loadCheckpoint: &actor.Checkpoint{
ActorID: "serverconn-client-1",
StateType: ackStateType,
StateData: []byte{0xff, 0x00, 0x01},
},
},
)
_, err = decodeErrActor.loadCheckpoint(t.Context())
require.Error(t, err)
}
// TestSaveCheckpoint_Error verifies saveCheckpoint surfaces store save errors.
func TestSaveCheckpoint_Error(t *testing.T) {
t.Parallel()
actor := newErrorPathActor(
&mailboxClientStub{},
&checkpointSaveStore{
memCheckpointStore: newMemCheckpointStore(),
saveErr: fmt.Errorf("save failed"),
},
)
err := actor.saveCheckpoint(t.Context(), AckState{})
require.ErrorContains(t, err, "save failed")
}
// TestStatusError_ErrorString verifies statusError string formatting paths.
func TestStatusError_ErrorString(t *testing.T) {
t.Parallel()
errNil := (&mailboxconn.StatusError{
Op: "AckUpTo",
}).Error()
require.Contains(t, errNil, "nil status")
errStatus := (&mailboxconn.StatusError{
Op: "Pull",
Status: &mailboxpb.Status{
Ok: false,
Code: "EIO",
Message: "io failure",
},
}).Error()
require.Contains(t, errStatus, "io failure")
require.Contains(t, errStatus, "EIO")
}