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: flaky test should also wait for the proxy to be available #480

Merged
merged 2 commits into from
Jan 31, 2024
Merged
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
46 changes: 32 additions & 14 deletions cmd/templ/generatecmd/testwatch/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,15 @@ func Setup() (args TestArgs, teardown func(t *testing.T), err error) {
}()

// Wait for server to start.
var tries int
for {
if tries > 5 {
cancel()
wg.Wait()
return args, teardown, fmt.Errorf("failed to start server after 5 tries")
}
tries++
_, err = getHTML(args.ProxyURL)
if err != nil {
time.Sleep(time.Second)
continue
}
break
if err = waitForUrl(args.AppURL); err != nil {
cancel()
wg.Wait()
return args, teardown, fmt.Errorf("failed to start app server: %v", err)
}
if err = waitForUrl(args.ProxyURL); err != nil {
cancel()
wg.Wait()
return args, teardown, fmt.Errorf("failed to start proxy server: %v", err)
}

// Wait for exit.
Expand All @@ -334,3 +329,26 @@ func Setup() (args TestArgs, teardown func(t *testing.T), err error) {
}
return args, teardown, err
}

func waitForUrl(url string) (err error) {
var tries int
for {
time.Sleep(time.Second)
if tries > 5 {
return err
}
tries++
var resp *http.Response
resp, err = http.Get(url)
if err != nil {
fmt.Printf("failed to get %q: %v\n", url, err)
continue
}
if resp.StatusCode != http.StatusOK {
fmt.Printf("failed to get %q: %v\n", url, err)
err = fmt.Errorf("expected status code %d, got %d", http.StatusOK, resp.StatusCode)
continue
}
return nil
}
}
Loading