Skip to content

Commit

Permalink
wait for server to be ready before testing
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTheElder committed Apr 20, 2022
1 parent d4736f6 commit 7a8ff9d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cmd/archeio/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ package main

import (
"context"
"net/http"
"os"
"os/exec"
"testing"
"time"

"sigs.k8s.io/oci-proxy/internal/integration"
)
Expand Down Expand Up @@ -62,11 +64,17 @@ func TestIntegrationMain(t *testing.T) {
serverErrChan <- serverCmd.Wait()
}()

// TODO: wait for readyz or similar ...
// wait for server to be up and running
startErr := <-serverErrChan
if startErr != nil {
t.Fatalf("Failed to start archeio: %v", err)
}
if !tryUntil(time.Now().Add(time.Second), func() bool {
_, err := http.Get("http://" + testAddr + "/v2/")
return err == nil
}) {
t.Fatal("timed out waiting for archeio to be ready")
}

// TODO: fake being on AWS
testPull := func(image string) {
Expand Down Expand Up @@ -94,3 +102,15 @@ func TestIntegrationMain(t *testing.T) {
t.Fatalf("archeio did not exit cleanly: %v", err)
}
}

// helper that calls `try()`` in a loop until the deadline `until`
// has passed or `try()`returns true, returns whether try ever returned true
func tryUntil(until time.Time, try func() bool) bool {
for until.After(time.Now()) {
if try() {
return true
}
time.Sleep(time.Millisecond * 10)
}
return false
}

0 comments on commit 7a8ff9d

Please sign in to comment.