From 7946e9070d8159788016af8144a868c61805c923 Mon Sep 17 00:00:00 2001 From: Ichinose Shogo Date: Sat, 15 Jan 2022 15:29:51 +0900 Subject: [PATCH] fix go vet warnings: call to (*T).Fatalf from a non-test goroutine --- inotify_poller_test.go | 6 +++++- inotify_test.go | 9 +++++++-- integration_test.go | 18 +++++++++++++++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/inotify_poller_test.go b/inotify_poller_test.go index c7475454..110e00db 100644 --- a/inotify_poller_test.go +++ b/inotify_poller_test.go @@ -179,7 +179,7 @@ func TestPollerConcurrent(t *testing.T) { for { ok, err := poller.wait() if err != nil { - t.Fatalf("poller failed: %v", err) + t.Errorf("poller failed: %v", err) } oks <- ok if !<-live { @@ -227,4 +227,8 @@ func TestPollerConcurrent(t *testing.T) { t.Fatalf("expected true") } tfd.get(t) + + // wait for all goroutines for finish. + live <- false + <-oks } diff --git a/inotify_test.go b/inotify_test.go index 90c82b8e..f121c014 100644 --- a/inotify_test.go +++ b/inotify_test.go @@ -330,15 +330,17 @@ func TestInotifyInnerMapLength(t *testing.T) { if err != nil { t.Fatalf("Failed to create watcher: %v", err) } - defer w.Close() err = w.Add(testFile) if err != nil { t.Fatalf("Failed to add testFile: %v", err) } + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() for err := range w.Errors { - t.Fatalf("error received: %s", err) + t.Errorf("error received: %s", err) } }() @@ -357,6 +359,9 @@ func TestInotifyInnerMapLength(t *testing.T) { if len(w.paths) != 0 { t.Fatalf("Expected paths len is 0, but got: %d, %v", len(w.paths), w.paths) } + + w.Close() + wg.Wait() } func TestInotifyOverflow(t *testing.T) { diff --git a/integration_test.go b/integration_test.go index fc09e03f..85d7b9bb 100644 --- a/integration_test.go +++ b/integration_test.go @@ -14,6 +14,7 @@ import ( "path" "path/filepath" "runtime" + "sync" "sync/atomic" "testing" "time" @@ -75,9 +76,12 @@ func TestFsnotifyMultipleOperations(t *testing.T) { watcher := newWatcher(t) // Receive errors on the error channel on a separate goroutine + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() for err := range watcher.Errors { - t.Fatalf("error received: %s", err) + t.Errorf("error received: %s", err) } }() @@ -187,6 +191,9 @@ func TestFsnotifyMultipleOperations(t *testing.T) { case <-time.After(2 * time.Second): t.Fatal("event stream was not closed after 2 seconds") } + + // wait for all groutines to finish. + wg.Wait() } func TestFsnotifyMultipleCreates(t *testing.T) { @@ -837,10 +844,13 @@ func TestRemovalOfWatch(t *testing.T) { t.Fatalf("Could not remove the watch: %v\n", err) } + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() select { case ev := <-watcher.Events: - t.Fatalf("We received event: %v\n", ev) + t.Errorf("We received event: %v\n", ev) case <-time.After(500 * time.Millisecond): t.Log("No event received, as expected.") } @@ -858,7 +868,9 @@ func TestRemovalOfWatch(t *testing.T) { if err := os.Chmod(testFileAlreadyExists, 0700); err != nil { t.Fatalf("chmod failed: %s", err) } - time.Sleep(400 * time.Millisecond) + + // wait for all groutines to finish. + wg.Wait() } func TestFsnotifyAttrib(t *testing.T) {