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
|
package ingest
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)
func TestFetchExportLimitedRetries429ThenSucceeds(t *testing.T) {
var attempts atomic.Int32
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
n := attempts.Add(1)
if n <= 2 {
w.Header().Set("Retry-After", "0")
http.Error(w, "rate limited", http.StatusTooManyRequests)
return
}
_, _ = fmt.Fprintln(w, `{"seq":1,"did":"did:plc:alice","cid":"cid1","operation":{"x":1}}`)
}))
defer ts.Close()
client := NewClient(ts.URL, ClientOptions{
MaxAttempts: 5,
BaseDelay: time.Millisecond,
MaxDelay: 2 * time.Millisecond,
})
records, err := client.FetchExportLimited(context.Background(), 0, 0)
if err != nil {
t.Fatalf("fetch export: %v", err)
}
if len(records) != 1 {
t.Fatalf("record count mismatch: got %d want 1", len(records))
}
if got := attempts.Load(); got != 3 {
t.Fatalf("attempt count mismatch: got %d want 3", got)
}
}
func TestFetchExportLimitedDoesNotRetry400(t *testing.T) {
var attempts atomic.Int32
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attempts.Add(1)
http.Error(w, "bad request", http.StatusBadRequest)
}))
defer ts.Close()
client := NewClient(ts.URL, ClientOptions{
MaxAttempts: 5,
BaseDelay: time.Millisecond,
MaxDelay: 2 * time.Millisecond,
})
_, err := client.FetchExportLimited(context.Background(), 0, 0)
if err == nil {
t.Fatalf("expected error for 400 response")
}
if got := attempts.Load(); got != 1 {
t.Fatalf("unexpected retries on 400: got attempts=%d want 1", got)
}
}
|