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.
Этот коммит содержится в:
коммит произвёл
Miguel de la Cruz
родитель
013a81a33d
Коммит
8ab0e80b77
@@ -11,6 +11,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/mattermost/mattermost-server/utils/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWatcherInvalidDirectory(t *testing.T) {
|
func TestWatcherInvalidDirectory(t *testing.T) {
|
||||||
@@ -47,17 +49,9 @@ func TestWatcher(t *testing.T) {
|
|||||||
|
|
||||||
// Write to a different file
|
// Write to a different file
|
||||||
ioutil.WriteFile(filepath.Join(tempDir, "unrelated"), []byte("data"), 0644)
|
ioutil.WriteFile(filepath.Join(tempDir, "unrelated"), []byte("data"), 0644)
|
||||||
select {
|
require.False(t, testutils.WasCalled(called, 1*time.Second), "callback should not have been called for unrelated file")
|
||||||
case <-called:
|
|
||||||
t.Fatal("callback should not have been called for unrelated file")
|
|
||||||
case <-time.After(1 * time.Second):
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write to the watched file
|
// Write to the watched file
|
||||||
ioutil.WriteFile(f.Name(), []byte("data"), 0644)
|
ioutil.WriteFile(f.Name(), []byte("data"), 0644)
|
||||||
select {
|
require.True(t, testutils.WasCalled(called, 5*time.Second), "callback should have been called when file written")
|
||||||
case <-called:
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("callback should have been called when file written")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/utils/fileutils"
|
"github.com/mattermost/mattermost-server/utils/fileutils"
|
||||||
)
|
)
|
||||||
@@ -27,3 +28,15 @@ func ReadTestFile(name string) ([]byte, error) {
|
|||||||
return data.Bytes(), nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user