Add unit tests

This commit is contained in:
Fred 2020-03-21 19:05:36 +00:00 committed by Michael Eischer
parent d629333efe
commit baf58fbaa8
1 changed files with 55 additions and 0 deletions

View File

@ -6,7 +6,9 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"testing" "testing"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/restic/restic/internal/backend/mock" "github.com/restic/restic/internal/backend/mock"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
@ -301,3 +303,56 @@ func TestBackendCanceledContext(t *testing.T) {
// don't test "Delete" as it is not used by normal code // don't test "Delete" as it is not used by normal code
} }
func TestNotifyWithSuccessIsNotCalled(t *testing.T) {
operation := func() error {
return nil
}
notify := func(error, time.Duration) {
t.Fatal("Notify should not have been called")
}
success := func() {
t.Fatal("Success should not have been called")
}
err := retryNotifyErrorWithSuccess(operation, &backoff.ZeroBackOff{}, notify, success)
if err != nil {
t.Fatal("retry should not have returned an error")
}
}
func TestNotifyWithSuccessIsCalled(t *testing.T) {
operationCalled := 0
operation := func() error {
operationCalled++
if operationCalled <= 2 {
return errors.New("expected error in test")
}
return nil
}
notifyCalled := 0
notify := func(error, time.Duration) {
notifyCalled++
}
successCalled := 0
success := func() {
successCalled++
}
err := retryNotifyErrorWithSuccess(operation, &backoff.ZeroBackOff{}, notify, success)
if err != nil {
t.Fatal("retry should not have returned an error")
}
if notifyCalled != 2 {
t.Fatalf("Notify should have been called 2 times, but was called %d times instead", notifyCalled)
}
if successCalled != 1 {
t.Fatalf("Success should have been called only once, but was called %d times instead", successCalled)
}
}