Skip to content

Commit 2aac9e3

Browse files
authored
[Bugfix] Retry on 503 in tests (#311)
1 parent 9cc66b6 commit 2aac9e3

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ services:
2828
language: go
2929

3030
env:
31-
- TEST_SUITE=run-unit-tests ALWAYS=1
31+
- TEST_SUITE=run-unit-tests GOIMAGE=gcr.io/gcr-for-testing/golang:1.13.4-stretch ALWAYS=1
3232
- TEST_SUITE=run-tests-single GOIMAGE=gcr.io/gcr-for-testing/golang:1.13.4-stretch STARTER=gcr.io/gcr-for-testing/arangodb/arangodb-starter:latest ALPINE_IMAGE=gcr.io/gcr-for-testing/alpine:3.4 ARANGODB=gcr.io/gcr-for-testing/arangodb:3.6
3333
- TEST_SUITE=run-tests-single GOIMAGE=gcr.io/gcr-for-testing/golang:1.13.4-stretch STARTER=gcr.io/gcr-for-testing/arangodb/arangodb-starter:latest ALPINE_IMAGE=gcr.io/gcr-for-testing/alpine:3.4 ARANGODB=gcr.io/gcr-for-testing/arangodb/arangodb:latest ALWAYS=1
3434
- TEST_SUITE=run-tests-single GOIMAGE=gcr.io/gcr-for-testing/golang:1.13.4-stretch STARTER=gcr.io/gcr-for-testing/arangodb/arangodb-starter:latest ALPINE_IMAGE=gcr.io/gcr-for-testing/alpine:3.4 ARANGODB=gcr.io/gcr-for-testing/arangodb:3.7

error.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ func IsArangoError(err error) bool {
100100
return ok && ae.HasError
101101
}
102102

103+
// AsArangoError returns true when the given error is an ArangoError together with an object.
104+
func AsArangoError(err error) (ArangoError, bool) {
105+
ae, ok := Cause(err).(ArangoError)
106+
if ok {
107+
return ae, true
108+
} else {
109+
return ArangoError{}, false
110+
}
111+
}
112+
103113
// IsArangoErrorWithCode returns true when the given error is an ArangoError and its Code field is equal to the given code.
104114
func IsArangoErrorWithCode(err error, code int) bool {
105115
ae, ok := Cause(err).(ArangoError)

test/agency_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ import (
4242
"github.com/stretchr/testify/require"
4343
)
4444

45+
func checkAgencyEndpoints(ctx context.Context, c driver.Client) error {
46+
cl, err := c.Cluster(ctx)
47+
if err != nil {
48+
return err
49+
}
50+
_, err = cl.Health(ctx)
51+
if err != nil {
52+
return err
53+
}
54+
55+
return nil
56+
}
57+
4558
// getAgencyEndpoints queries the cluster to get all agency endpoints.
4659
func getAgencyEndpoints(ctx context.Context, c driver.Client) ([]string, error) {
4760
cl, err := c.Cluster(ctx)
@@ -114,6 +127,11 @@ func getAgencyConnection(ctx context.Context, t testEnv, c driver.Client) (agenc
114127
// These tests assume an HTTP connetion, so we skip under this condition
115128
return nil, driver.ArangoError{HasError: true, Code: 412, ErrorMessage: "Using vst is not supported in agency tests"}
116129
}
130+
131+
if err := driverErrorCheck(ctx, c, checkAgencyEndpoints, driverErrorCheckRetry503).Retry(125*time.Millisecond, time.Minute); err != nil {
132+
return nil, err
133+
}
134+
117135
endpoints, err := getAgencyEndpoints(ctx, c)
118136
if err != nil {
119137
return nil, err

test/check_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2021 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Adam Janikowski
21+
//
22+
23+
package test
24+
25+
import (
26+
"context"
27+
"net/http"
28+
29+
"github.com/arangodb/go-driver"
30+
)
31+
32+
type driverErrorCheckFunc func(err error) (bool, error)
33+
type driverErrorChecker func(ctx context.Context, client driver.Client) error
34+
35+
func driverErrorCheck(ctx context.Context, c driver.Client, checker driverErrorChecker, checks ...driverErrorCheckFunc) retryFunc {
36+
return func() error {
37+
err := checker(ctx, c)
38+
39+
for _, check := range checks {
40+
if valid, err := check(err); err != nil {
41+
return err
42+
} else if !valid {
43+
return nil
44+
}
45+
}
46+
47+
return interrupt{}
48+
}
49+
}
50+
51+
func driverErrorCheckRetry503(err error) (bool, error) {
52+
if err == nil {
53+
return true, nil
54+
}
55+
56+
if ae, ok := driver.AsArangoError(err); !ok {
57+
return false, err
58+
} else {
59+
if !ae.HasError {
60+
return true, nil
61+
}
62+
switch ae.Code {
63+
case http.StatusServiceUnavailable:
64+
return false, nil
65+
default:
66+
return true, err
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)