Skip to content
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

[vipersync] deflake TestWatchConfig #13545

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions go/viperutil/internal/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,16 @@ import (
)

func TestWatchConfig(t *testing.T) {
t.Skip("flaky test (@ajm188): https://github.com/vitessio/vitess/issues/13498")
type config struct {
A, B int
}

tmp, err := os.CreateTemp(".", "TestWatchConfig_*.json")
require.NoError(t, err)
t.Cleanup(func() { os.Remove(tmp.Name()) })

stat, err := os.Stat(tmp.Name())
require.NoError(t, err)
writeConfig := func(tmp *os.File, a, b int) error {
stat, err := os.Stat(tmp.Name())
if err != nil {
return err
}

writeConfig := func(a, b int) error {
data, err := json.Marshal(&config{A: a, B: b})
if err != nil {
return err
Expand All @@ -75,19 +72,22 @@ func TestWatchConfig(t *testing.T) {

return nil
}
writeRandomConfig := func() error {
writeRandomConfig := func(tmp *os.File) error {
a, b := rand.Intn(100), rand.Intn(100)
return writeConfig(a, b)
return writeConfig(tmp, a, b)
}

require.NoError(t, writeRandomConfig())
tmp, err := os.CreateTemp(t.TempDir(), "TestWatchConfig_*.json")
Copy link
Contributor

@mattlord mattlord Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that this is the key fix, as we're using a dedicated directory for our config files rather than the (very deep) PWD for the tests of the vitess root dir (VTROOT).

require.NoError(t, err)

require.NoError(t, writeRandomConfig(tmp))

v := viper.New()
v.SetConfigFile(tmp.Name())
require.NoError(t, v.ReadInConfig())

wCh, rCh := make(chan struct{}), make(chan struct{})
v.OnConfigChange(func(in fsnotify.Event) {
v.OnConfigChange(func(event fsnotify.Event) {
select {
case <-rCh:
return
Expand All @@ -103,7 +103,7 @@ func TestWatchConfig(t *testing.T) {
// Make sure that basic, unsynchronized WatchConfig is set up before
// beginning the actual test.
a, b := v.GetInt("a"), v.GetInt("b")
require.NoError(t, writeConfig(a+1, b+1))
require.NoError(t, writeConfig(tmp, a+1, b+1))
<-wCh // wait for the update to finish

require.Equal(t, a+1, v.GetInt("a"))
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestWatchConfig(t *testing.T) {
}

for i := 0; i < 100; i++ {
require.NoError(t, writeRandomConfig())
require.NoError(t, writeRandomConfig(tmp))
time.Sleep(writeJitter())
}

Expand Down
Loading