-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretry_unit_test.go
More file actions
42 lines (39 loc) · 942 Bytes
/
retry_unit_test.go
File metadata and controls
42 lines (39 loc) · 942 Bytes
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
package norm
import (
"context"
"errors"
"testing"
"time"
)
// Test withRetry attempts logic without requiring DB
func TestWithRetry_SucceedsOnLaterAttempt(t *testing.T) {
kn := &KintsNorm{config: &Config{RetryAttempts: 3, RetryBackoff: 10 * time.Millisecond}}
attempts := 0
err := kn.withRetry(context.Background(), func() error {
attempts++
if attempts < 3 {
return errors.New("transient")
}
return nil
})
if err != nil {
t.Fatalf("expected success, got %v", err)
}
if attempts != 3 {
t.Fatalf("expected 3 attempts, got %d", attempts)
}
}
func TestWithRetry_NoRetryWhenAttemptsZero(t *testing.T) {
kn := &KintsNorm{config: &Config{RetryAttempts: 0}}
attempts := 0
err := kn.withRetry(context.Background(), func() error {
attempts++
return errors.New("fail")
})
if err == nil {
t.Fatalf("expected error")
}
if attempts != 1 {
t.Fatalf("expected single call when no retry, got %d", attempts)
}
}