GCS driver: fix retry function

Signed-off-by: Arthur Baars <arthur@semmle.com>
This commit is contained in:
Arthur Baars 2016-01-06 11:47:28 +00:00
parent 74d719d83f
commit d3d40a8807
2 changed files with 43 additions and 3 deletions

View file

@ -318,13 +318,13 @@ func retry(maxTries int, req request) error {
backoff := time.Second backoff := time.Second
var err error var err error
for i := 0; i < maxTries; i++ { for i := 0; i < maxTries; i++ {
err := req() err = req()
if err == nil { if err == nil {
return nil return nil
} }
status := err.(*googleapi.Error) status, ok := err.(*googleapi.Error)
if status == nil || (status.Code != 429 && status.Code < http.StatusInternalServerError) { if !ok || (status.Code != 429 && status.Code < http.StatusInternalServerError) {
return err return err
} }

View file

@ -3,10 +3,13 @@
package gcs package gcs
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"google.golang.org/api/googleapi"
ctx "github.com/docker/distribution/context" ctx "github.com/docker/distribution/context"
storagedriver "github.com/docker/distribution/registry/storage/driver" storagedriver "github.com/docker/distribution/registry/storage/driver"
"github.com/docker/distribution/registry/storage/driver/testsuites" "github.com/docker/distribution/registry/storage/driver/testsuites"
@ -55,6 +58,43 @@ func init() {
}, skipGCS) }, skipGCS)
} }
func TestRetry(t *testing.T) {
if skipGCS() != "" {
t.Skip(skipGCS())
}
assertError := func(expected string, observed error) {
observedMsg := "<nil>"
if observed != nil {
observedMsg = observed.Error()
}
if observedMsg != expected {
t.Fatalf("expected %v, observed %v\n", expected, observedMsg)
}
}
err := retry(2, func() error {
return &googleapi.Error{
Code: 503,
Message: "google api error",
}
})
assertError("googleapi: Error 503: google api error", err)
err = retry(2, func() error {
return &googleapi.Error{
Code: 404,
Message: "google api error",
}
})
assertError("googleapi: Error 404: google api error", err)
err = retry(2, func() error {
return fmt.Errorf("error")
})
assertError("error", err)
}
func TestEmptyRootList(t *testing.T) { func TestEmptyRootList(t *testing.T) {
if skipGCS() != "" { if skipGCS() != "" {
t.Skip(skipGCS()) t.Skip(skipGCS())