refactor test to avoid negative counter on WG

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
This commit is contained in:
Xavier Coulon 2018-08-06 09:29:13 +02:00
parent e12d3d32d1
commit 41f829b2c9
2 changed files with 6 additions and 5 deletions

View file

@ -319,7 +319,7 @@ func (v *Viper) WatchConfig() {
realConfigFile = currentConfigFile
err := v.ReadInConfig()
if err != nil {
log.Printf("error reading file: %v\n", err)
log.Printf("error reading config file: %v\n", err)
}
if v.onConfigChange != nil {
v.onConfigChange(event)
@ -343,7 +343,6 @@ func (v *Viper) WatchConfig() {
initWG.Done() // done initalizing the watch in this go routine, so the parent routine can move on...
eventsWG.Wait() // now, wait for event loop to end in this go-routine...
}()
fmt.Println(" init WG done")
initWG.Wait() // make sure that the go routine above fully ended before returning
}

View file

@ -1469,17 +1469,19 @@ func TestWatchFile(t *testing.T) {
t.Run("file content changed", func(t *testing.T) {
// given a `config.yaml` file being watched
v, configFile, cleanup := newViperWithConfigFile(t)
fmt.Printf("test config file: %s\n", configFile)
defer cleanup()
_, err := os.Stat(configFile)
require.NoError(t, err)
t.Logf("test config file: %s\n", configFile)
wg := sync.WaitGroup{}
wg.Add(1)
v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) {
t.Logf("config file changed")
wg.Done()
})
v.WatchConfig()
// when overwriting the file and waiting for the custom change notification handler to be triggered
err := ioutil.WriteFile(configFile, []byte("foo: baz\n"), 0640)
err = ioutil.WriteFile(configFile, []byte("foo: baz\n"), 0640)
wg.Wait()
// then the config value should have changed
require.Nil(t, err)