forked from charmbracelet/fantasy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathretry_test.go
More file actions
145 lines (124 loc) · 3.41 KB
/
Copy pathretry_test.go
File metadata and controls
145 lines (124 loc) · 3.41 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
package fantasy
import (
"context"
"errors"
"net"
"testing"
"time"
)
func TestIsRetryableError(t *testing.T) {
t.Parallel()
t.Run("retryable ProviderError", func(t *testing.T) {
t.Parallel()
err := &ProviderError{
StatusCode: 429,
Message: "rate limited",
}
if !isRetryableError(err) {
t.Error("expected retryable ProviderError to be retryable")
}
})
t.Run("non-retryable ProviderError", func(t *testing.T) {
t.Parallel()
err := &ProviderError{
StatusCode: 400,
Message: "bad request",
}
if isRetryableError(err) {
t.Error("expected non-retryable ProviderError to not be retryable")
}
})
t.Run("net.OpError is retryable", func(t *testing.T) {
t.Parallel()
err := &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("connection refused")}
if !isRetryableError(err) {
t.Error("expected net.OpError to be retryable")
}
})
t.Run("context.Canceled is not retryable", func(t *testing.T) {
t.Parallel()
err := context.Canceled
if isRetryableError(err) {
t.Error("expected context.Canceled to not be retryable")
}
})
t.Run("context.DeadlineExceeded is not retryable", func(t *testing.T) {
t.Parallel()
err := context.DeadlineExceeded
if isRetryableError(err) {
t.Error("expected context.DeadlineExceeded to not be retryable")
}
})
t.Run("generic error is not retryable", func(t *testing.T) {
t.Parallel()
err := errors.New("something went wrong")
if isRetryableError(err) {
t.Error("expected generic error to not be retryable")
}
})
}
func TestRetryWithExponentialBackoff_ConnectionErrors(t *testing.T) {
t.Parallel()
t.Run("retries on net.OpError", func(t *testing.T) {
t.Parallel()
attempts := 0
opts := RetryOptions{
MaxRetries: 2,
InitialDelayIn: 1 * time.Millisecond,
BackoffFactor: 2.0,
}
retryFn := RetryWithExponentialBackoffRespectingRetryHeaders[int](opts)
result, err := retryFn(context.Background(), func() (int, error) {
attempts++
if attempts < 3 {
return 0, &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("connection refused")}
}
return 42, nil
})
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result != 42 {
t.Fatalf("expected result 42, got %d", result)
}
if attempts != 3 {
t.Fatalf("expected 3 attempts, got %d", attempts)
}
})
t.Run("does not retry on context.Canceled", func(t *testing.T) {
t.Parallel()
attempts := 0
opts := RetryOptions{
MaxRetries: 2,
InitialDelayIn: 1 * time.Millisecond,
BackoffFactor: 2.0,
}
retryFn := RetryWithExponentialBackoffRespectingRetryHeaders[int](opts)
_, err := retryFn(context.Background(), func() (int, error) {
attempts++
return 0, context.Canceled
})
if err == nil {
t.Fatal("expected error, got nil")
}
if attempts != 1 {
t.Fatalf("expected 1 attempt (no retry), got %d", attempts)
}
})
t.Run("returns RetryError when max retries exceeded on connection error", func(t *testing.T) {
t.Parallel()
opts := RetryOptions{
MaxRetries: 2,
InitialDelayIn: 1 * time.Millisecond,
BackoffFactor: 2.0,
}
retryFn := RetryWithExponentialBackoffRespectingRetryHeaders[int](opts)
_, err := retryFn(context.Background(), func() (int, error) {
return 0, &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("connection refused")}
})
var retryErr *RetryError
if !errors.As(err, &retryErr) {
t.Fatalf("expected RetryError, got %T: %v", err, err)
}
})
}