Fix flaky file discovery tests - sync the channel draining goroutine

This commit is contained in:
Krasi Georgiev 2017-11-13 11:38:46 +00:00
parent 3cc01a3088
commit 1005ef0a70

View file

@ -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, // 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. // 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() { go func() {
for { for {
select { select {
case <-ch: case <-ch:
case <-filesReady: case <-fileReady:
close(drainReady)
return return
} }
} }
@ -77,7 +79,6 @@ func testFileSD(t *testing.T, prefix, ext string, expect bool) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer newf.Close()
f, err := os.Open(filepath.Join(testDir, prefix+ext)) f, err := os.Open(filepath.Join(testDir, prefix+ext))
if err != nil { 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. // 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. // 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() newf.Close()
timeout := time.After(15 * time.Second) timeout := time.After(15 * time.Second)