blob: 1e3126317cb1853cff39959a0a1a931fa0135f66 [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package retry
import (
"context"
"fmt"
"testing"
)
func TestRetry(t *testing.T) {
const tries = 5
t.Run("retries until function returns nil", func(t *testing.T) {
var i int
err := Retry(context.Background(), &ZeroBackoff{}, func() error {
i++
if i == tries {
return nil
}
return fmt.Errorf("try %d", i)
}, nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if i != tries {
t.Errorf("got %d tries, wanted %d", i, tries)
}
})
t.Run("stops retrying after context is canceled", func(t *testing.T) {
var i int
ctx, cancel := context.WithCancel(context.Background())
err := Retry(ctx, &ZeroBackoff{}, func() error {
i++
if i == tries {
cancel()
}
return fmt.Errorf("try %d", i)
}, nil)
if err == nil {
t.Error("error is nil")
}
expectedErr := "try 5"
if err.Error() != expectedErr {
t.Errorf("got error: %v, wanted: %s", err, expectedErr)
}
if i != tries {
t.Errorf("got %d tries, wanted %d", i, tries)
}
})
t.Run("stops retrying after a fatal error", func(t *testing.T) {
var i int
var lastErr error
err := Retry(context.Background(), &ZeroBackoff{}, func() error {
i++
err := fmt.Errorf("try %d", i)
lastErr = err
if i == tries {
return Fatal(err)
}
return err
}, nil)
if err == nil {
t.Error("error is nil")
} else if err != lastErr {
t.Errorf("got error: %q, wanted: %q", err, lastErr)
}
if i != tries {
t.Errorf("got %d tries, wanted %d", i, tries)
}
})
}