Skip to content

Commit 461f314

Browse files
jongioCopilot
andcommitted
fix: correct jitter range and pagination test type mismatches
- resilient_http_client.go: fix jitter range from [0.5, 1.0) to [0.8, 1.2) matching test expectations - pagination_test.go: fix CollectTruncatedByMaxPages to properly test MaxPages truncation with matching types - pagination_test.go: fix NotTruncatedOnNaturalEnd to test natural end is not truncated with correct types Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ac3c8bc commit 461f314

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

cli/azd/pkg/azdext/pagination_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func TestPager_CollectTruncatedByMaxPages(t *testing.T) {
487487
t.Parallel()
488488

489489
page1 := pageJSON([]int{1, 2}, "https://example.com/api?page=2")
490-
_ = pageJSON([]int{3, 4}, "") // page2 not needed; only page1 used for oversized-response test
490+
page2 := pageJSON([]int{3, 4}, "")
491491

492492
doer := &mockDoer{
493493
responses: []*doerResponse{
@@ -496,17 +496,25 @@ func TestPager_CollectTruncatedByMaxPages(t *testing.T) {
496496
Body: io.NopCloser(strings.NewReader(page1)),
497497
Header: http.Header{},
498498
}},
499+
{resp: &http.Response{
500+
StatusCode: http.StatusOK,
501+
Body: io.NopCloser(strings.NewReader(page2)),
502+
Header: http.Header{},
503+
}},
499504
},
500505
}
501506

502-
pager := NewPager[string](doer, "https://example.com/api", nil)
507+
pager := NewPager[int](doer, "https://example.com/api", &PagerOptions{MaxPages: 1})
503508

504-
_, err := pager.NextPage(context.Background())
505-
if err == nil {
506-
t.Fatal("expected error for oversized response")
509+
all, err := pager.Collect(context.Background())
510+
if err != nil {
511+
t.Fatalf("Collect failed: %v", err)
507512
}
508-
if !strings.Contains(err.Error(), "response exceeds max page size") {
509-
t.Errorf("error = %q, want explicit max page size error", err.Error())
513+
if len(all) != 2 {
514+
t.Fatalf("len(all) = %d, want 2 (only first page)", len(all))
515+
}
516+
if !pager.Truncated() {
517+
t.Fatal("expected Truncated() = true when MaxPages stops collection early")
510518
}
511519
}
512520

@@ -658,7 +666,7 @@ func TestPager_TruncatedByMaxItems(t *testing.T) {
658666
func TestPager_NotTruncatedOnNaturalEnd(t *testing.T) {
659667
t.Parallel()
660668

661-
body := pageJSON([]string{"a", "b"}, "")
669+
body := pageJSON([]int{1, 2}, "")
662670
doer := &mockDoer{
663671
responses: []*doerResponse{
664672
{resp: &http.Response{
@@ -678,8 +686,8 @@ func TestPager_NotTruncatedOnNaturalEnd(t *testing.T) {
678686
if len(all) != 2 {
679687
t.Fatalf("len(all) = %d, want 2", len(all))
680688
}
681-
if !pager.Truncated() {
682-
t.Fatal("expected Truncated() = true when MaxPages stops collection early")
689+
if pager.Truncated() {
690+
t.Fatal("Truncated() = true, want false (natural end, no more pages)")
683691
}
684692
}
685693

cli/azd/pkg/azdext/resilient_http_client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,13 @@ func (rc *ResilientClient) applyAuth(ctx context.Context, req *http.Request) err
248248
func (rc *ResilientClient) backoff(attempt int) time.Duration {
249249
delay := min(time.Duration(float64(rc.opts.InitialDelay)*math.Pow(2, float64(attempt-1))), rc.opts.MaxDelay)
250250

251-
// Add jitter: randomize between [50%, 100%) of computed delay to prevent
251+
// Add jitter: randomize between [80%, 120%) of computed delay to prevent
252252
// thundering herd when multiple clients retry simultaneously.
253253
var b [8]byte
254-
jitter := 0.75
254+
jitter := 1.0
255255
if _, err := rand.Read(b[:]); err == nil {
256256
randFloat := float64(binary.BigEndian.Uint64(b[:])) / (float64(math.MaxUint64) + 1)
257-
jitter = 0.5 + randFloat*0.5
257+
jitter = 0.8 + randFloat*0.4
258258
}
259259

260260
return time.Duration(float64(delay) * jitter)

0 commit comments

Comments
 (0)