Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase code coverage #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions internal/tezos/error_mapping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package tezos

import (
"errors"
"testing"

"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
"github.com/stretchr/testify/assert"
)

func TestMapError(t *testing.T) {
testCases := []struct {
name string
methodType tezosRPCMethodCategory
err error
errorReason ffcapi.ErrorReason
}{
{
name: "BlockRPCMethods with 404 Status error",
methodType: blockRPCMethods,
err: errors.New("status 404"),
errorReason: ffcapi.ErrorReasonNotFound,
},
{
name: "SendRPCMethods with counter_in_the_past error",
methodType: sendRPCMethods,
err: errors.New("counter_in_the_past"),
errorReason: ffcapi.ErrorReasonNonceTooLow,
},
{
name: "BlockRPCMethods with some non 404 Status error",
methodType: blockRPCMethods,
err: errors.New("error"),
errorReason: ffcapi.ErrorReason(""),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := mapError(tc.methodType, tc.err)

assert.Equal(t, tc.errorReason, res)
})
}
}

func TestErrorStatusNonHttpError(t *testing.T) {
errorStatus := ErrorStatus(errors.New("error"))

assert.Equal(t, errorStatus, 0)
}

func TestErrorStatusHttpError(t *testing.T) {
err := httpError{
request: "request",
status: "status",
statusCode: 400,
body: []byte("body"),
}

errorStatus := ErrorStatus(&err)

assert.Equal(t, errorStatus, err.StatusCode())
assert.Equal(t, err.Status(), "status")
assert.Equal(t, err.StatusCode(), 400)
assert.Equal(t, err.Request(), "request")
assert.Equal(t, err.Body(), []byte("body"))
assert.Equal(t, err.Error(), "rpc: request status 400 (body)")
}
19 changes: 14 additions & 5 deletions internal/tezos/retry_delay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,43 @@ func TestRetryDelay(t *testing.T) {
_, c, _, done := newTestConnector(t)
defer done()

c.retry.MaximumDelay = 1 * time.Microsecond
c.retry.InitialDelay = 100 * time.Microsecond
c.retry.MaximumDelay = 100 * time.Microsecond
c.retry.InitialDelay = 1 * time.Microsecond

ctx, cancel := context.WithCancel(context.Background())
go func() {
cancel()
}()

testCases := []struct {
name string
failureCount int
ctx context.Context
result bool
}{
{
name: "zero failure count",
failureCount: 0,
ctx: context.Background(),
result: false,
},
{
name: "retry delay exceeds max delay",
failureCount: 10,
ctx: context.Background(),
result: false,
},
{
name: "ctx done",
failureCount: 0,
result: false,
failureCount: 10,
ctx: ctx,
result: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := c.doFailureDelay(context.Background(), 0)
res := c.doFailureDelay(tc.ctx, tc.failureCount)
assert.Equal(t, tc.result, res)
})
}
Expand Down