|
| 1 | +package backoff |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDelay(t *testing.T) { |
| 13 | + for _, tt := range []struct { |
| 14 | + name string |
| 15 | + act time.Duration |
| 16 | + exp time.Duration |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: xtest.CurrentFileLine(), |
| 20 | + act: Delay(TypeNoBackoff, 0), |
| 21 | + exp: 0, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: xtest.CurrentFileLine(), |
| 25 | + act: Delay(TypeNoBackoff, 1), |
| 26 | + exp: 0, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: xtest.CurrentFileLine(), |
| 30 | + act: Delay(TypeNoBackoff, 2), |
| 31 | + exp: 0, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: xtest.CurrentFileLine(), |
| 35 | + act: Delay(TypeFast, 0, WithFastBackoff(New( |
| 36 | + WithSlotDuration(fastSlot), |
| 37 | + WithCeiling(6), |
| 38 | + WithJitterLimit(1), |
| 39 | + ))), |
| 40 | + exp: 5 * time.Millisecond, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: xtest.CurrentFileLine(), |
| 44 | + act: Delay(TypeFast, 1, WithFastBackoff(New( |
| 45 | + WithSlotDuration(fastSlot), |
| 46 | + WithCeiling(6), |
| 47 | + WithJitterLimit(1), |
| 48 | + ))), |
| 49 | + exp: 10 * time.Millisecond, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: xtest.CurrentFileLine(), |
| 53 | + act: Delay(TypeFast, 3, WithFastBackoff(New( |
| 54 | + WithSlotDuration(fastSlot), |
| 55 | + WithCeiling(6), |
| 56 | + WithJitterLimit(1), |
| 57 | + ))), |
| 58 | + exp: 40 * time.Millisecond, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: xtest.CurrentFileLine(), |
| 62 | + act: Delay(TypeSlow, 0, WithSlowBackoff(New( |
| 63 | + WithSlotDuration(slowSlot), |
| 64 | + WithCeiling(6), |
| 65 | + WithJitterLimit(1), |
| 66 | + ))), |
| 67 | + exp: time.Second, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: xtest.CurrentFileLine(), |
| 71 | + act: Delay(TypeSlow, 1, WithSlowBackoff(New( |
| 72 | + WithSlotDuration(slowSlot), |
| 73 | + WithCeiling(6), |
| 74 | + WithJitterLimit(1), |
| 75 | + ))), |
| 76 | + exp: 2 * time.Second, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: xtest.CurrentFileLine(), |
| 80 | + act: Delay(TypeSlow, 3, WithSlowBackoff(New( |
| 81 | + WithSlotDuration(slowSlot), |
| 82 | + WithCeiling(6), |
| 83 | + WithJitterLimit(1), |
| 84 | + ))), |
| 85 | + exp: 8 * time.Second, |
| 86 | + }, |
| 87 | + } { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + require.Equal(t, tt.exp, tt.act) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments