Debian Codegraph @

exporters/otlp/otlptrace/otlptracehttp/internal/retry/retry_test.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/retry/retry_test.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package retry
8
9 import (
10 "context"
11 "errors"
12 "math"
13 "sync"
14 "testing"
15 "time"
16
17 "github.com/cenkalti/backoff/v5"
18 "github.com/stretchr/testify/assert"
19 )
20
21 func TestWait(t *testing.T) {
22 tests := []struct {
23 ctx context.Context
24 delay time.Duration
25 expected error
26 }{
27 {
28 ctx: t.Context(),
29 delay: time.Duration(0),
30 },
31 {
32 ctx: t.Context(),
33 delay: time.Duration(1),
34 },
35 {
36 ctx: t.Context(),
37 delay: time.Duration(-1),
38 },
39 {
40 ctx: func() context.Context {
41 ctx, cancel := context.WithCancel(t.Context())
42 cancel()
43 return ctx
44 }(),
45 // Ensure the timer and context do not end simultaneously.
46 delay: 1 * time.Hour,
47 expected: context.Canceled,
48 },
49 }
50
51 for _, test := range tests {
52 err := wait(test.ctx, test.delay)
53 if test.expected == nil {
54 assert.NoError(t, err)
55 } else {
56 assert.ErrorIs(t, err, test.expected)
57 }
58 }
59 }
60
61 func TestNonRetryableError(t *testing.T) {
62 ev := func(error) (bool, time.Duration) { return false, 0 }
63
64 reqFunc := Config{
65 Enabled: true,
66 InitialInterval: 1 * time.Nanosecond,
67 MaxInterval: 1 * time.Nanosecond,
68 // Never stop retrying.
69 MaxElapsedTime: 0,
70 }.RequestFunc(ev)
71 ctx := t.Context()
72 assert.NoError(t, reqFunc(ctx, func(context.Context) error {
73 return nil
74 }))
75 assert.ErrorIs(t, reqFunc(ctx, func(context.Context) error {
76 return assert.AnError
77 }), assert.AnError)
78 }
79
80 func TestThrottledRetry(t *testing.T) {
81 // Ensure the throttle delay is used by making longer than backoff delay.
82 throttleDelay, backoffDelay := time.Second, time.Nanosecond
83
84 ev := func(error) (bool, time.Duration) {
85 // Retry everything with a throttle delay.
86 return true, throttleDelay
87 }
88
89 reqFunc := Config{
90 Enabled: true,
91 InitialInterval: backoffDelay,
92 MaxInterval: backoffDelay,
93 // Never stop retrying.
94 MaxElapsedTime: 0,
95 }.RequestFunc(ev)
96
97 origWait := waitFunc
98 var done bool
99 waitFunc = func(_ context.Context, delay time.Duration) error {
100 assert.Equal(t, throttleDelay, delay, "retry not throttled")
101 // Try twice to ensure call is attempted again after delay.
102 if done {
103 return assert.AnError
104 }
105 done = true
106 return nil
107 }
108 defer func() { waitFunc = origWait }()
109
110 ctx := t.Context()
111 assert.ErrorIs(t, reqFunc(ctx, func(context.Context) error {
112 return errors.New("not this error")
113 }), assert.AnError)
114 }
115
116 func TestBackoffRetry(t *testing.T) {
117 ev := func(error) (bool, time.Duration) { return true, 0 }
118
119 delay := time.Nanosecond
120 reqFunc := Config{
121 Enabled: true,
122 InitialInterval: delay,
123 MaxInterval: delay,
124 // Never stop retrying.
125 MaxElapsedTime: 0,
126 }.RequestFunc(ev)
127
128 origWait := waitFunc
129 var done bool
130 waitFunc = func(_ context.Context, d time.Duration) error {
131 delta := math.Ceil(float64(delay) * backoff.DefaultRandomizationFactor)
132 assert.InDelta(t, delay, d, delta, "retry not backoffed")
133 // Try twice to ensure call is attempted again after delay.
134 if done {
135 return assert.AnError
136 }
137 done = true
138 return nil
139 }
140 t.Cleanup(func() { waitFunc = origWait })
141
142 ctx := t.Context()
143 assert.ErrorIs(t, reqFunc(ctx, func(context.Context) error {
144 return errors.New("not this error")
145 }), assert.AnError)
146 }
147
148 func TestBackoffRetryCanceledContext(t *testing.T) {
149 ev := func(error) (bool, time.Duration) { return true, 0 }
150
151 delay := time.Millisecond
152 reqFunc := Config{
153 Enabled: true,
154 InitialInterval: delay,
155 MaxInterval: delay,
156 // Never stop retrying.
157 MaxElapsedTime: 10 * time.Millisecond,
158 }.RequestFunc(ev)
159
160 ctx, cancel := context.WithCancel(t.Context())
161 count := 0
162 cancel()
163 err := reqFunc(ctx, func(context.Context) error {
164 count++
165 return assert.AnError
166 })
167
168 assert.ErrorIs(t, err, context.Canceled)
169 assert.Contains(t, err.Error(), assert.AnError.Error())
170 assert.Equal(t, 1, count)
171 }
172
173 func TestThrottledRetryGreaterThanMaxElapsedTime(t *testing.T) {
174 // Ensure the throttle delay is used by making longer than backoff delay.
175 tDelay, bDelay := time.Hour, time.Nanosecond
176 ev := func(error) (bool, time.Duration) { return true, tDelay }
177 reqFunc := Config{
178 Enabled: true,
179 InitialInterval: bDelay,
180 MaxInterval: bDelay,
181 MaxElapsedTime: tDelay - (time.Nanosecond),
182 }.RequestFunc(ev)
183
184 ctx := t.Context()
185 assert.Contains(t, reqFunc(ctx, func(context.Context) error {
186 return assert.AnError
187 }).Error(), "max retry time would elapse: ")
188 }
189
190 func TestMaxElapsedTime(t *testing.T) {
191 ev := func(error) (bool, time.Duration) { return true, 0 }
192 delay := time.Nanosecond
193 reqFunc := Config{
194 Enabled: true,
195 // InitialInterval > MaxElapsedTime means immediate return.
196 InitialInterval: 2 * delay,
197 MaxElapsedTime: delay,
198 }.RequestFunc(ev)
199
200 ctx := t.Context()
201 assert.Contains(t, reqFunc(ctx, func(context.Context) error {
202 return assert.AnError
203 }).Error(), "max retry time")
204 }
205
206 func TestRetryNotEnabled(t *testing.T) {
207 ev := func(error) (bool, time.Duration) {
208 t.Error("evaluated retry when not enabled")
209 return false, 0
210 }
211
212 reqFunc := Config{}.RequestFunc(ev)
213 ctx := t.Context()
214 assert.NoError(t, reqFunc(ctx, func(context.Context) error {
215 return nil
216 }))
217 assert.ErrorIs(t, reqFunc(ctx, func(context.Context) error {
218 return assert.AnError
219 }), assert.AnError)
220 }
221
222 func TestRetryConcurrentSafe(t *testing.T) {
223 ev := func(error) (bool, time.Duration) { return true, 0 }
224 reqFunc := Config{
225 Enabled: true,
226 }.RequestFunc(ev)
227
228 var wg sync.WaitGroup
229 ctx := t.Context()
230
231 for i := 1; i < 5; i++ {
232 wg.Add(1)
233
234 go func() {
235 defer wg.Done()
236
237 var done bool
238 assert.NoError(t, reqFunc(ctx, func(context.Context) error {
239 if !done {
240 done = true
241 return assert.AnError
242 }
243
244 return nil
245 }))
246 }()
247 }
248
249 wg.Wait()
250 }