Skip to content

Commit

Permalink
fix: TestRunBinary should wait for std output
Browse files Browse the repository at this point in the history
  • Loading branch information
clement2026 committed Sep 6, 2024
1 parent 4bd1962 commit 94058d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Binary file modified assets/web/html.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/e2e/run_binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestRunBinary(t *testing.T) {
c.Start()
defer c.Stop()

WaitForRunningOrFail(t, c)
WaitForRunningOrFail(t, c, true)

assert.True(t, LogContainsText(c.Status(), "listening on localhost:12345") ||
LogContainsText(c.Status(), "listening on 127.0.0.1:12345"), WhatsWrong(c.Status()))
Expand Down
37 changes: 25 additions & 12 deletions tests/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func FindBinary(t *testing.T) (string, error) {
}

func IsRunning(s cmd.Status) bool {
return s.Error == nil && s.StartTs > 0 && !s.Complete && s.Exit == 0
return s.Error == nil && s.StartTs > 0 && !s.Complete && s.Exit == -1
}

func LogContainsText(s cmd.Status, text string) bool {
Expand Down Expand Up @@ -75,17 +75,30 @@ func WhatsWrong(s cmd.Status) error {
return errors.New("debug info: " + string(marshal))
}

func WaitForRunningOrFail(t *testing.T, c *cmd.Cmd) {
timer := time.NewTimer(3 * time.Second)
ticker := time.NewTimer(100 * time.Millisecond)

select {
case <-timer.C:
t.Fatalf("failed to start: %v", WhatsWrong(c.Status()))
case <-ticker.C:
status := c.Status()
if IsRunning(status) {
break
// WaitForRunningOrFail checks if stdout or stderr has content when mustWaitForStdOutput is true.
// mustWaitForStdOutput is useful when the OS performs security checks on the binary, which can be
// terribly slow on macOS.
func WaitForRunningOrFail(t *testing.T, c *cmd.Cmd, mustWaitForStdOutput bool) {
timer := time.NewTimer(100 * time.Second)
ticker := time.NewTicker(100 * time.Millisecond)
defer timer.Stop()
defer ticker.Stop()

for {
select {
case <-timer.C:
t.Fatalf("failed to start: %v", WhatsWrong(c.Status()))
case <-ticker.C:
status := c.Status()
if IsRunning(status) {
if mustWaitForStdOutput {
if len(status.Stdout) > 0 || len(status.Stderr) > 0 {
return
}
} else {
return
}
}
}
}

Expand Down

0 comments on commit 94058d0

Please sign in to comment.