Refactor more code to use testutils.WasCalled (#13054)

* Refactor more code to use testutils.WasCalled

* incorporate review comments

* Revert watcher_test.go changes

The file is in config package and the other tests use config_test.
So it is not visible.
Этот коммит содержится в:
Agniva De Sarker
2019-11-13 13:41:26 +05:30
коммит произвёл Saturnino Abril
родитель 0c8b580458
Коммит 2db6823f5d
5 изменённых файлов: 30 добавлений и 62 удалений

Просмотреть файл

@@ -477,11 +477,7 @@ func TestFileStoreSet(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, oldCfg, retCfg)
select {
case <-called:
case <-time.After(5 * time.Second):
require.Fail(t, "callback should have been called when config written")
}
require.True(t, wasCalled(called, 5*time.Second), "callback should have been called when config written")
})
t.Run("watcher restarted", func(t *testing.T) {
@@ -513,11 +509,7 @@ func TestFileStoreSet(t *testing.T) {
require.NoError(t, err)
ioutil.WriteFile(path, cfgData, 0644)
select {
case <-called:
case <-time.After(5 * time.Second):
require.Fail(t, "callback should have been called when config written")
}
require.True(t, wasCalled(called, 5*time.Second), "callback should have been called when config written")
})
}
@@ -749,11 +741,7 @@ func TestFileStoreLoad(t *testing.T) {
err = fs.Load()
require.NoError(t, err)
select {
case <-called:
case <-time.After(5 * time.Second):
require.Fail(t, "callback should have been called when config loaded")
}
require.True(t, wasCalled(called, 5*time.Second), "callback should have been called when config loaded")
})
}
@@ -786,11 +774,7 @@ func TestFileStoreWatcherEmitter(t *testing.T) {
require.NoError(t, err)
ioutil.WriteFile(path, cfgData, 0644)
select {
case <-called:
require.Fail(t, "callback should not have been called since watching disabled")
case <-time.After(1 * time.Second):
}
require.False(t, wasCalled(called, 1*time.Second), "callback should not have been called since watching disabled")
})
t.Run("enabled", func(t *testing.T) {
@@ -809,11 +793,7 @@ func TestFileStoreWatcherEmitter(t *testing.T) {
require.NoError(t, err)
ioutil.WriteFile(path, cfgData, 0644)
select {
case <-called:
case <-time.After(5 * time.Second):
require.Fail(t, "callback should have been called when config written")
}
require.True(t, wasCalled(called, 5*time.Second), "callback should have been called when config written")
})
}
@@ -1126,3 +1106,14 @@ func TestFileStoreString(t *testing.T) {
assert.Equal(t, "file://"+path, fs.String())
}
// wasCalled reports whether a given callback channel was called
// within the specified time duration or not.
func wasCalled(c chan bool, duration time.Duration) bool {
select {
case <-c:
return true
case <-time.After(duration):
}
return false
}