From 1005ef0a709e35e1680cc3afa2e0576ff94cbbe0 Mon Sep 17 00:00:00 2001 From: Krasi Georgiev Date: Mon, 13 Nov 2017 11:38:46 +0000 Subject: [PATCH] Fix flaky file discovery tests - sync the channel draining goroutine --- discovery/file/file_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/discovery/file/file_test.go b/discovery/file/file_test.go index 4106c5f1fa..2305a0684a 100644 --- a/discovery/file/file_test.go +++ b/discovery/file/file_test.go @@ -62,12 +62,14 @@ func testFileSD(t *testing.T, prefix, ext string, expect bool) { // To avoid empty group struct sent from the discovery caused by invalid fsnotify updates, // drain the channel until we are ready with the test files. - filesReady := make(chan struct{}) + fileReady := make(chan struct{}) + drainReady := make(chan struct{}) go func() { for { select { case <-ch: - case <-filesReady: + case <-fileReady: + close(drainReady) return } } @@ -77,7 +79,6 @@ func testFileSD(t *testing.T, prefix, ext string, expect bool) { if err != nil { t.Fatal(err) } - defer newf.Close() f, err := os.Open(filepath.Join(testDir, prefix+ext)) if err != nil { @@ -91,7 +92,8 @@ func testFileSD(t *testing.T, prefix, ext string, expect bool) { // File is written with the config so stop draining the discovery channel. // It needs to be before the file closing so that fsnotify triggers a new loop of the discovery service. - close(filesReady) + close(fileReady) + <-drainReady newf.Close() timeout := time.After(15 * time.Second)