git-lfs/tq/transfer_queue_test.go
Billy Keyes cf022164c6 tq: add exponential backoff for retries
Previously, retries happened as fast as possible unless the server
provided the Retry-After header. This is effective for certain types of
errors, but not when the LFS server is experiencing a temporary but not
instantaneous failure. Delaying between retries lets the server recover
and the LFS operation complete.

Delays start at a fixed 250ms for the first retry and double with each
successive retry up to a configurable maximum delay, 10s by default. The
maximum retry is configurable using lfs.transfer.maxretrydelay. Delays
can be disabled by setting the max delay to 0.
2020-04-15 14:10:16 -07:00

83 lines
2.0 KiB
Go

package tq
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestManifestDefaultsToFixedRetries(t *testing.T) {
assert.Equal(t, 8, NewManifest(nil, nil, "", "").MaxRetries())
}
func TestManifestDefaultsToFixedRetryDelay(t *testing.T) {
assert.Equal(t, 10, NewManifest(nil, nil, "", "").MaxRetryDelay())
}
func TestRetryCounterDefaultsToFixedRetries(t *testing.T) {
rc := newRetryCounter()
assert.Equal(t, 8, rc.MaxRetries)
}
func TestRetryCounterDefaultsToFixedRetryDelay(t *testing.T) {
rc := newRetryCounter()
assert.Equal(t, 10, rc.MaxRetryDelay)
}
func TestRetryCounterIncrementsObjects(t *testing.T) {
rc := newRetryCounter()
assert.Equal(t, 1, rc.Increment("oid"))
assert.Equal(t, 1, rc.CountFor("oid"))
assert.Equal(t, 2, rc.Increment("oid"))
assert.Equal(t, 2, rc.CountFor("oid"))
}
func TestRetryCounterCanNotRetryAfterExceedingRetryCount(t *testing.T) {
rc := newRetryCounter()
rc.MaxRetries = 1
rc.Increment("oid")
count, canRetry := rc.CanRetry("oid")
assert.Equal(t, 1, count)
assert.False(t, canRetry)
}
func TestRetryCounterDoesNotDelayFirstAttempt(t *testing.T) {
rc := newRetryCounter()
assert.Equal(t, time.Time{}, rc.ReadyTime("oid"))
}
func TestRetryCounterDelaysExponentially(t *testing.T) {
rc := newRetryCounter()
start := time.Now()
rc.Increment("oid")
ready1 := rc.ReadyTime("oid")
assert.GreaterOrEqual(t, int64(ready1.Sub(start)/time.Millisecond), int64(baseRetryDelayMs))
rc.Increment("oid")
ready2 := rc.ReadyTime("oid")
assert.GreaterOrEqual(t, int64(ready2.Sub(start)/time.Millisecond), int64(2*baseRetryDelayMs))
}
func TestRetryCounterLimitsDelay(t *testing.T) {
rc := newRetryCounter()
rc.MaxRetryDelay = 1
for i := 0; i < 4; i++ {
rc.Increment("oid")
}
rt := rc.ReadyTime("oid")
assert.WithinDuration(t, time.Now(), rt, 1*time.Second)
}
func TestBatchSizeReturnsBatchSize(t *testing.T) {
q := NewTransferQueue(
Upload, NewManifest(nil, nil, "", ""), "origin", WithBatchSize(3))
assert.Equal(t, 3, q.BatchSize())
}