func TestGenerateRandomInt64InRange(t *testing.T) { assert := assert.On(t) base := time.Now().Unix() delta := 100 generator := NewTimestampGenerator(Timestamp(base), delta) for i := 0; i < 100; i++ { val := int64(generator()) assert.Int64(val).AtMost(base + int64(delta)) assert.Int64(val).AtLeast(base - int64(delta)) } }
func TestNoRetry(t *testing.T) { assert := assert.On(t) startTime := time.Now().Unix() err := Timed(10, 100000).On(func() error { return nil }) endTime := time.Now().Unix() assert.Error(err).IsNil() assert.Int64(endTime - startTime).AtLeast(0) }
func TestExponentialBackoff(t *testing.T) { assert := assert.On(t) startTime := time.Now() called := 0 err := ExponentialBackoff(10, 100).On(func() error { called++ return errorTestOnly }) duration := time.Since(startTime) assert.Error(err).Equals(ErrRetryFailed) assert.Int64(int64(duration / time.Millisecond)).AtLeast(4000) }
func TestRetryExhausted(t *testing.T) { assert := assert.On(t) startTime := time.Now() called := 0 err := Timed(2, 1000).On(func() error { called++ return errorTestOnly }) duration := time.Since(startTime) assert.Error(err).Equals(ErrRetryFailed) assert.Int64(int64(duration / time.Millisecond)).AtLeast(1900) }
func TestRetryMultiple(t *testing.T) { assert := assert.On(t) startTime := time.Now() called := 0 err := Timed(10, 1000).On(func() error { if called < 5 { called++ return errorTestOnly } return nil }) duration := time.Since(startTime) assert.Error(err).IsNil() assert.Int64(int64(duration / time.Millisecond)).AtLeast(4900) }