Fix race issue in monitor endpoint test

This commit is contained in:
Kyle Havlovitz 2016-12-01 12:34:21 -05:00
parent 98adcfb17d
commit ead30ca62b
1 changed files with 11 additions and 13 deletions

View File

@ -1,7 +1,6 @@
package agent package agent
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -1145,8 +1144,7 @@ func TestHTTPAgentRegisterServiceCheck(t *testing.T) {
func TestHTTPAgent_Monitor(t *testing.T) { func TestHTTPAgent_Monitor(t *testing.T) {
logWriter := logger.NewLogWriter(512) logWriter := logger.NewLogWriter(512)
expectedLogs := bytes.Buffer{} logger := io.MultiWriter(os.Stdout, logWriter)
logger := io.MultiWriter(os.Stdout, &expectedLogs, logWriter)
dir, srv := makeHTTPServerWithConfigLog(t, nil, logger, logWriter) dir, srv := makeHTTPServerWithConfigLog(t, nil, logger, logWriter)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
@ -1206,21 +1204,21 @@ func TestHTTPAgent_Monitor(t *testing.T) {
} }
}() }()
// Verify that the first 5 logs we get match the expected stream // Wait until we see the expected log line
for i := 0; i < 5; i++ { expected := "raft: Initial configuration (index=1)"
testutil.WaitForResult(func() (bool, error) {
select { select {
case log := <-logCh: case log := <-logCh:
expected, err := expectedLogs.ReadString('\n') if !strings.Contains(log, expected) {
if err != nil { return false, fmt.Errorf("Log message does not match expected")
t.Fatalf("err: %v", err)
}
if log != expected {
t.Fatalf("bad: %q %q", expected, log)
} }
case <-time.After(10 * time.Second): case <-time.After(10 * time.Second):
t.Fatalf("failed to get log within timeout") return false, fmt.Errorf("failed to get log within timeout")
}
} }
return true, nil
}, func(err error) {
t.Fatal(err)
})
} }
type closableRecorder struct { type closableRecorder struct {