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.
This commit is contained in:
Julius Volz 2015-08-26 02:58:59 +02:00
parent 6664b77f36
commit 3fd5826589
2 changed files with 5 additions and 4 deletions

View file

@ -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. // updated target groups through the channel.
func (fd *FileDiscovery) refresh(ch chan<- *config.TargetGroup) { func (fd *FileDiscovery) refresh(ch chan<- *config.TargetGroup) {
ref := map[string]int{} ref := map[string]int{}

View file

@ -30,7 +30,6 @@ func testFileSD(t *testing.T, ext string) {
done = make(chan struct{}) done = make(chan struct{})
) )
go fsd.Run(ch, done) go fsd.Run(ch, done)
defer close(done)
select { select {
case <-time.After(25 * time.Millisecond): 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.). // 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 // We have to drain those (as the target manager would) to avoid deadlocking and must
// not try to make sense of it all... // not try to make sense of it all...
drained := make(chan struct{})
go func() { go func() {
for tg := range ch { for tg := range ch {
// Below we will change the file to a bad syntax. Previously extracted target // 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) t.Errorf("Unexpected empty target group received: %s", tg)
} }
} }
close(drained)
}() }()
newf, err = os.Create("fixtures/_test.new") 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) os.Rename(newf.Name(), "fixtures/_test"+ext)
// Give notifcations some time to arrive. close(done)
time.Sleep(50 * time.Millisecond) <-drained
} }