mirror of https://github.com/spf13/viper.git
refactor test to avoid negative counter on WG
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
This commit is contained in:
parent
e12d3d32d1
commit
41f829b2c9
3
viper.go
3
viper.go
|
@ -319,7 +319,7 @@ func (v *Viper) WatchConfig() {
|
||||||
realConfigFile = currentConfigFile
|
realConfigFile = currentConfigFile
|
||||||
err := v.ReadInConfig()
|
err := v.ReadInConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error reading file: %v\n", err)
|
log.Printf("error reading config file: %v\n", err)
|
||||||
}
|
}
|
||||||
if v.onConfigChange != nil {
|
if v.onConfigChange != nil {
|
||||||
v.onConfigChange(event)
|
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...
|
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...
|
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
|
initWG.Wait() // make sure that the go routine above fully ended before returning
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1469,17 +1469,19 @@ func TestWatchFile(t *testing.T) {
|
||||||
t.Run("file content changed", func(t *testing.T) {
|
t.Run("file content changed", func(t *testing.T) {
|
||||||
// given a `config.yaml` file being watched
|
// given a `config.yaml` file being watched
|
||||||
v, configFile, cleanup := newViperWithConfigFile(t)
|
v, configFile, cleanup := newViperWithConfigFile(t)
|
||||||
fmt.Printf("test config file: %s\n", configFile)
|
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
_, err := os.Stat(configFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Logf("test config file: %s\n", configFile)
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
v.WatchConfig()
|
|
||||||
v.OnConfigChange(func(in fsnotify.Event) {
|
v.OnConfigChange(func(in fsnotify.Event) {
|
||||||
t.Logf("config file changed")
|
t.Logf("config file changed")
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
|
v.WatchConfig()
|
||||||
// when overwriting the file and waiting for the custom change notification handler to be triggered
|
// 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()
|
wg.Wait()
|
||||||
// then the config value should have changed
|
// then the config value should have changed
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
Loading…
Reference in New Issue