Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TestRunBinary should wait for std output #17

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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