-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package context | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/tendermint/mempool" | ||
"github.com/tendermint/tendermint/rpc/client/mock" | ||
ctypes "github.com/tendermint/tendermint/rpc/core/types" | ||
tmtypes "github.com/tendermint/tendermint/types" | ||
) | ||
|
||
type MockClient struct { | ||
mock.Client | ||
err error | ||
} | ||
|
||
func (c MockClient) BroadcastTxCommit(tx tmtypes.Tx) (*ctypes.ResultBroadcastTxCommit, error) { | ||
return nil, c.err | ||
} | ||
|
||
func (c MockClient) BroadcastTxAsync(tx tmtypes.Tx) (*ctypes.ResultBroadcastTx, error) { | ||
return nil, c.err | ||
} | ||
|
||
func (c MockClient) BroadcastTxSync(tx tmtypes.Tx) (*ctypes.ResultBroadcastTx, error) { | ||
return nil, c.err | ||
} | ||
|
||
func CreateContextWithErrorAndMode(err error, mode string) CLIContext { | ||
return CLIContext{ | ||
Client: MockClient{err: err}, | ||
BroadcastMode: mode, | ||
} | ||
} | ||
|
||
// Test the correct code is returned when | ||
func TestBroadcastError(t *testing.T) { | ||
errors := map[error]uint32{ | ||
mempool.ErrTxInCache: uint32(types.CodeTxInCache), | ||
mempool.ErrTxTooLarge{}: uint32(types.CodeTxTooLarge), | ||
mempool.ErrPreCheck{}: uint32(types.CodeFailedPreCheck), | ||
mempool.ErrMempoolIsFull{}: uint32(types.CodeMempoolIsFull), | ||
} | ||
|
||
modes := []string{ | ||
flags.BroadcastAsync, | ||
flags.BroadcastBlock, | ||
flags.BroadcastSync, | ||
} | ||
|
||
for _, mode := range modes { | ||
for err, code := range errors { | ||
ctx := CreateContextWithErrorAndMode(err, mode) | ||
resp, returnedErr := ctx.BroadcastTx([]byte{}) | ||
require.Error(t, returnedErr) | ||
require.Equal(t, code, resp.Code) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters