internal/check/retry/backoff_test.go

package retry

import (
	"testing"
	"time"
)

func TestBackoff_Delay(t *testing.T) {
	b := New(4, 10*time.Millisecond, 100*time.Millisecond)
	cases := []struct {
		attempt int
		want    time.Duration
	}{
		{0, 0},
		{1, 10 * time.Millisecond},
		{2, 20 * time.Millisecond},
		{3, 40 * time.Millisecond},
		{4, 80 * time.Millisecond},
		{5, 100 * time.Millisecond},
		{20, 100 * time.Millisecond},
	}
	for _, tc := range cases {
		if got := b.Delay(tc.attempt); got != tc.want {
			t.Errorf("Delay(%d) = %v, want %v", tc.attempt, got, tc.want)
		}
	}
}

func TestBackoff_Max(t *testing.T) {
	if New(-5, time.Second, time.Second).Max() != 0 {
		t.Errorf("negative max should clamp to 0")
	}
	if New(3, time.Second, time.Second).Max() != 3 {
		t.Errorf("max 3 lost")
	}
}

func TestBackoff_zeroBase(t *testing.T) {
	b := New(3, 0, time.Second)
	if b.Delay(2) != 0 {
		t.Errorf("zero base should give zero delay")
	}
}