-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathadaptive_retry_integration_test.go
More file actions
66 lines (54 loc) · 1.77 KB
/
Copy pathadaptive_retry_integration_test.go
File metadata and controls
66 lines (54 loc) · 1.77 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
package resile
import (
"context"
"errors"
"testing"
"time"
)
func TestRetryLoop_AdaptiveBucket_FailFast(t *testing.T) {
t.Parallel()
// Given a bucket with very limited capacity (e.g. max 5, cost 5, refill 1)
// Initial state: 5 tokens.
bucket := NewAdaptiveBucket(5, 5, 1)
attempts := 0
action := func(ctx context.Context, state RetryState) error {
attempts++
return errors.New("simulated transient error")
}
// First execution (attempt 0 does not cost a token, but attempt 1 costs 5).
// tokens: 5. First retry will consume 5 tokens -> 0.
// Second retry will fail to acquire -> fail fast.
err := DoErrState(context.Background(), action, WithMaxAttempts(5), WithBaseDelay(time.Millisecond), WithAdaptiveBucket(bucket))
if err == nil {
t.Fatal("expected error, got nil")
}
// Expecting only 2 attempts:
// Attempt 0 (fails) -> acquires token -> 0 tokens left.
// Attempt 1 (fails) -> tries to acquire token -> fails -> terminates.
if attempts != 2 {
t.Fatalf("expected 2 attempts due to adaptive bucket fail-fast, got %d", attempts)
}
}
func TestRetryLoop_AdaptiveBucket_SuccessRefill(t *testing.T) {
t.Parallel()
bucket := NewAdaptiveBucket(10, 5, 5)
// First execution succeeds on the 2nd attempt.
// Attempt 0: fails.
// Attempt 1: acquire (10 -> 5). succeeds -> refill (5 -> 10).
attempts1 := 0
action1 := func(ctx context.Context, state RetryState) error {
attempts1++
if attempts1 < 2 {
return errors.New("error")
}
return nil
}
err := DoErrState(context.Background(), action1, WithMaxAttempts(5), WithBaseDelay(time.Millisecond), WithAdaptiveBucket(bucket))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Bucket should have 10 tokens again.
if bucket.tokens != 10 {
t.Fatalf("expected 10 tokens, got %v", bucket.tokens)
}
}