Skip to content

Commit

Permalink
handle retry limit
Browse files Browse the repository at this point in the history
  • Loading branch information
gavincabbage committed Sep 19, 2019
1 parent 2110203 commit e8654f0
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -19,36 +20,35 @@ const (
retryInterval = 3 * time.Second
)

type fataler interface {
Fatal(...interface{})
}

func newDB(f fataler, driver string, url string) *sql.DB {
func newDB(t testing.TB, driver string, url string) *sql.DB {
db, err := sql.Open(driver, url)
if err != nil {
f.Fatal(err)
t.Fatal(err)
}

for i := 0; i < retryLimit; i++ {
if err := db.Ping(); err != nil {
time.Sleep(retryInterval)
continue
if i < retryLimit {
time.Sleep(retryInterval)
continue
}
t.Fatalf("connecting to %s: %w", driver, err)
}
break
}

return db
}

func newS3Client(f fataler, region string, endpoint string) *s3.S3 {
func newS3Client(t testing.TB, region string, endpoint string) *s3.S3 {
awsConfig := aws.NewConfig().
WithRegion(region).
WithDisableSSL(true).
WithCredentials(credentials.NewEnvCredentials())

awsSession, err := session.NewSession(awsConfig)
if err != nil {
f.Fatal(err)
t.Fatal(err)
}

client := s3.New(awsSession)
Expand All @@ -57,68 +57,68 @@ func newS3Client(f fataler, region string, endpoint string) *s3.S3 {
return client
}

func exec(f fataler, db *sql.DB, statements string) {
func exec(t testing.TB, db *sql.DB, statements string) {
s := strings.Split(statements, ";\n\n")
for _, statement := range s {
if _, err := db.Exec(statement); err != nil {
f.Fatal(err)
t.Fatal(err)
}
}
}

func createBucket(f fataler, client *s3.S3, name string) {
func createBucket(t testing.TB, client *s3.S3, name string) {
if _, err := client.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(name),
}); err != nil {
f.Fatal(err)
t.Fatal(err)
}
}

func deleteBucket(f fataler, client *s3.S3, name string) {
func deleteBucket(t testing.TB, client *s3.S3, name string) {
// we could do this more cleanly with BatchDeleteIterator, but localstack doesn't like batch deletes :shrug:
out, err := client.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(name),
})
if err != nil {
f.Fatal(err)
t.Fatal(err)
}

for _, o := range out.Contents {
if _, err := client.DeleteObject(&s3.DeleteObjectInput{
Key: o.Key,
Bucket: aws.String(name),
}); err != nil {
f.Fatal(err)
t.Fatal(err)
}
}

if _, err := client.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(name),
}); err != nil {
f.Fatal(err)
t.Fatal(err)
}
}

//nolint deadcode false positive
func readFile(f fataler, path string) string {
func readFile(t testing.TB, path string) string {
contents, err := ioutil.ReadFile(path)
if err != nil {
f.Fatal(err)
t.Fatal(err)
}

return string(contents)
}

//nolint deadcode false positive
func download(f fataler, downloader *s3manager.Downloader, bucket string, key string) string {
func download(t testing.TB, downloader *s3manager.Downloader, bucket string, key string) string {
b := &aws.WriteAtBuffer{}
_, err := downloader.Download(b, &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
fmt.Println("error:", bucket, key)
f.Fatal(err)
t.Fatal(err)
}

return string(b.Bytes())
Expand Down

0 comments on commit e8654f0

Please sign in to comment.