-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_query_limits_test.go
87 lines (70 loc) · 2.09 KB
/
client_query_limits_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package fauna_test
import (
"os"
"sync"
"testing"
"github.com/fauna/fauna-go/v3"
"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)
}
})
}