Fixes a race in the monitor endpoint test that would cause panics.

This commit is contained in:
James Phillips 2016-12-14 18:13:30 -08:00
parent 4ffd824547
commit 171ec6e487
No known key found for this signature in database
GPG Key ID: 77183E682AC5FC11
1 changed files with 22 additions and 48 deletions

View File

@ -1927,59 +1927,29 @@ func TestAgent_Monitor(t *testing.T) {
t.Fatalf("bad: %s", body)
}
// Begin streaming logs from the monitor endpoint
req, _ = http.NewRequest("GET", "/v1/agent/monitor?loglevel=debug", nil)
resp = newClosableRecorder()
go func() {
if _, err := srv.AgentMonitor(resp, req); err != nil {
t.Fatalf("err: %s", err)
}
}()
// Write the incoming logs from http to a channel for comparison
logCh := make(chan string, 5)
// Block until the first log entry from http
// Try to stream logs until we see the expected log line
expected := []byte("raft: Initial configuration (index=1)")
testutil.WaitForResult(func() (bool, error) {
line, err := resp.Body.ReadString('\n')
if err != nil && err != io.EOF {
return false, fmt.Errorf("err: %v", err)
}
if line == "" {
return false, fmt.Errorf("blank line")
}
logCh <- line
return true, nil
}, func(err error) {
t.Fatal(err)
})
req, _ = http.NewRequest("GET", "/v1/agent/monitor?loglevel=debug", nil)
resp = newClosableRecorder()
done := make(chan struct{})
go func() {
if _, err := srv.AgentMonitor(resp, req); err != nil {
t.Fatalf("err: %s", err)
}
close(done)
}()
go func() {
for {
line, err := resp.Body.ReadString('\n')
if err != nil && err != io.EOF {
t.Fatalf("err: %v", err)
}
if line != "" {
logCh <- line
}
}
}()
resp.Close()
<-done
// Wait until we see the expected log line
expected := "raft: Initial configuration (index=1)"
testutil.WaitForResult(func() (bool, error) {
select {
case log := <-logCh:
if !strings.Contains(log, expected) {
return false, fmt.Errorf("Log message does not match expected")
}
case <-time.After(10 * time.Second):
return false, fmt.Errorf("failed to get log within timeout")
if bytes.Contains(resp.Body.Bytes(), expected) {
return true, nil
} else {
return false, fmt.Errorf("didn't see expected")
}
return true, nil
}, func(err error) {
t.Fatal(err)
t.Fatalf("err: %v", err)
})
}
@ -1994,6 +1964,10 @@ func newClosableRecorder() *closableRecorder {
return &closableRecorder{r, closer}
}
func (r *closableRecorder) Close() {
close(r.closer)
}
func (r *closableRecorder) CloseNotify() <-chan bool {
return r.closer
}