waitForServer waits for a server to be ready
(ctx context.Context, t testing.TB, url string, timeout time.Duration)
| 442 | |
| 443 | // waitForServer waits for a server to be ready |
| 444 | func waitForServer(ctx context.Context, t testing.TB, url string, timeout time.Duration) error { |
| 445 | t.Helper() |
| 446 | client := &http.Client{Timeout: time.Second} |
| 447 | healthCtx, cancel := context.WithTimeout(ctx, timeout) |
| 448 | defer cancel() |
| 449 | |
| 450 | ticker := time.NewTicker(100 * time.Millisecond) |
| 451 | defer ticker.Stop() |
| 452 | |
| 453 | for { |
| 454 | select { |
| 455 | case <-healthCtx.Done(): |
| 456 | require.Failf(t, "failed to start server", "server at %s not ready within timeout: %w", url, healthCtx.Err()) |
| 457 | case <-ticker.C: |
| 458 | resp, err := client.Get(url) |
| 459 | if err == nil { |
| 460 | _ = resp.Body.Close() |
| 461 | return nil |
| 462 | } |
| 463 | t.Logf("Server not ready yet: %s", err) |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | func waitAgentAPIStable(ctx context.Context, t testing.TB, apiClient *agentapisdk.Client, waitFor time.Duration, msg string) error { |
| 469 | t.Helper() |