Skip to content

Commit

Permalink
fix: add check to server start test
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Vazquez <[email protected]>
  • Loading branch information
austinvazquez committed Jun 19, 2024
1 parent 3749402 commit dcb19f8
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,49 @@ func Run(o *RunOption) {
io.WriteString(w, response) //nolint:errcheck,gosec // Function call in server handler for testing only.
})
hostPort := fnet.GetFreePort()
s := http.Server{Addr: fmt.Sprintf(":%d", hostPort), Handler: mux, ReadTimeout: 30 * time.Second}
serverAddress := fmt.Sprintf(":%d", hostPort)
s := http.Server{Addr: serverAddress, Handler: mux, ReadTimeout: 30 * time.Second}
go s.ListenAndServe() //nolint:errcheck // Asynchronously starting server for testing only.

time.Sleep(5 * time.Second)
upChan := make(chan struct{})
timeoutChan := make(chan struct{})
go func() {
verifyServerIsUp := func() error {
client := http.Client{}
resp, err := client.Get(serverAddress)
if err != nil {
return fmt.Errorf("server has not started: %w", err)
}
defer resp.Body.Close()

Check failure on line 403 in tests/run.go

View workflow job for this annotation

GitHub Actions / go-linter

Error return value of `resp.Body.Close` is not checked (errcheck)
return nil
}

attempt := 0
for {
select {
case <-timeoutChan:
return
default:
}

err := verifyServerIsUp()
if err != nil {
close(upChan)
return
}
attempt += 1

Check failure on line 420 in tests/run.go

View workflow job for this annotation

GitHub Actions / go-linter

increment-decrement: should replace attempt += 1 with attempt++ (revive)
fmt.Fprintf(ginkgo.GinkgoWriter, "Try server %d: %v", attempt, err)
time.Sleep(5 * time.Second)
}
}()

select {
case <-time.After(30 * time.Second):
close(timeoutChan)
ginkgo.Fail("Server failed to start after 30 seconds")
case <-upChan:
}

ginkgo.DeferCleanup(s.Shutdown, ctx)
command.Run(o.BaseOpt, "run", "-d", "--name", testContainerName, "--add-host", "test-host:host-gateway",
localImages[amazonLinux2Image], "sleep", "infinity")
Expand Down

0 comments on commit dcb19f8

Please sign in to comment.