Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Semenyuk <[email protected]>
  • Loading branch information
alex-semenyuk committed Oct 30, 2023
1 parent c8b86f6 commit 078f67e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
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

0 comments on commit 078f67e

Please sign in to comment.