Commit 41055e9a authored by Oliver Tan's avatar Oliver Tan

settings: fix RegisterPublicNonNegativeDurationSettingWithMaximum

`RegisterPublicNonNegativeDurationSettingWithMaximum` should include the
maximum. Seems broken by `ac3c7233`.

Release note (bug fix): Re-allow
`diagnostics.forced_sql_stat_reset.interval`,
`diagnostics.sql_stat_reset.interval` and `external.graphite.interval`
to set to their maximum values (24hr, 24hr and 15min respectively). This
previously only excluded these values to be allowed.
parent 91b28807
......@@ -117,6 +117,7 @@ func RegisterPublicNonNegativeDurationSetting(
// RegisterPublicNonNegativeDurationSettingWithMaximum defines a new setting with
// type duration, makes it public, and sets a maximum value.
// The maximum value is an allowed value.
func RegisterPublicNonNegativeDurationSettingWithMaximum(
key, desc string, defaultValue time.Duration, maxValue time.Duration,
) *DurationSetting {
......@@ -124,7 +125,7 @@ func RegisterPublicNonNegativeDurationSettingWithMaximum(
if v < 0 {
return errors.Errorf("cannot set %s to a negative duration: %s", key, v)
}
if v >= maxValue {
if v > maxValue {
return errors.Errorf("cannot set %s to a value larger than %s", key, maxValue)
}
return nil
......
......@@ -146,6 +146,7 @@ var i1A = settings.RegisterIntSetting("i.1", "desc", 0)
var i2A = settings.RegisterIntSetting("i.2", "desc", 5)
var fA = settings.RegisterFloatSetting("f", "desc", 5.4)
var dA = settings.RegisterDurationSetting("d", "desc", time.Second)
var _ = settings.RegisterPublicNonNegativeDurationSettingWithMaximum("d_with_maximum", "desc", time.Second, time.Hour)
var eA = settings.RegisterEnumSetting("e", "desc", "foo", map[int64]string{1: "foo", 2: "bar", 3: "baz"})
var byteSize = settings.RegisterByteSizeSetting("zzz", "desc", mb)
var mA = settings.RegisterStateMachineSettingImpl("statemachine", "foo", &dummyTransformer{})
......@@ -181,6 +182,26 @@ var iVal = settings.RegisterValidatedIntSetting(
return nil
})
func TestValidation(t *testing.T) {
sv := &settings.Values{}
sv.Init(settings.TestOpaque)
u := settings.NewUpdater(sv)
t.Run("d_with_maximum", func(t *testing.T) {
err := u.Set("d_with_maximum", "1h", "d")
require.NoError(t, err)
err = u.Set("d_with_maximum", "0h", "d")
require.NoError(t, err)
err = u.Set("d_with_maximum", "30m", "d")
require.NoError(t, err)
err = u.Set("d_with_maximum", "-1m", "d")
require.Error(t, err)
err = u.Set("d_with_maximum", "1h1s", "d")
require.Error(t, err)
})
}
func TestCache(t *testing.T) {
ctx := context.Background()
sv := &settings.Values{}
......
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