mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-17 11:00:09 +00:00
Fix another race condition in TestWebhook
There's the potential for a race condition where we try to read the logs buffer before the logs have been flushed by the webhook process. Kill the process to flush the logs before testing against the log buffer.
This commit is contained in:
parent
337621998e
commit
471c849c50
1 changed files with 28 additions and 20 deletions
|
@ -74,6 +74,9 @@ func TestWebhook(t *testing.T) {
|
|||
configPath, cleanupConfigFn := genConfig(t, hookecho, hookTmpl)
|
||||
defer cleanupConfigFn()
|
||||
|
||||
for _, tt := range hookHandlerTests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
|
||||
ip, port := serverAddress(t)
|
||||
args := []string{fmt.Sprintf("-hooks=%s", configPath), fmt.Sprintf("-ip=%s", ip), fmt.Sprintf("-port=%s", port), "-verbose"}
|
||||
|
||||
|
@ -91,9 +94,6 @@ func TestWebhook(t *testing.T) {
|
|||
|
||||
waitForServerReady(t, ip, port)
|
||||
|
||||
for _, tt := range hookHandlerTests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
|
||||
url := fmt.Sprintf("http://%s:%s/hooks/%s", ip, port, tt.id)
|
||||
|
||||
req, err := http.NewRequest("POST", url, ioutil.NopCloser(strings.NewReader(tt.body)))
|
||||
|
@ -130,15 +130,19 @@ func TestWebhook(t *testing.T) {
|
|||
}
|
||||
|
||||
if tt.logMatch == "" {
|
||||
b.Reset()
|
||||
return
|
||||
}
|
||||
|
||||
// There's the potential for a race condition below where we
|
||||
// try to read the logs buffer b before the logs have been
|
||||
// flushed by the webhook process. Kill the process to flush
|
||||
// the logs.
|
||||
killAndWait(cmd)
|
||||
|
||||
matched, _ := regexp.MatchString(tt.logMatch, b.String())
|
||||
if !matched {
|
||||
t.Fatalf("Unexpected log output:\n%s", b)
|
||||
t.Errorf("failed log match for %q (id: %s):\nmatch pattern: %q\ngot:\n%s", tt.desc, tt.id, tt.logMatch, b)
|
||||
}
|
||||
b.Reset()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -267,6 +271,10 @@ func waitForServer(t *testing.T, url string, status int, timeout time.Duration)
|
|||
}
|
||||
|
||||
func killAndWait(cmd *exec.Cmd) {
|
||||
if cmd == nil || cmd.ProcessState != nil && cmd.ProcessState.Exited() {
|
||||
return
|
||||
}
|
||||
|
||||
cmd.Process.Kill()
|
||||
cmd.Wait()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue