Skip to content

Commit 6588c04

Browse files
authored
[Bugfix] Add proper wait function in tests (#324)
1 parent 721038b commit 6588c04

11 files changed

+76
-84
lines changed

revision.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type RevisionRanges struct {
4646
// RevisionTreeNode is a leaf in Merkle tree with hashed Revisions and with count of documents in the leaf
4747
type RevisionTreeNode struct {
4848
Hash uint64 `json:"hash"`
49-
Count uint64 `json:"count,int"`
49+
Count uint64 `json:"count,int"`
5050
}
5151

5252
// RevisionTree is a list of Revisions in a Merkle tree

test/client_test.go

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ package test
2525
import (
2626
"context"
2727
"crypto/tls"
28-
"fmt"
2928
"log"
3029
httplib "net/http"
3130
"os"
@@ -196,7 +195,7 @@ func createConnectionFromEnv(t testEnv) driver.Connection {
196195
}
197196

198197
// createClientFromEnv initializes a Client from information specified in environment variables.
199-
func createClientFromEnv(t testEnv, waitUntilReady bool, connection ...*driver.Connection) driver.Client {
198+
func createClientFromEnv(t testEnv, waitUntilReady bool) driver.Client {
200199
runPProfServerOnce.Do(func() {
201200
if os.Getenv("TEST_PPROF") != "" {
202201
go func() {
@@ -216,16 +215,14 @@ func createClientFromEnv(t testEnv, waitUntilReady bool, connection ...*driver.C
216215
conn = wrappers.NewLoggerConnection(conn, wrappers.NewZeroLogLogger(l), true)
217216
}
218217

219-
if len(connection) == 1 {
220-
*connection[0] = conn
221-
}
222218
c, err := driver.NewClient(driver.ClientConfig{
223219
Connection: conn,
224220
Authentication: createAuthenticationFromEnv(t),
225221
})
226222
if err != nil {
227223
t.Fatalf("Failed to create new client: %s", describe(err))
228224
}
225+
229226
if waitUntilReady {
230227
timeout := time.Minute
231228
ctx, cancel := context.WithTimeout(context.Background(), timeout)
@@ -250,39 +247,42 @@ func createClientFromEnv(t testEnv, waitUntilReady bool, connection ...*driver.C
250247

251248
// waitUntilServerAvailable keeps waiting until the server/cluster that the client is addressing is available.
252249
func waitUntilServerAvailable(ctx context.Context, c driver.Client, t testEnv) error {
253-
instanceUp := make(chan error)
254-
go func() {
255-
for {
256-
verCtx, cancel := context.WithTimeout(ctx, time.Second*5)
257-
if _, err := c.Version(verCtx); err == nil {
258-
259-
// check if leader challenge is ongoing
260-
if _, err := c.ServerRole(verCtx); err != nil {
261-
if !driver.IsNoLeaderOrOngoing(err) {
262-
cancel()
263-
instanceUp <- err
264-
return
265-
}
266-
//t.Logf("Retry. Waiting for leader: %s", describe(err))
267-
continue
268-
}
269-
270-
//t.Logf("Found version %s", v.Version)
271-
cancel()
272-
instanceUp <- nil
273-
return
250+
return driverErrorCheck(ctx, c, func(ctx context.Context, client driver.Client) error {
251+
if getTestMode() != testModeSingle {
252+
// Refresh endpoints
253+
if err := client.SynchronizeEndpoints2(ctx, "_system"); err != nil {
254+
return err
274255
}
275-
cancel()
276-
//t.Logf("Version failed: %s %#v", describe(err), err)
277-
time.Sleep(time.Second * 2)
278256
}
279-
}()
280-
select {
281-
case up := <-instanceUp:
282-
return up
283-
case <-ctx.Done():
257+
258+
if _, err := client.Version(ctx); err != nil {
259+
return err
260+
}
261+
262+
if _, err := client.Databases(ctx); err != nil {
263+
return err
264+
}
265+
284266
return nil
285-
}
267+
}, func(err error) (bool, error) {
268+
if err == nil {
269+
return true, nil
270+
}
271+
272+
if driver.IsNoLeaderOrOngoing(err) {
273+
t.Logf("Retry. Waiting for leader: %s", describe(err))
274+
return false, nil
275+
}
276+
277+
if driver.IsArangoErrorWithCode(err, 503) {
278+
t.Logf("Retry. Service not ready: %s", describe(err))
279+
return false, nil
280+
}
281+
282+
t.Logf("Retry. Unknown error: %s", describe(err))
283+
284+
return false, nil
285+
}).Retry(3*time.Second, time.Minute)
286286
}
287287

288288
// waitUntilClusterHealthy keeps waiting until the servers are healthy
@@ -325,31 +325,21 @@ func waitUntilClusterHealthy(c driver.Client) error {
325325

326326
// waitUntilEndpointSynchronized keeps waiting until the endpoints are synchronized. leadership might be ongoing.
327327
func waitUntilEndpointSynchronized(ctx context.Context, c driver.Client, dbname string, t testEnv) error {
328-
endpointsSynced := make(chan error)
329-
go func() {
330-
for {
331-
callCtx, cancel := context.WithTimeout(ctx, time.Second*5)
332-
if err := c.SynchronizeEndpoints2(callCtx, dbname); err != nil {
333-
t.Logf("SynchonizedEnpoints failed: %s", describe(err))
334-
} else {
335-
cancel()
336-
endpointsSynced <- nil
337-
return
338-
}
339-
cancel()
340-
select {
341-
case <-ctx.Done():
342-
return
343-
case <-time.After(time.Second):
344-
}
328+
return driverErrorCheck(ctx, c, func(ctx context.Context, client driver.Client) error {
329+
callCtx, cancel := context.WithTimeout(ctx, time.Second*5)
330+
defer cancel()
331+
if err := c.SynchronizeEndpoints2(callCtx, dbname); err != nil {
332+
return err
345333
}
346-
}()
347-
select {
348-
case up := <-endpointsSynced:
349-
return up
350-
case <-ctx.Done():
351-
return fmt.Errorf("Timeout while synchronizing endpoints")
352-
}
334+
335+
return nil
336+
}, func(err error) (bool, error) {
337+
if err == nil {
338+
return true, nil
339+
} else {
340+
return false, nil
341+
}
342+
}).Retry(3*time.Second, time.Minute)
353343
}
354344

355345
// TestCreateClientHttpConnection creates an HTTP connection to the environment specified

test/cluster.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ if [ "$CMD" == "start" ]; then
6969
${STARTER} \
7070
--starter.port=${STARTERPORT} --starter.address=127.0.0.1 \
7171
--docker.image=${ARANGODB} \
72-
--starter.local --starter.mode=${STARTERMODE} --all.log.level=debug --all.log.output=+ $STARTERARGS
72+
--starter.local --starter.mode=${STARTERMODE} --all.log.level=debug --all.log.output=+ --log.verbose $STARTERARGS
7373
fi

test/document_update_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func TestUpdateDocumentReturnNew(t *testing.T) {
125125
// TestUpdateDocumentKeepNullTrue creates a document, updates it with KeepNull(true) and then checks the update has succeeded.
126126
func TestUpdateDocumentKeepNullTrue(t *testing.T) {
127127
ctx := context.Background()
128-
var conn driver.Connection
129-
c := createClientFromEnv(t, true, &conn)
128+
c := createClientFromEnv(t, true)
129+
conn := c.Connection()
130130
db := ensureDatabase(ctx, c, "document_test", nil, t)
131131
col := ensureCollection(ctx, db, "document_test", nil, t)
132132
doc := Account{

test/documents_update_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ func TestUpdateDocumentsReturnNew(t *testing.T) {
187187
// TestUpdateDocumentsKeepNullTrue creates documents, updates them with KeepNull(true) and then checks the updates have succeeded.
188188
func TestUpdateDocumentsKeepNullTrue(t *testing.T) {
189189
ctx := context.Background()
190-
var conn driver.Connection
191-
c := createClientFromEnv(t, true, &conn)
190+
c := createClientFromEnv(t, true)
191+
conn := c.Connection()
192192
db := ensureDatabase(ctx, c, "document_test", nil, t)
193193
col := ensureCollection(ctx, db, "documents_test", nil, t)
194194
docs := []Account{

test/edge_update_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ func TestUpdateEdgeReturnNew(t *testing.T) {
151151
// TestUpdateEdgeKeepNullTrue creates a document, updates it with KeepNull(true) and then checks the update has succeeded.
152152
func TestUpdateEdgeKeepNullTrue(t *testing.T) {
153153
var ctx context.Context
154-
var conn driver.Connection
155-
c := createClientFromEnv(t, true, &conn)
154+
c := createClientFromEnv(t, true)
155+
conn := c.Connection()
156156
db := ensureDatabase(ctx, c, "edge_test", nil, t)
157157
prefix := "update_edge_keepNullTrue_"
158158
g := ensureGraph(ctx, db, prefix+"graph", nil, t)

test/edges_update_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ func TestUpdateEdgesReturnNew(t *testing.T) {
202202
// TestUpdateEdgesKeepNullTrue creates documents, updates them with KeepNull(true) and then checks the updates have succeeded.
203203
func TestUpdateEdgesKeepNullTrue(t *testing.T) {
204204
ctx := context.Background()
205-
var conn driver.Connection
206-
c := createClientFromEnv(t, true, &conn)
205+
c := createClientFromEnv(t, true)
206+
conn := c.Connection()
207207
db := ensureDatabase(ctx, c, "edges_test", nil, t)
208208
prefix := "update_edges_keepNullTrue_"
209209
g := ensureGraph(ctx, db, prefix+"graph", nil, t)

test/user_auth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func TestUpdateUserPasswordMyself(t *testing.T) {
3838
if getTestMode() == testModeResilientSingle {
3939
t.Skip("Disabled in active failover mode")
4040
}
41-
var conn driver.Connection
42-
c := createClientFromEnv(t, true, &conn)
41+
c := createClientFromEnv(t, true)
42+
conn := c.Connection()
4343
version, err := c.Version(nil)
4444
if err != nil {
4545
t.Fatalf("Version failed: %s", describe(err))
@@ -76,8 +76,8 @@ func TestUpdateUserPasswordOtherUser(t *testing.T) {
7676
if getTestMode() == testModeResilientSingle {
7777
t.Skip("Disabled in active failover mode")
7878
}
79-
var conn driver.Connection
80-
c := createClientFromEnv(t, true, &conn)
79+
c := createClientFromEnv(t, true)
80+
conn := c.Connection()
8181
version, err := c.Version(nil)
8282
if err != nil {
8383
t.Fatalf("Version failed: %s", describe(err))

test/util.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,19 @@ func (r retryFunc) Retry(interval, timeout time.Duration) error {
150150
defer intervalT.Stop()
151151

152152
for {
153+
if err := r(); err != nil {
154+
if _, ok := err.(interrupt); ok {
155+
return nil
156+
}
157+
158+
return err
159+
}
160+
153161
select {
154162
case <-timeoutT.C:
155163
return fmt.Errorf("function timeouted")
156164
case <-intervalT.C:
157-
if err := r(); err != nil {
158-
if _, ok := err.(interrupt); ok {
159-
return nil
160-
}
161-
162-
return err
163-
}
165+
continue
164166
}
165167
}
166168
}

test/vertex_update_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ func TestUpdateVertexReturnNew(t *testing.T) {
132132
// TestUpdateVertexKeepNullTrue creates a document, updates it with KeepNull(true) and then checks the update has succeeded.
133133
func TestUpdateVertexKeepNullTrue(t *testing.T) {
134134
var ctx context.Context
135-
var conn driver.Connection
136-
c := createClientFromEnv(t, true, &conn)
135+
c := createClientFromEnv(t, true)
136+
conn := c.Connection()
137137
db := ensureDatabase(ctx, c, "vertex_test", nil, t)
138138
g := ensureGraph(ctx, db, "update_vertex_keepNullTrue_test", nil, t)
139139
vc := ensureVertexCollection(ctx, g, "accounts", t)

0 commit comments

Comments
 (0)