diff --git a/internal/tezos/error_mapping_test.go b/internal/tezos/error_mapping_test.go new file mode 100644 index 0000000..a5e6b2f --- /dev/null +++ b/internal/tezos/error_mapping_test.go @@ -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)") +} diff --git a/internal/tezos/retry_delay_test.go b/internal/tezos/retry_delay_test.go index 4dd479e..f1c5c49 100644 --- a/internal/tezos/retry_delay_test.go +++ b/internal/tezos/retry_delay_test.go @@ -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) }) }