Skip to content

Commit

Permalink
runtime/jitter: allow override of defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
  • Loading branch information
hiddeco committed Aug 1, 2023
1 parent 6268121 commit 4b175f1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
19 changes: 16 additions & 3 deletions runtime/jitter/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import (
)

const (
flagIntervalJitter = "interval-jitter-percentage"
flagIntervalJitter = "interval-jitter-percentage"
defaultIntervalJitterPercentage = 10
)

var (
Expand Down Expand Up @@ -101,9 +102,21 @@ type Interval struct {
Percentage uint8
}

// BindFlags will parse the given pflag.FlagSet and load the interval jitter.
// BindFlags will parse the given pflag.FlagSet and load the interval jitter
// with the default value of 10%.
func (o *Interval) BindFlags(fs *pflag.FlagSet) {
fs.Uint8Var(&o.Percentage, flagIntervalJitter, 0,
o.BindFlagsWithDefault(fs, -1)
}

// BindFlagsWithDefault will parse the given pflag.FlagSet and load the interval
// jitter. The defaultPercentage is used to set the default value for the
// interval jitter percentage. If the defaultPercentage is negative, then the
// default value (of 10%) will be used.
func (o *Interval) BindFlagsWithDefault(fs *pflag.FlagSet, defaultPercentage int) {
if defaultPercentage < 0 {
defaultPercentage = defaultIntervalJitterPercentage
}
fs.Uint8Var(&o.Percentage, flagIntervalJitter, uint8(defaultPercentage),
"Percentage of jitter to apply to interval durations. A value of 10 "+
"will apply a jitter of +/-10% to the interval duration. It cannot be "+
"negative, and must be less than 100.")
Expand Down
37 changes: 34 additions & 3 deletions runtime/jitter/interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,40 @@ func TestInterval_BindFlags(t *testing.T) {
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
interval.BindFlags(fs)

err := fs.Set(flagIntervalJitter, "20")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(interval.Percentage).To(Equal(uint8(20)))
g.Expect(interval.Percentage).To(Equal(uint8(defaultIntervalJitterPercentage)))
}

func TestInterval_BindFlagsWithDefault(t *testing.T) {
g := NewWithT(t)

t.Run("with default fallback", func(t *testing.T) {
interval := &Interval{}

fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
interval.BindFlagsWithDefault(fs, -1)

g.Expect(interval.Percentage).To(Equal(uint8(defaultIntervalJitterPercentage)))
})

t.Run("with custom default", func(t *testing.T) {
interval := &Interval{}

fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
interval.BindFlagsWithDefault(fs, 50)

g.Expect(interval.Percentage).To(Equal(uint8(50)))
})

t.Run("with flag override", func(t *testing.T) {
interval := &Interval{}

fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
interval.BindFlagsWithDefault(fs, 0)

err := fs.Set("interval-jitter-percentage", "25")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(interval.Percentage).To(Equal(uint8(25)))
})
}

func TestInterval_SetGlobalJitter(t *testing.T) {
Expand Down

0 comments on commit 4b175f1

Please sign in to comment.