Backoff and Randomness additions for use in client utilities
Updated String to use int, and int not to panic, and panic to test panic Format fixes.
This commit is contained in:
parent
3a5e7d15f7
commit
7ffaef63d1
@ -70,6 +70,13 @@ func (p *Backoff) Next(id string, eventTime time.Time) {
|
|||||||
entry.lastUpdate = p.Clock.Now()
|
entry.lastUpdate = p.Clock.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset forces clearing of all backoff data for a given key.
|
||||||
|
func (p *Backoff) Reset(id string) {
|
||||||
|
p.Lock()
|
||||||
|
defer p.Unlock()
|
||||||
|
delete(p.perItemBackoff, id)
|
||||||
|
}
|
||||||
|
|
||||||
// Returns True if the elapsed time since eventTime is smaller than the current backoff window
|
// Returns True if the elapsed time since eventTime is smaller than the current backoff window
|
||||||
func (p *Backoff) IsInBackOffSince(id string, eventTime time.Time) bool {
|
func (p *Backoff) IsInBackOffSince(id string, eventTime time.Time) bool {
|
||||||
p.Lock()
|
p.Lock()
|
||||||
|
@ -46,6 +46,14 @@ func TestSlowBackoff(t *testing.T) {
|
|||||||
}
|
}
|
||||||
b.Next(id, tc.Now())
|
b.Next(id, tc.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Now confirm that the Reset cancels backoff.
|
||||||
|
b.Next(id, tc.Now())
|
||||||
|
b.Reset(id)
|
||||||
|
if b.Get(id) != 0 {
|
||||||
|
t.Errorf("Reset didn't clear the backoff.")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBackoffReset(t *testing.T) {
|
func TestBackoffReset(t *testing.T) {
|
||||||
|
@ -32,19 +32,12 @@ var rng = struct {
|
|||||||
rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
|
rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
|
||||||
}
|
}
|
||||||
|
|
||||||
// String generates a random alphanumeric string n characters long. This will
|
// Intn generates an integer in range 0->max.
|
||||||
// panic if n is less than zero.
|
// By design this should panic if input is invalid, <= 0.
|
||||||
func String(n int) string {
|
func Intn(max int) int {
|
||||||
if n < 0 {
|
|
||||||
panic("out-of-bounds value")
|
|
||||||
}
|
|
||||||
b := make([]rune, n)
|
|
||||||
rng.Lock()
|
rng.Lock()
|
||||||
defer rng.Unlock()
|
defer rng.Unlock()
|
||||||
for i := range b {
|
return rng.rand.Intn(max)
|
||||||
b[i] = letters[rng.rand.Intn(numLetters)]
|
|
||||||
}
|
|
||||||
return string(b)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seed seeds the rng with the provided seed.
|
// Seed seeds the rng with the provided seed.
|
||||||
@ -62,3 +55,13 @@ func Perm(n int) []int {
|
|||||||
defer rng.Unlock()
|
defer rng.Unlock()
|
||||||
return rng.rand.Perm(n)
|
return rng.rand.Perm(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String generates a random alphanumeric string n characters long. This will
|
||||||
|
// panic if n is less than zero.
|
||||||
|
func String(length int) string {
|
||||||
|
b := make([]rune, length)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = letters[Intn(numLetters)]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
@ -37,6 +37,27 @@ func TestString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Confirm that panic occurs on invalid input.
|
||||||
|
func TestRangePanic(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err == nil {
|
||||||
|
t.Errorf("Panic didn't occur!")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
// Should result in an error...
|
||||||
|
Intn(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntn(t *testing.T) {
|
||||||
|
// 0 is invalid.
|
||||||
|
for _, max := range []int{1, 2, 10, 123} {
|
||||||
|
inrange := Intn(max)
|
||||||
|
if inrange < 0 || inrange > max {
|
||||||
|
t.Errorf("%v out of range (0,%v)", inrange, max)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPerm(t *testing.T) {
|
func TestPerm(t *testing.T) {
|
||||||
Seed(5)
|
Seed(5)
|
||||||
rand.Seed(5)
|
rand.Seed(5)
|
||||||
|
Loading…
Reference in New Issue
Block a user