Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions faunadb/faunadb_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package faunadb

import (
"errors"
"fmt"
"math/rand"
"os"
Expand All @@ -9,6 +10,10 @@ import (
"time"
)

const (
maxWaitSeconds = 10
)

var (
faunaSecret = os.Getenv("FAUNA_ROOT_KEY")
faunaEndpoint = os.Getenv("FAUNA_ENDPOINT")
Expand Down Expand Up @@ -40,13 +45,30 @@ 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

adminClient = NewFaunaClient(
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(
Expand Down