-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CI] deflake viper sync tests #13185
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
Copyright 2023 The Vitess Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sync | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPersistConfig(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. testing done here:
results:
|
||
type config struct { | ||
Foo int `json:"foo"` | ||
} | ||
|
||
loadConfig := func(t *testing.T, fs afero.Fs) config { | ||
t.Helper() | ||
|
||
data, err := afero.ReadFile(fs, "config.json") | ||
require.NoError(t, err) | ||
|
||
var cfg config | ||
require.NoError(t, json.Unmarshal(data, &cfg)) | ||
|
||
return cfg | ||
} | ||
|
||
setup := func(t *testing.T, v *Viper, minWaitInterval time.Duration) (afero.Fs, <-chan struct{}) { | ||
t.Helper() | ||
|
||
fs := afero.NewMemMapFs() | ||
cfg := config{ | ||
Foo: jitter(1, 100), | ||
} | ||
|
||
data, err := json.Marshal(&cfg) | ||
require.NoError(t, err) | ||
|
||
err = afero.WriteFile(fs, "config.json", data, 0644) | ||
require.NoError(t, err) | ||
|
||
static := viper.New() | ||
static.SetFs(fs) | ||
static.SetConfigFile("config.json") | ||
|
||
require.NoError(t, static.ReadInConfig()) | ||
require.Equal(t, cfg.Foo, static.GetInt("foo")) | ||
|
||
ch := make(chan struct{}, 1) | ||
v.onConfigWrite = func() { ch <- struct{}{} } | ||
v.SetFs(fs) | ||
|
||
cancel, err := v.Watch(context.Background(), static, minWaitInterval) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(cancel) | ||
return fs, ch | ||
} | ||
|
||
t.Run("basic", func(t *testing.T) { | ||
v := New() | ||
|
||
minPersistWaitInterval := 1 * time.Second | ||
get := AdaptGetter("foo", func(v *viper.Viper) func(key string) int { return v.GetInt }, v) | ||
fs, ch := setup(t, v, minPersistWaitInterval) | ||
|
||
old := get("foo") | ||
loadConfig(t, fs) | ||
v.Set("foo", old+1) | ||
// This should happen immediately in-memory and on-disk. | ||
assert.Equal(t, old+1, get("foo")) | ||
<-ch | ||
assert.Equal(t, old+1, loadConfig(t, fs).Foo) | ||
Comment on lines
+91
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming the Set call is synchronous so when it returns the in-memory value is set. For the file, is that I/O synchronous as well (e.g. os.WriteFile done in the main coroutine)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the write is async w.r.t the |
||
|
||
v.Set("foo", old+2) | ||
// This should _also_ happen immediately in-memory, but not on-disk. | ||
// It will take up to 2 * minPersistWaitInterval to reach the disk. | ||
assert.Equal(t, old+2, get("foo")) | ||
assert.Equal(t, old+1, loadConfig(t, fs).Foo) | ||
|
||
select { | ||
case <-ch: | ||
case <-time.After(2 * minPersistWaitInterval): | ||
assert.Fail(t, "config was not persisted quickly enough", "config took longer than %s to persist (minPersistWaitInterval = %s)", 2*minPersistWaitInterval, minPersistWaitInterval) | ||
} | ||
|
||
assert.Equal(t, old+2, loadConfig(t, fs).Foo) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we're CPU starved and/or disk I/O is paused/backed up (or we're scheduled later than other processes)? Is that really a failure of the Vitess feature? We can't assume that an I/O request is immediately serviced and completed. Since I don't think this is relevant to the feature, I would wait up to 30 seconds here. Failing here because of issues outside of our control in the execution environment does not help anyone. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good |
||
}) | ||
|
||
t.Run("no persist interval", func(t *testing.T) { | ||
v := New() | ||
|
||
var minPersistWaitInterval time.Duration | ||
get := AdaptGetter("foo", func(v *viper.Viper) func(key string) int { return v.GetInt }, v) | ||
fs, ch := setup(t, v, minPersistWaitInterval) | ||
|
||
old := get("foo") | ||
loadConfig(t, fs) | ||
v.Set("foo", old+1) | ||
// This should happen immediately in-memory and on-disk. | ||
assert.Equal(t, old+1, get("foo")) | ||
<-ch | ||
assert.Equal(t, old+1, loadConfig(t, fs).Foo) | ||
|
||
v.Set("foo", old+2) | ||
// This should _also_ happen immediately in-memory, and on-disk. | ||
assert.Equal(t, old+2, get("foo")) | ||
<-ch | ||
assert.Equal(t, old+2, loadConfig(t, fs).Foo) | ||
}) | ||
} | ||
|
||
func jitter(min, max int) int { | ||
return min + rand.Intn(max-min+1) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason not to set it to nil and only execute it if it's not nil? We should probably have a nil check anyway as a user could set the value to nil, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not possible for a user to set it (it's a private field and this is an internal package), but I was surprised to learn just how much faster it is to do the nil check than an empty func call:
trivial benchmark
soooo lemme make that change!