-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
f0d039a
commit a5515a8
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package fauna_test | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/fauna/fauna-go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClientRetriesWithQueryLimits(t *testing.T) { | ||
t.Run("Query limits succeed on retry", func(t *testing.T) { | ||
dbName, dbNameSet := os.LookupEnv("QUERY_LIMITS_DB") | ||
collName, collNameSet := os.LookupEnv("QUERY_LIMITS_COLL") | ||
|
||
// If run in a pipeline, these will be empty strings, so check both | ||
if (!dbNameSet || !collNameSet) || | ||
(dbName == "" || collName == "") { | ||
t.Skip("Skipping query limits test due to missing env var(s)") | ||
} | ||
|
||
if _, found := os.LookupEnv(fauna.EnvFaunaSecret); !found { | ||
t.Setenv(fauna.EnvFaunaSecret, "secret") | ||
} | ||
|
||
client, clientErr := fauna.NewDefaultClient() | ||
if !assert.NoError(t, clientErr) { | ||
return | ||
} | ||
|
||
type secretObj struct { | ||
Secret string `fauna:"secret"` | ||
} | ||
|
||
query, _ := fauna.FQL(` | ||
if (Database.byName(${dbName}).exists()) { | ||
Key.create({ role: "admin", database: ${dbName} }) { secret } | ||
} else { | ||
abort("Database not found.") | ||
}`, map[string]any{"dbName": dbName}) | ||
|
||
res, queryErr := client.Query(query) | ||
if !assert.NoError(t, queryErr) { | ||
t.FailNow() | ||
} | ||
|
||
var secret secretObj | ||
marshalErr := res.Unmarshal(&secret) | ||
if assert.NoError(t, marshalErr) { | ||
clients := make([]*fauna.Client, 5) | ||
results := make(chan int, len(clients)) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(len(clients)) | ||
|
||
for i := range clients { | ||
clients[i] = fauna.NewClient(secret.Secret, fauna.DefaultTimeouts(), fauna.URL(os.Getenv(fauna.EnvFaunaEndpoint))) | ||
|
||
go func(collName string, client *fauna.Client, result chan int) { | ||
defer wg.Done() | ||
coll, _ := fauna.FQL(collName, nil) | ||
q, _ := fauna.FQL(`${coll}.all().paginate(50)`, map[string]any{"coll": coll}) | ||
res, err := client.Query(q) | ||
if err != nil { | ||
result <- -1 | ||
} else { | ||
result <- res.Stats.Attempts | ||
} | ||
}(collName, clients[i], results) | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(results) | ||
}() | ||
|
||
throttled := false | ||
|
||
for result := range results { | ||
throttled = throttled || result > 1 | ||
} | ||
|
||
assert.True(t, throttled) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
version: "3.8" | ||
|
||
networks: | ||
limit-net: | ||
external: true | ||
name: limit-net | ||
|
||
services: | ||
query-limits-tests: | ||
image: golang:1.21-alpine | ||
entrypoint: ["/bin/sh", "-c"] | ||
volumes: | ||
- "../..:/tmp/app" | ||
working_dir: "/tmp/app" | ||
environment: | ||
FAUNA_ENDPOINT: ${FAUNA_ENDPOINT:-http://fauna-limits:8443} | ||
QUERY_LIMITS_DB: ${QUERY_LIMITS_DB} | ||
QUERY_LIMITS_COLL: ${QUERY_LIMITS_COLL} | ||
networks: | ||
- limit-net | ||
command: | ||
- | | ||
apk add build-base | ||
CGO_ENABLED=0 go build . | ||
go test -v client_query_limits_test.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
platform: linux | ||
image_resource: | ||
type: registry-image | ||
source: | ||
repository: shared-concourse-dind | ||
aws_access_key_id: ((prod-images-aws-access-key-id)) | ||
aws_secret_access_key: ((prod-images-aws-secret-key)) | ||
aws_region: us-east-2 | ||
|
||
params: | ||
FAUNA_ENDPOINT: http://fauna-limits:8443 | ||
QUERY_LIMITS_DB: | ||
QUERY_LIMITS_COLL: | ||
|
||
inputs: | ||
- name: fauna-go-repository | ||
- name: testtools-repo | ||
|
||
run: | ||
path: entrypoint.sh | ||
args: | ||
- bash | ||
- -ceu | ||
- | | ||
# setup Fauna container | ||
docker-compose -f testtools-repo/fauna-driver-query-limits-tests/docker-compose.yml run setup | ||
# run tests | ||
docker-compose -f fauna-go-repository/concourse/scripts/docker-compose-fauna-limits.yml run query-limits-tests | ||
# stop and remove containers | ||
docker-compose -f fauna-go-repository/concourse/scripts/docker-compose-fauna-limits.yml down | ||
docker-compose -f testtools-repo/fauna-driver-query-limits-tests/docker-compose.yml down |