Skip to content

Commit 4a678dd

Browse files
authored
fix: add test util to wait for healthy piri (#306)
Aims to fix: ``` === RUN TestFXSpaceContentRetrieve/space/content/retrieve ucan_fx_test.go:106: Error Trace: /Users/runner/work/piri/piri/pkg/service/retrieval/ucan_fx_test.go:106 Error: Received unexpected error: sending message: doing HTTP request: Get "http://localhost:49232/piece/bafkreibmtmy3p773cf7uj24hfg2dhniov45xv4p54cs4oe3nxhm7p3ov5e": dial tcp [::1]:49232: connect: connection refused ``` Possibly because the server is not ready to accept connections yet.
1 parent 579a25a commit 4a678dd

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

pkg/internal/testutil/environment.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package testutil
22

33
import (
44
"net"
5+
"net/http"
6+
"net/url"
57
"os"
68
"testing"
9+
"time"
710

811
docker_client "github.com/docker/docker/client"
912
"github.com/stretchr/testify/require"
@@ -44,3 +47,20 @@ func IsDockerAvailable(t testing.TB) bool {
4447
}
4548
return true
4649
}
50+
51+
// WaitForHealthy waits for the URL to return HTTP 200 for up to 10 seconds.
52+
func WaitForHealthy(t testing.TB, url *url.URL) {
53+
t.Helper()
54+
start := time.Now()
55+
for i := 0; i < 100; i++ {
56+
resp, err := http.DefaultClient.Get(url.String())
57+
if err == nil {
58+
resp.Body.Close()
59+
if resp.StatusCode == http.StatusOK {
60+
return
61+
}
62+
}
63+
time.Sleep(time.Millisecond * 100)
64+
}
65+
t.Fatalf("%s was not healthy after %s", url.String(), time.Since(start).String())
66+
}

pkg/service/retrieval/ucan_fx_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestFXSpaceContentRetrieve(t *testing.T) {
3939

4040
testApp.RequireStart()
4141
defer testApp.RequireStop()
42+
piritestutil.WaitForHealthy(t, &appConfig.Server.PublicURL)
4243

4344
t.Run("space/content/retrieve", func(t *testing.T) {
4445
space := testutil.RandomSigner(t)
@@ -141,6 +142,7 @@ func TestFXBlobRetrieve(t *testing.T) {
141142

142143
testApp.RequireStart()
143144
defer testApp.RequireStop()
145+
piritestutil.WaitForHealthy(t, &appConfig.Server.PublicURL)
144146

145147
t.Run("blob/retrieve", func(t *testing.T) {
146148
randBytes := testutil.RandomBytes(t, 256)

pkg/service/storage/ucan_access_fx_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestFXAccessGrant(t *testing.T) {
5555

5656
testApp.RequireStart()
5757
defer testApp.RequireStop()
58+
piritestutil.WaitForHealthy(t, &appConfig.Server.PublicURL)
5859

5960
channel := http.NewChannel(&appConfig.Server.PublicURL)
6061
conn, err := client.NewConnection(granter, channel)

0 commit comments

Comments
 (0)