Commit f91cdb03 authored by hannahhoward's avatar hannahhoward

Reorder params and defaults for delay

parent f1bb990c
...@@ -20,8 +20,8 @@ type D interface { ...@@ -20,8 +20,8 @@ type D interface {
type delay struct { type delay struct {
l sync.RWMutex l sync.RWMutex
t time.Duration t time.Duration
sleeper Sleeper
generator Generator generator Generator
sleeper Sleeper
} }
func (d *delay) Set(t time.Duration) time.Duration { func (d *delay) Set(t time.Duration) time.Duration {
...@@ -51,7 +51,10 @@ func (d *delay) Get() time.Duration { ...@@ -51,7 +51,10 @@ func (d *delay) Get() time.Duration {
} }
// Delay generates a generic delay form a t, a sleeper, and a generator // Delay generates a generic delay form a t, a sleeper, and a generator
func Delay(t time.Duration, sleeper Sleeper, generator Generator) D { func Delay(t time.Duration, generator Generator, sleeper Sleeper) D {
if sleeper == nil {
sleeper = sharedRealSleeper
}
return &delay{ return &delay{
t: t, t: t,
sleeper: sleeper, sleeper: sleeper,
...@@ -61,19 +64,19 @@ func Delay(t time.Duration, sleeper Sleeper, generator Generator) D { ...@@ -61,19 +64,19 @@ func Delay(t time.Duration, sleeper Sleeper, generator Generator) D {
// Fixed returns a delay with fixed latency // Fixed returns a delay with fixed latency
func Fixed(t time.Duration) D { func Fixed(t time.Duration) D {
return Delay(t, sharedRealSleeper, FixedGenerator()) return Delay(t, FixedGenerator(), nil)
} }
// VariableUniform is a delay following a uniform distribution // VariableUniform is a delay following a uniform distribution
// Notice that to implement the D interface Set can only change the minimum delay // Notice that to implement the D interface Set can only change the minimum delay
// the delta is set only at initialization // the delta is set only at initialization
func VariableUniform(t, d time.Duration, rng *rand.Rand) D { func VariableUniform(t, d time.Duration, rng *rand.Rand) D {
return Delay(t, sharedRealSleeper, VariableUniformGenerator(d, rng)) return Delay(t, VariableUniformGenerator(d, rng), nil)
} }
// VariableNormal is a delay following a normal distribution // VariableNormal is a delay following a normal distribution
// Notice that to implement the D interface Set can only change the mean delay // Notice that to implement the D interface Set can only change the mean delay
// the standard deviation is set only at initialization // the standard deviation is set only at initialization
func VariableNormal(t, std time.Duration, rng *rand.Rand) D { func VariableNormal(t, std time.Duration, rng *rand.Rand) D {
return Delay(t, sharedRealSleeper, VariableNormalGenerator(std, rng)) return Delay(t, VariableNormalGenerator(std, rng), nil)
} }
...@@ -68,7 +68,7 @@ func TestDelaySleep(t *testing.T) { ...@@ -68,7 +68,7 @@ func TestDelaySleep(t *testing.T) {
generator := &fixedAdd{toAdd: toAdd} generator := &fixedAdd{toAdd: toAdd}
sleeper := &recordSleeper{lastSleep: -1} sleeper := &recordSleeper{lastSleep: -1}
delay := Delay(initialValue, sleeper, generator) delay := Delay(initialValue, generator, sleeper)
if delay.NextWaitTime() != initialValue+toAdd { if delay.NextWaitTime() != initialValue+toAdd {
t.Fatal("NextWaitTime should call the generator") t.Fatal("NextWaitTime should call the generator")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment