2015-09-17 17:12:37 +00:00
|
|
|
package pacer
|
|
|
|
|
|
|
|
import (
|
2019-02-09 20:52:15 +00:00
|
|
|
"sync"
|
2015-09-17 17:12:37 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-06-12 14:06:02 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-02-09 20:52:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-09-17 17:12:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
2016-02-26 16:59:52 +00:00
|
|
|
const expectedRetries = 7
|
2019-02-09 20:52:15 +00:00
|
|
|
const expectedConnections = 9
|
|
|
|
p := New(RetriesOption(expectedRetries), MaxConnectionsOption(expectedConnections))
|
|
|
|
if d, ok := p.calculator.(*Default); ok {
|
|
|
|
assert.Equal(t, 10*time.Millisecond, d.minSleep)
|
|
|
|
assert.Equal(t, 2*time.Second, d.maxSleep)
|
|
|
|
assert.Equal(t, d.minSleep, p.state.SleepTime)
|
|
|
|
assert.Equal(t, uint(2), d.decayConstant)
|
|
|
|
assert.Equal(t, uint(1), d.attackConstant)
|
|
|
|
} else {
|
|
|
|
t.Errorf("calculator")
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedRetries, p.retries)
|
|
|
|
assert.Equal(t, 1, cap(p.pacer))
|
|
|
|
assert.Equal(t, 1, len(p.pacer))
|
|
|
|
assert.Equal(t, expectedConnections, p.maxConnections)
|
|
|
|
assert.Equal(t, expectedConnections, cap(p.connTokens))
|
|
|
|
assert.Equal(t, 0, p.state.ConsecutiveRetries)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaxConnections(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
p := New()
|
|
|
|
p.SetMaxConnections(20)
|
|
|
|
assert.Equal(t, 20, p.maxConnections)
|
|
|
|
assert.Equal(t, 20, cap(p.connTokens))
|
2015-09-17 17:12:37 +00:00
|
|
|
p.SetMaxConnections(0)
|
2019-02-09 20:52:15 +00:00
|
|
|
assert.Equal(t, 0, p.maxConnections)
|
|
|
|
assert.Nil(t, p.connTokens)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 16:07:39 +00:00
|
|
|
func TestDecay(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
c := NewDefault(MinSleep(1*time.Microsecond), MaxSleep(1*time.Second))
|
2016-03-05 16:07:39 +00:00
|
|
|
for _, test := range []struct {
|
2019-02-09 20:52:15 +00:00
|
|
|
in State
|
2016-03-05 16:07:39 +00:00
|
|
|
attackConstant uint
|
|
|
|
want time.Duration
|
|
|
|
}{
|
2019-02-09 20:52:15 +00:00
|
|
|
{State{SleepTime: 8 * time.Millisecond}, 1, 4 * time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Millisecond}, 0, 1 * time.Microsecond},
|
|
|
|
{State{SleepTime: 1 * time.Millisecond}, 2, (3 * time.Millisecond) / 4},
|
|
|
|
{State{SleepTime: 1 * time.Millisecond}, 3, (7 * time.Millisecond) / 8},
|
2016-03-05 16:07:39 +00:00
|
|
|
} {
|
2019-02-09 20:52:15 +00:00
|
|
|
c.decayConstant = test.attackConstant
|
|
|
|
got := c.Calculate(test.in)
|
|
|
|
assert.Equal(t, test.want, got, "test: %+v", test)
|
2016-03-05 16:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttack(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
c := NewDefault(MinSleep(1*time.Microsecond), MaxSleep(1*time.Second))
|
2016-03-05 16:07:39 +00:00
|
|
|
for _, test := range []struct {
|
2019-02-09 20:52:15 +00:00
|
|
|
in State
|
2016-03-05 16:07:39 +00:00
|
|
|
attackConstant uint
|
|
|
|
want time.Duration
|
|
|
|
}{
|
2019-02-09 20:52:15 +00:00
|
|
|
{State{SleepTime: 1 * time.Millisecond, ConsecutiveRetries: 1}, 1, 2 * time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Millisecond, ConsecutiveRetries: 1}, 0, 1 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Millisecond, ConsecutiveRetries: 1}, 2, (4 * time.Millisecond) / 3},
|
|
|
|
{State{SleepTime: 1 * time.Millisecond, ConsecutiveRetries: 1}, 3, (8 * time.Millisecond) / 7},
|
2016-03-05 16:07:39 +00:00
|
|
|
} {
|
2019-02-09 20:52:15 +00:00
|
|
|
c.attackConstant = test.attackConstant
|
|
|
|
got := c.Calculate(test.in)
|
|
|
|
assert.Equal(t, test.want, got, "test: %+v", test)
|
2016-03-05 16:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 17:12:37 +00:00
|
|
|
func TestSetRetries(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
p := New()
|
|
|
|
p.SetRetries(18)
|
|
|
|
assert.Equal(t, 18, p.retries)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// emptyTokens empties the pacer of all its tokens
|
|
|
|
func emptyTokens(p *Pacer) {
|
|
|
|
for len(p.pacer) != 0 {
|
|
|
|
<-p.pacer
|
|
|
|
}
|
|
|
|
for len(p.connTokens) != 0 {
|
|
|
|
<-p.connTokens
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 18:08:55 +00:00
|
|
|
// waitForPace waits for duration for the pace to arrive
|
|
|
|
// returns the time that it arrived or a zero time
|
|
|
|
func waitForPace(p *Pacer, duration time.Duration) (when time.Time) {
|
|
|
|
select {
|
|
|
|
case <-time.After(duration):
|
|
|
|
return
|
|
|
|
case <-p.pacer:
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 17:12:37 +00:00
|
|
|
func TestBeginCall(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
p := New(MaxConnectionsOption(10), CalculatorOption(NewDefault(MinSleep(1*time.Millisecond))))
|
2015-09-17 17:12:37 +00:00
|
|
|
emptyTokens(p)
|
|
|
|
go p.beginCall()
|
2015-10-02 18:08:55 +00:00
|
|
|
if !waitForPace(p, 10*time.Millisecond).IsZero() {
|
2015-09-17 17:12:37 +00:00
|
|
|
t.Errorf("beginSleep fired too early #1")
|
|
|
|
}
|
2015-10-02 18:08:55 +00:00
|
|
|
startTime := time.Now()
|
2015-09-17 17:12:37 +00:00
|
|
|
p.pacer <- struct{}{}
|
2015-10-02 18:08:55 +00:00
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
connTime := time.Now()
|
2015-09-17 17:12:37 +00:00
|
|
|
p.connTokens <- struct{}{}
|
2015-10-02 18:08:55 +00:00
|
|
|
time.Sleep(1 * time.Millisecond)
|
2018-02-27 11:06:59 +00:00
|
|
|
paceTime := waitForPace(p, 1000*time.Millisecond)
|
2015-10-02 18:08:55 +00:00
|
|
|
if paceTime.IsZero() {
|
2015-09-17 17:12:37 +00:00
|
|
|
t.Errorf("beginSleep didn't fire")
|
2015-10-02 18:08:55 +00:00
|
|
|
} else if paceTime.Sub(startTime) < 0 {
|
|
|
|
t.Errorf("pace arrived before returning pace token")
|
|
|
|
} else if paceTime.Sub(connTime) < 0 {
|
|
|
|
t.Errorf("pace arrived before sending conn token")
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeginCallZeroConnections(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
p := New(MaxConnectionsOption(0), CalculatorOption(NewDefault(MinSleep(1*time.Millisecond))))
|
2015-09-17 17:12:37 +00:00
|
|
|
emptyTokens(p)
|
|
|
|
go p.beginCall()
|
2015-10-02 18:08:55 +00:00
|
|
|
if !waitForPace(p, 10*time.Millisecond).IsZero() {
|
2015-09-17 17:12:37 +00:00
|
|
|
t.Errorf("beginSleep fired too early #1")
|
|
|
|
}
|
2015-10-02 18:08:55 +00:00
|
|
|
startTime := time.Now()
|
2015-09-17 17:12:37 +00:00
|
|
|
p.pacer <- struct{}{}
|
2015-10-02 18:08:55 +00:00
|
|
|
time.Sleep(1 * time.Millisecond)
|
2018-02-27 11:06:59 +00:00
|
|
|
paceTime := waitForPace(p, 1000*time.Millisecond)
|
2015-10-02 18:08:55 +00:00
|
|
|
if paceTime.IsZero() {
|
2015-09-17 17:12:37 +00:00
|
|
|
t.Errorf("beginSleep didn't fire")
|
2015-10-02 18:08:55 +00:00
|
|
|
} else if paceTime.Sub(startTime) < 0 {
|
|
|
|
t.Errorf("pace arrived before returning pace token")
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultPacer(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
c := NewDefault(MinSleep(1*time.Millisecond), MaxSleep(1*time.Second), DecayConstant(2))
|
2015-09-17 17:12:37 +00:00
|
|
|
for _, test := range []struct {
|
2019-02-09 20:52:15 +00:00
|
|
|
state State
|
2015-09-17 17:12:37 +00:00
|
|
|
want time.Duration
|
|
|
|
}{
|
2019-02-09 20:52:15 +00:00
|
|
|
{State{SleepTime: 1 * time.Millisecond, ConsecutiveRetries: 1}, 2 * time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 1}, 1 * time.Second},
|
|
|
|
{State{SleepTime: (3 * time.Second) / 4, ConsecutiveRetries: 1}, 1 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second}, 750 * time.Millisecond},
|
|
|
|
{State{SleepTime: 1000 * time.Microsecond}, 1 * time.Millisecond},
|
|
|
|
{State{SleepTime: 1200 * time.Microsecond}, 1 * time.Millisecond},
|
2015-09-17 17:12:37 +00:00
|
|
|
} {
|
2019-02-09 20:52:15 +00:00
|
|
|
got := c.Calculate(test.state)
|
|
|
|
assert.Equal(t, test.want, got, "test: %+v", test)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAmazonCloudDrivePacer(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
c := NewAmazonCloudDrive(MinSleep(1 * time.Millisecond))
|
2015-09-17 17:12:37 +00:00
|
|
|
// Do lots of times because of the random number!
|
|
|
|
for _, test := range []struct {
|
2019-02-09 20:52:15 +00:00
|
|
|
state State
|
|
|
|
want time.Duration
|
2015-09-17 17:12:37 +00:00
|
|
|
}{
|
2019-02-09 20:52:15 +00:00
|
|
|
{State{SleepTime: 1 * time.Millisecond, ConsecutiveRetries: 0}, 1 * time.Millisecond},
|
|
|
|
{State{SleepTime: 10 * time.Millisecond, ConsecutiveRetries: 0}, 1 * time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 1}, 500 * time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 2}, 1 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 3}, 2 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 4}, 4 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 5}, 8 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 6}, 16 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 7}, 32 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 8}, 64 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 9}, 128 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 10}, 128 * time.Second},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 11}, 128 * time.Second},
|
2015-09-17 17:12:37 +00:00
|
|
|
} {
|
|
|
|
const n = 1000
|
|
|
|
var sum time.Duration
|
|
|
|
// measure average time over n cycles
|
|
|
|
for i := 0; i < n; i++ {
|
2019-02-09 20:52:15 +00:00
|
|
|
sum += c.Calculate(test.state)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
got := sum / n
|
2019-02-09 20:52:15 +00:00
|
|
|
assert.False(t, got < (test.want*9)/10 || got > (test.want*11)/10, "test: %+v", test)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:40:30 +00:00
|
|
|
func TestAzureIMDSPacer(t *testing.T) {
|
|
|
|
c := NewAzureIMDS()
|
|
|
|
for _, test := range []struct {
|
|
|
|
state State
|
|
|
|
want time.Duration
|
|
|
|
}{
|
|
|
|
{State{SleepTime: 0, ConsecutiveRetries: 0}, 0},
|
|
|
|
{State{SleepTime: 0, ConsecutiveRetries: 1}, 2 * time.Second},
|
|
|
|
{State{SleepTime: 2 * time.Second, ConsecutiveRetries: 2}, 6 * time.Second},
|
|
|
|
{State{SleepTime: 6 * time.Second, ConsecutiveRetries: 3}, 14 * time.Second},
|
|
|
|
{State{SleepTime: 14 * time.Second, ConsecutiveRetries: 4}, 30 * time.Second},
|
|
|
|
} {
|
|
|
|
got := c.Calculate(test.state)
|
|
|
|
assert.Equal(t, test.want, got, "test: %+v", test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 16:57:09 +00:00
|
|
|
func TestGoogleDrivePacer(t *testing.T) {
|
|
|
|
// Do lots of times because of the random number!
|
|
|
|
for _, test := range []struct {
|
2019-02-09 20:52:15 +00:00
|
|
|
state State
|
|
|
|
want time.Duration
|
2016-10-17 16:57:09 +00:00
|
|
|
}{
|
2019-02-09 20:52:15 +00:00
|
|
|
{State{SleepTime: 1 * time.Millisecond}, 0},
|
|
|
|
{State{SleepTime: 10 * time.Millisecond}, 0},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 1}, 1*time.Second + 500*time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 2}, 2*time.Second + 500*time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 3}, 4*time.Second + 500*time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 4}, 8*time.Second + 500*time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 5}, 16*time.Second + 500*time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 6}, 16*time.Second + 500*time.Millisecond},
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 7}, 16*time.Second + 500*time.Millisecond},
|
2016-10-17 16:57:09 +00:00
|
|
|
} {
|
|
|
|
const n = 1000
|
|
|
|
var sum time.Duration
|
|
|
|
// measure average time over n cycles
|
|
|
|
for i := 0; i < n; i++ {
|
2019-02-09 20:52:15 +00:00
|
|
|
c := NewGoogleDrive(MinSleep(1 * time.Millisecond))
|
|
|
|
sum += c.Calculate(test.state)
|
2016-10-17 16:57:09 +00:00
|
|
|
}
|
|
|
|
got := sum / n
|
2019-02-09 20:52:15 +00:00
|
|
|
assert.False(t, got < (test.want*9)/10 || got > (test.want*11)/10, "test: %+v, got: %v", test, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
const minSleep = 2 * time.Millisecond
|
|
|
|
for _, test := range []struct {
|
|
|
|
calls int
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
{1, 0},
|
|
|
|
{9, 0},
|
|
|
|
{10, 0},
|
|
|
|
{11, 1},
|
|
|
|
{12, 2},
|
|
|
|
} {
|
|
|
|
c := NewGoogleDrive(MinSleep(minSleep), Burst(10))
|
|
|
|
count := 0
|
|
|
|
for i := 0; i < test.calls; i++ {
|
|
|
|
sleep := c.Calculate(State{})
|
|
|
|
if sleep != 0 {
|
|
|
|
count++
|
|
|
|
}
|
2016-10-17 16:57:09 +00:00
|
|
|
}
|
2019-02-09 20:52:15 +00:00
|
|
|
assert.Equalf(t, test.want, count, "test: %+v, got: %v", test, count)
|
2016-10-17 16:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 04:41:04 +00:00
|
|
|
func TestS3Pacer(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
c := NewS3(MinSleep(10*time.Millisecond), MaxSleep(1*time.Second), DecayConstant(2))
|
2018-09-03 04:41:04 +00:00
|
|
|
for _, test := range []struct {
|
2019-02-09 20:52:15 +00:00
|
|
|
state State
|
2018-09-03 04:41:04 +00:00
|
|
|
want time.Duration
|
|
|
|
}{
|
2019-02-09 20:52:15 +00:00
|
|
|
{State{SleepTime: 0, ConsecutiveRetries: 1}, 10 * time.Millisecond}, //Things were going ok, we failed once, back off to minSleep
|
|
|
|
{State{SleepTime: 10 * time.Millisecond, ConsecutiveRetries: 1}, 20 * time.Millisecond}, //Another fail, double the backoff
|
|
|
|
{State{SleepTime: 10 * time.Millisecond}, 0}, //Things start going ok when we're at minSleep; should result in no sleep
|
|
|
|
{State{SleepTime: 12 * time.Millisecond}, 0}, //*near* minsleep and going ok, decay would take below minSleep, should go to 0
|
|
|
|
{State{SleepTime: 0}, 0}, //Things have been going ok; not retrying should keep sleep at 0
|
|
|
|
{State{SleepTime: 1 * time.Second, ConsecutiveRetries: 1}, 1 * time.Second}, //Check maxSleep is enforced
|
|
|
|
{State{SleepTime: (3 * time.Second) / 4, ConsecutiveRetries: 1}, 1 * time.Second}, //Check attack heading to maxSleep doesn't exceed maxSleep
|
|
|
|
{State{SleepTime: 1 * time.Second}, 750 * time.Millisecond}, //Check decay from maxSleep
|
|
|
|
{State{SleepTime: 48 * time.Millisecond}, 36 * time.Millisecond}, //Check simple decay above minSleep
|
2018-09-03 04:41:04 +00:00
|
|
|
} {
|
2019-02-09 20:52:15 +00:00
|
|
|
got := c.Calculate(test.state)
|
|
|
|
assert.Equal(t, test.want, got, "test: %+v", test)
|
2018-09-03 04:41:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 17:12:37 +00:00
|
|
|
func TestEndCall(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
p := New(MaxConnectionsOption(5))
|
2015-09-17 17:12:37 +00:00
|
|
|
emptyTokens(p)
|
2019-02-09 20:52:15 +00:00
|
|
|
p.state.ConsecutiveRetries = 1
|
|
|
|
p.endCall(true, nil)
|
|
|
|
assert.Equal(t, 1, len(p.connTokens))
|
|
|
|
assert.Equal(t, 2, p.state.ConsecutiveRetries)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndCallZeroConnections(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
p := New(MaxConnectionsOption(0))
|
2015-09-17 17:12:37 +00:00
|
|
|
emptyTokens(p)
|
2019-02-09 20:52:15 +00:00
|
|
|
p.state.ConsecutiveRetries = 1
|
|
|
|
p.endCall(false, nil)
|
|
|
|
assert.Equal(t, 0, len(p.connTokens))
|
|
|
|
assert.Equal(t, 0, p.state.ConsecutiveRetries)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 14:06:02 +00:00
|
|
|
var errFoo = errors.New("foo")
|
2015-09-17 17:12:37 +00:00
|
|
|
|
|
|
|
type dummyPaced struct {
|
|
|
|
retry bool
|
|
|
|
called int
|
2019-02-09 20:52:15 +00:00
|
|
|
wait *sync.Cond
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *dummyPaced) fn() (bool, error) {
|
2019-02-09 20:52:15 +00:00
|
|
|
if dp.wait != nil {
|
|
|
|
dp.wait.L.Lock()
|
|
|
|
dp.called++
|
|
|
|
dp.wait.Wait()
|
|
|
|
dp.wait.L.Unlock()
|
|
|
|
} else {
|
|
|
|
dp.called++
|
|
|
|
}
|
2015-09-17 17:12:37 +00:00
|
|
|
return dp.retry, errFoo
|
|
|
|
}
|
|
|
|
|
2019-02-09 20:52:15 +00:00
|
|
|
func TestCallFixed(t *testing.T) {
|
|
|
|
p := New(CalculatorOption(NewDefault(MinSleep(1*time.Millisecond), MaxSleep(2*time.Millisecond))))
|
2015-09-17 17:12:37 +00:00
|
|
|
|
|
|
|
dp := &dummyPaced{retry: false}
|
|
|
|
err := p.call(dp.fn, 10)
|
2019-02-09 20:52:15 +00:00
|
|
|
assert.Equal(t, 1, dp.called)
|
|
|
|
assert.Equal(t, errFoo, err)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_callRetry(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
p := New(CalculatorOption(NewDefault(MinSleep(1*time.Millisecond), MaxSleep(2*time.Millisecond))))
|
2015-09-17 17:12:37 +00:00
|
|
|
|
|
|
|
dp := &dummyPaced{retry: true}
|
|
|
|
err := p.call(dp.fn, 10)
|
2019-02-09 20:52:15 +00:00
|
|
|
assert.Equal(t, 10, dp.called)
|
|
|
|
assert.Equal(t, errFoo, err)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCall(t *testing.T) {
|
2019-02-09 20:52:15 +00:00
|
|
|
p := New(RetriesOption(20), CalculatorOption(NewDefault(MinSleep(1*time.Millisecond), MaxSleep(2*time.Millisecond))))
|
2015-09-17 17:12:37 +00:00
|
|
|
|
|
|
|
dp := &dummyPaced{retry: true}
|
|
|
|
err := p.Call(dp.fn)
|
2019-02-09 20:52:15 +00:00
|
|
|
assert.Equal(t, 20, dp.called)
|
|
|
|
assert.Equal(t, errFoo, err)
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 20:52:15 +00:00
|
|
|
func TestCallParallel(t *testing.T) {
|
|
|
|
p := New(MaxConnectionsOption(3), RetriesOption(1), CalculatorOption(NewDefault(MinSleep(100*time.Microsecond), MaxSleep(1*time.Millisecond))))
|
2015-09-17 17:12:37 +00:00
|
|
|
|
2019-02-09 20:52:15 +00:00
|
|
|
wait := sync.NewCond(&sync.Mutex{})
|
|
|
|
funcs := make([]*dummyPaced, 5)
|
|
|
|
for i := range funcs {
|
|
|
|
dp := &dummyPaced{wait: wait}
|
|
|
|
funcs[i] = dp
|
|
|
|
go func() {
|
|
|
|
assert.Equal(t, errFoo, p.CallNoRetry(dp.fn))
|
|
|
|
}()
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
2019-09-20 15:07:55 +00:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2019-02-09 20:52:15 +00:00
|
|
|
called := 0
|
|
|
|
wait.L.Lock()
|
|
|
|
for _, dp := range funcs {
|
|
|
|
called += dp.called
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|
2019-02-09 20:52:15 +00:00
|
|
|
wait.L.Unlock()
|
|
|
|
|
|
|
|
assert.Equal(t, 3, called)
|
|
|
|
wait.Broadcast()
|
2019-09-20 15:07:55 +00:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2019-02-09 20:52:15 +00:00
|
|
|
|
|
|
|
called = 0
|
|
|
|
wait.L.Lock()
|
|
|
|
for _, dp := range funcs {
|
|
|
|
called += dp.called
|
|
|
|
}
|
|
|
|
wait.L.Unlock()
|
|
|
|
|
|
|
|
assert.Equal(t, 5, called)
|
|
|
|
wait.Broadcast()
|
2015-09-17 17:12:37 +00:00
|
|
|
}
|