Skip to content

Commit

Permalink
test: connection retries
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosNicolau committed Oct 23, 2024
1 parent 9eef24f commit 053d1fb
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions core/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,65 @@ func DummyFunction(x uint64) (uint64, error) {
return x, nil
}

// here we run the dummy function and with the retry and check:
// - The number of retries checks based on the `n`
// - The returned valued matches based on the `n`
// - The returned err matches based on the `n`
func TestRetryWithData(t *testing.T) {
function := func() (*uint64, error) {
x, err := DummyFunction(43)
return &x, err
retries := -1
testFun := func(n uint64) func() (*uint64, error) {
return func() (*uint64, error) {
retries++
x, err := DummyFunction(n)
return &x, err
}
}
data, err := connection.RetryWithData(function, 1000, 2, 3)
if err != nil {
t.Errorf("Retry error!: %s", err)
} else {
fmt.Printf("DATA: %d\n", data)
data, err := connection.RetryWithData(testFun(uint64(41)), 1000, 2, 3)
if !(retries == 3 && *data == 0 && err != nil) {
t.Error("Incorrect execution when n == 41")
}
//restart
retries = -1
data, err = connection.RetryWithData(testFun(42), 1000, 2, 3)
if !(retries == 0 && data == nil) {
if _, ok := err.(*connection.PermanentError); ok {
t.Error("Incorrect execution when n == 42")
}
}
//restart
retries = -1
data, err = connection.RetryWithData(testFun(43), 1000, 2, 3)
if !(retries == 0 && *data == 43 && err == nil) {
t.Error("Incorrect execution when n == 43")
}
}

// same as above but without checking returned data
func TestRetry(t *testing.T) {
function := func() error {
_, err := DummyFunction(43)
return err
retries := -1
testFun := func(n uint64) func() error {
return func() error {
retries++
_, err := DummyFunction(n)
return err
}
}
err := connection.Retry(testFun(uint64(41)), 1000, 2, 3)
if !(retries == 3 && err != nil) {
t.Error("Incorrect execution when n == 41")
}
//restart
retries = -1
err = connection.Retry(testFun(42), 1000, 2, 3)
if !(retries == 0) {
if _, ok := err.(*connection.PermanentError); ok {
t.Error("Incorrect execution when n == 42")
}
}
err := connection.Retry(function, 1000, 2, 3)
if err != nil {
t.Errorf("Retry error!: %s", err)
//restart
retries = -1
err = connection.Retry(testFun(43), 1000, 2, 3)
if !(retries == 0 && err == nil) {
t.Error("Incorrect execution when n == 43")
}
}

0 comments on commit 053d1fb

Please sign in to comment.