mirror of
https://github.com/octoleo/restic.git
synced 2024-11-22 21:05:10 +00:00
Add unit tests
This commit is contained in:
parent
d629333efe
commit
baf58fbaa8
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user