-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmulti_error_test.go
More file actions
147 lines (122 loc) · 3.15 KB
/
multi_error_test.go
File metadata and controls
147 lines (122 loc) · 3.15 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
// Copyright (c) 2026 Onur Cinar.
// The source code is provided under MIT License.
// https://github.com/cinar/resile
package resile
import (
"context"
"errors"
"fmt"
"testing"
"time"
)
func TestMultiErrorAggregation_Retry(t *testing.T) {
t.Parallel()
ctx := context.Background()
err1 := errors.New("error 1")
err2 := errors.New("error 2")
err3 := errors.New("error 3")
var count int
err := DoErr(ctx, func(ctx context.Context) error {
count++
switch count {
case 1:
return err1
case 2:
return err2
case 3:
return err3
default:
return nil
}
}, WithMaxAttempts(3), WithBaseDelay(0))
if err == nil {
t.Fatal("expected error, got nil")
}
// Verify it contains all errors
if !errors.Is(err, err1) {
t.Errorf("expected error to wrap %v", err1)
}
if !errors.Is(err, err2) {
t.Errorf("expected error to wrap %v", err2)
}
if !errors.Is(err, err3) {
t.Errorf("expected error to wrap %v", err3)
}
// Verify Unwrap() []error support
type multiError interface {
Unwrap() []error
}
if me, ok := err.(multiError); ok {
unwrapped := me.Unwrap()
if len(unwrapped) != 3 {
t.Errorf("expected 3 unwrapped errors, got %d", len(unwrapped))
}
} else {
t.Error("error does not implement Unwrap() []error")
}
expectedStr := fmt.Sprintf("%v\n%v\n%v", err1, err2, err3)
if err.Error() != expectedStr {
t.Errorf("expected error string %q, got %q", expectedStr, err.Error())
}
}
func TestMultiErrorAggregation_Hedged(t *testing.T) {
t.Parallel()
ctx := context.Background()
err1 := errors.New("error 1")
err2 := errors.New("error 2")
err := DoErrStateHedged(ctx, func(ctx context.Context, state RetryState) error {
// In hedging, multiple attempts run concurrently.
if state.Attempt == 0 {
return err1
}
return err2
}, WithMaxAttempts(2), WithHedgingDelay(0))
if err == nil {
t.Fatal("expected error, got nil")
}
if !errors.Is(err, err1) {
t.Errorf("expected error to wrap %v", err1)
}
if !errors.Is(err, err2) {
t.Errorf("expected error to wrap %v", err2)
}
}
func TestMultiErrorAggregation_Mixed(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
err1 := errors.New("error 1")
err := DoErr(ctx, func(innerCtx context.Context) error {
cancel() // Cancel context during the first attempt
return err1
}, WithMaxAttempts(3), WithBaseDelay(10*time.Millisecond))
if err == nil {
t.Fatal("expected error, got nil")
}
if !errors.Is(err, err1) {
t.Errorf("expected error to wrap %v", err1)
}
if !errors.Is(err, context.Canceled) {
t.Errorf("expected error to wrap context.Canceled, got %v", err)
}
}
func TestMultiErrorAggregation_Hedged_Stateful(t *testing.T) {
t.Parallel()
ctx := context.Background()
err1 := errors.New("error 1")
err2 := errors.New("error 2")
err := DoErrStateHedged(ctx, func(ctx context.Context, state RetryState) error {
if state.Attempt == 0 {
return err1
}
return err2
}, WithMaxAttempts(2), WithHedgingDelay(0))
if err == nil {
t.Fatal("expected error, got nil")
}
if !errors.Is(err, err1) {
t.Errorf("expected error to wrap %v", err1)
}
if !errors.Is(err, err2) {
t.Errorf("expected error to wrap %v", err2)
}
}