diff --git a/config/watcher_test.go b/config/watcher_test.go index 40c925fc32..02b33683f7 100644 --- a/config/watcher_test.go +++ b/config/watcher_test.go @@ -11,6 +11,8 @@ import ( "time" "github.com/stretchr/testify/require" + + "github.com/mattermost/mattermost-server/utils/testutils" ) func TestWatcherInvalidDirectory(t *testing.T) { @@ -47,17 +49,9 @@ func TestWatcher(t *testing.T) { // Write to a different file ioutil.WriteFile(filepath.Join(tempDir, "unrelated"), []byte("data"), 0644) - select { - case <-called: - t.Fatal("callback should not have been called for unrelated file") - case <-time.After(1 * time.Second): - } + require.False(t, testutils.WasCalled(called, 1*time.Second), "callback should not have been called for unrelated file") // Write to the watched file ioutil.WriteFile(f.Name(), []byte("data"), 0644) - select { - case <-called: - case <-time.After(5 * time.Second): - t.Fatal("callback should have been called when file written") - } + require.True(t, testutils.WasCalled(called, 5*time.Second), "callback should have been called when file written") } diff --git a/utils/testutils/testutils.go b/utils/testutils/testutils.go index 4f56c51ebd..6664b8a7ae 100644 --- a/utils/testutils/testutils.go +++ b/utils/testutils/testutils.go @@ -8,6 +8,7 @@ import ( "io" "os" "path/filepath" + "time" "github.com/mattermost/mattermost-server/utils/fileutils" ) @@ -27,3 +28,15 @@ func ReadTestFile(name string) ([]byte, error) { return data.Bytes(), nil } } + +// 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 { + wasCalled := false + select { + case <-c: + wasCalled = true + case <-time.After(duration): + } + return wasCalled +}