From 20d0bf4d65c9e6aa5ececb6aee28866f21dce62f Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Wed, 26 Aug 2015 02:58:59 +0200 Subject: [PATCH] Fix flakey FileSD test. When the test ends, all files matching the watcher's glob are removed via defer. In that moment, the draining goroutine may still be running and then detect no files matching the configured glob just before the test exits. This is now solved by waiting for the draining goroutine to finish before leaving the test function and thus causing the deferred file removal. --- retrieval/discovery/file.go | 2 +- retrieval/discovery/file_test.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/retrieval/discovery/file.go b/retrieval/discovery/file.go index e9c3b410b0..b29b14e7e8 100644 --- a/retrieval/discovery/file.go +++ b/retrieval/discovery/file.go @@ -160,7 +160,7 @@ func (fd *FileDiscovery) Run(ch chan<- *config.TargetGroup, done <-chan struct{} } } -// refresh reads all files matching the discoveries patterns and sends the respective +// refresh reads all files matching the discovery's patterns and sends the respective // updated target groups through the channel. func (fd *FileDiscovery) refresh(ch chan<- *config.TargetGroup) { ref := map[string]int{} diff --git a/retrieval/discovery/file_test.go b/retrieval/discovery/file_test.go index cd541e5584..0335013b1e 100644 --- a/retrieval/discovery/file_test.go +++ b/retrieval/discovery/file_test.go @@ -30,7 +30,6 @@ func testFileSD(t *testing.T, ext string) { done = make(chan struct{}) ) go fsd.Run(ch, done) - defer close(done) select { case <-time.After(25 * time.Millisecond): @@ -81,6 +80,7 @@ func testFileSD(t *testing.T, ext string) { // some runs (which might be empty, chains of different operations etc.). // We have to drain those (as the target manager would) to avoid deadlocking and must // not try to make sense of it all... + drained := make(chan struct{}) go func() { for tg := range ch { // Below we will change the file to a bad syntax. Previously extracted target @@ -89,6 +89,7 @@ func testFileSD(t *testing.T, ext string) { t.Errorf("Unexpected empty target group received: %s", tg) } } + close(drained) }() newf, err = os.Create("fixtures/_test.new") @@ -104,6 +105,6 @@ func testFileSD(t *testing.T, ext string) { os.Rename(newf.Name(), "fixtures/_test"+ext) - // Give notifcations some time to arrive. - time.Sleep(50 * time.Millisecond) + close(done) + <-drained }