From 165aaf8de3dfa0cb7d69b3853dc9ad2be0f9b50d Mon Sep 17 00:00:00 2001 From: Tyson Trautmann Date: Tue, 14 Feb 2023 22:54:53 -0800 Subject: [PATCH] Adding wait for database to respond before tests. --- faunadb/faunadb_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/faunadb/faunadb_test.go b/faunadb/faunadb_test.go index f00735a5..e1171578 100644 --- a/faunadb/faunadb_test.go +++ b/faunadb/faunadb_test.go @@ -1,6 +1,7 @@ package faunadb import ( + "errors" "fmt" "math/rand" "os" @@ -9,6 +10,10 @@ import ( "time" ) +const ( + maxWaitSeconds = 10 +) + var ( faunaSecret = os.Getenv("FAUNA_ROOT_KEY") faunaEndpoint = os.Getenv("FAUNA_ENDPOINT") @@ -40,6 +45,17 @@ func RandomStartingWith(parts ...string) string { return fmt.Sprintf("%s%v", strings.Join(parts, ""), rand.Uint32()) } +func WaitForDB(client *FaunaClient) error { + for i := 0; i < maxWaitSeconds*1000; i += 100 { + res, err := client.Query(NewId()) + if res != nil && err == nil { + return nil + } + time.Sleep(100 * time.Millisecond) + } + return errors.New(fmt.Sprintf("database didn't send valid response after %d seconds", maxWaitSeconds)) +} + func SetupTestDB() (client *FaunaClient, err error) { var key Value @@ -47,6 +63,12 @@ func SetupTestDB() (client *FaunaClient, err error) { faunaSecret, Endpoint(faunaEndpoint), ) + + err = WaitForDB(adminClient) + if err != nil { + return nil, err + } + if allQueriesTimeout != "" { if millis, err := strconv.ParseUint(allQueriesTimeout, 10, 64); err == nil { adminClient = NewFaunaClient(