MM-19022: Convert config/watcher_test.go t.Fatal calls into require calls (#12524)

* Convert config/watcher_test.go t.Fatal calls into require calls

* Moved wasCalled to a public helper function

Now the testutils package has a new function WasCalled
that can be used by anywhere.
Этот коммит содержится в:
Agniva De Sarker
2019-10-02 22:27:43 +05:30
коммит произвёл Miguel de la Cruz
родитель 013a81a33d
Коммит 8ab0e80b77
2 изменённых файлов: 17 добавлений и 10 удалений

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

@@ -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")
}

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

@@ -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
}