package main import ( "context" "fmt" "net/http" "os" "testing" "time" ) func Test_main(t *testing.T) { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) t.Cleanup(cancel) args := map[string]string{} go run(ctx, os.Stdout, args) // wait for the server to become available waitForReady(ctx, 10*time.Second, "http://localhost:3333/healthz") // do tests fmt.Println("Tests starting") } func waitForReady( ctx context.Context, timeout time.Duration, endpoint string, ) error { client := http.Client{} startTime := time.Now() for { req, err := http.NewRequestWithContext( ctx, http.MethodGet, endpoint, nil, ) if err != nil { return fmt.Errorf("failed to create request: %w", err) } resp, err := client.Do(req) if err != nil { fmt.Printf("Error making request: %s\n", err.Error()) continue } if resp.StatusCode == http.StatusOK { fmt.Println("Endpoint is ready!") resp.Body.Close() return nil } resp.Body.Close() select { case <-ctx.Done(): return ctx.Err() default: if time.Since(startTime) >= timeout { return fmt.Errorf("timeout reached while waiting for endpoint") } // wait a little while between checks time.Sleep(250 * time.Millisecond) } } }