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

command/views: Fix flaky hook tests #36104

Merged
merged 1 commit into from
Dec 9, 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
10 changes: 9 additions & 1 deletion internal/command/views/hook_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package views
import (
"errors"
"fmt"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -497,7 +498,14 @@ func TestJSONHook_EphemeralOp_progress(t *testing.T) {
},
}

testJSONViewOutputEquals(t, done(t).Stdout(), want)
stdout := done(t).Stdout()

// time.Sleep can take longer than declared time
// so we only test the first lines we expect to see after sleeping
lines := strings.SplitN(stdout, "\n", 4)
firstLines := strings.Join(lines[:4], "\n")

testJSONViewOutputEquals(t, firstLines, want)
}

func TestJSONHook_EphemeralOp_error(t *testing.T) {
Expand Down
24 changes: 16 additions & 8 deletions internal/command/views/hook_ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestUiHookPreApply_periodicTimer(t *testing.T) {
t.Fatalf("Expected hook to continue, given: %#v", action)
}

time.Sleep(3100 * time.Millisecond)
time.Sleep(3005 * time.Millisecond)

// stop the background writer
uiState := h.resources[addr.String()]
Expand All @@ -143,7 +143,8 @@ test_instance.foo: Still modifying... [id=test, 3s elapsed]
`
result := done(t)
output := result.Stdout()
if output != expectedOutput {
// we do not test for equality because time.Sleep can take longer than declared time
if !strings.HasPrefix(output, expectedOutput) {
t.Fatalf("Output didn't match.\nExpected: %q\nGiven: %q", expectedOutput, output)
}

Expand Down Expand Up @@ -619,7 +620,9 @@ func TestUiHookEphemeralOp_progress(t *testing.T) {
t.Fatalf("Expected hook to continue, given: %#v", action)
}

start := time.Now()
time.Sleep(2005 * time.Millisecond)
elapsed := time.Since(start).Round(time.Second)

action, err = h.PostEphemeralOp(testUiHookResourceID(addr), plans.Open, nil)
if err != nil {
Expand All @@ -630,14 +633,19 @@ func TestUiHookEphemeralOp_progress(t *testing.T) {
}

result := done(t)
stdout := result.Stdout()

want := `ephemeral.test_instance.foo: Opening...
// we do not test for equality because time.Sleep can take longer than declared time
wantPrefix := `ephemeral.test_instance.foo: Opening...
ephemeral.test_instance.foo: Still opening... [1s elapsed]
ephemeral.test_instance.foo: Still opening... [2s elapsed]
ephemeral.test_instance.foo: Opening complete after 2s
`
if got := result.Stdout(); got != want {
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
ephemeral.test_instance.foo: Still opening... [2s elapsed]`
if !strings.HasPrefix(stdout, wantPrefix) {
t.Fatalf("unexpected prefix\n got: %q\nwant: %q", stdout, wantPrefix)
}
wantSuffix := fmt.Sprintf(`ephemeral.test_instance.foo: Opening complete after %s
`, elapsed)
if !strings.HasSuffix(stdout, wantSuffix) {
t.Fatalf("unexpected prefix\n got: %q\nwant: %q", stdout, wantSuffix)
}
}

Expand Down
Loading