From bd43f892a7271a9fd4b210a250a857c3094798bf Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Tue, 18 Jul 2023 14:01:27 -0400 Subject: [PATCH 1/2] slight rewrite to take more params Signed-off-by: Andrew Mason --- go/viperutil/internal/sync/sync_test.go | 29 +++++++++++++------------ 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/go/viperutil/internal/sync/sync_test.go b/go/viperutil/internal/sync/sync_test.go index 128824fe1ce..603550cd536 100644 --- a/go/viperutil/internal/sync/sync_test.go +++ b/go/viperutil/internal/sync/sync_test.go @@ -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 @@ -75,19 +72,23 @@ 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(".", "TestWatchConfig_*.json") + require.NoError(t, err) + t.Cleanup(func() { os.Remove(tmp.Name()) }) + + 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 @@ -103,7 +104,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")) @@ -163,7 +164,7 @@ func TestWatchConfig(t *testing.T) { } for i := 0; i < 100; i++ { - require.NoError(t, writeRandomConfig()) + require.NoError(t, writeRandomConfig(tmp)) time.Sleep(writeJitter()) } From a9ee42db7b64d048a08124774c7cc973ea74795b Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Tue, 18 Jul 2023 14:02:08 -0400 Subject: [PATCH 2/2] watchconfig runs in its own tempdir Signed-off-by: Andrew Mason --- go/viperutil/internal/sync/sync_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/go/viperutil/internal/sync/sync_test.go b/go/viperutil/internal/sync/sync_test.go index 603550cd536..e817c6863a7 100644 --- a/go/viperutil/internal/sync/sync_test.go +++ b/go/viperutil/internal/sync/sync_test.go @@ -77,9 +77,8 @@ func TestWatchConfig(t *testing.T) { return writeConfig(tmp, a, b) } - tmp, err := os.CreateTemp(".", "TestWatchConfig_*.json") + tmp, err := os.CreateTemp(t.TempDir(), "TestWatchConfig_*.json") require.NoError(t, err) - t.Cleanup(func() { os.Remove(tmp.Name()) }) require.NoError(t, writeRandomConfig(tmp))