You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.2 KiB

8 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
8 years ago
  1. package mock_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/pkg/errors"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/abci/example/kvstore"
  9. abci "github.com/tendermint/tendermint/abci/types"
  10. "github.com/tendermint/tendermint/libs/bytes"
  11. "github.com/tendermint/tendermint/rpc/client"
  12. "github.com/tendermint/tendermint/rpc/client/mock"
  13. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. func TestABCIMock(t *testing.T) {
  17. assert, require := assert.New(t), require.New(t)
  18. key, value := []byte("foo"), []byte("bar")
  19. height := int64(10)
  20. goodTx := types.Tx{0x01, 0xff}
  21. badTx := types.Tx{0x12, 0x21}
  22. m := mock.ABCIMock{
  23. Info: mock.Call{Error: errors.New("foobar")},
  24. Query: mock.Call{Response: abci.ResponseQuery{
  25. Key: key,
  26. Value: value,
  27. Height: height,
  28. }},
  29. // Broadcast commit depends on call
  30. BroadcastCommit: mock.Call{
  31. Args: goodTx,
  32. Response: &ctypes.ResultBroadcastTxCommit{
  33. CheckTx: abci.ResponseCheckTx{Data: bytes.HexBytes("stand")},
  34. DeliverTx: abci.ResponseDeliverTx{Data: bytes.HexBytes("deliver")},
  35. },
  36. Error: errors.New("bad tx"),
  37. },
  38. Broadcast: mock.Call{Error: errors.New("must commit")},
  39. }
  40. // now, let's try to make some calls
  41. _, err := m.ABCIInfo()
  42. require.NotNil(err)
  43. assert.Equal("foobar", err.Error())
  44. // query always returns the response
  45. _query, err := m.ABCIQueryWithOptions("/", nil, client.ABCIQueryOptions{Prove: false})
  46. query := _query.Response
  47. require.Nil(err)
  48. require.NotNil(query)
  49. assert.EqualValues(key, query.Key)
  50. assert.EqualValues(value, query.Value)
  51. assert.Equal(height, query.Height)
  52. // non-commit calls always return errors
  53. _, err = m.BroadcastTxSync(goodTx)
  54. require.NotNil(err)
  55. assert.Equal("must commit", err.Error())
  56. _, err = m.BroadcastTxAsync(goodTx)
  57. require.NotNil(err)
  58. assert.Equal("must commit", err.Error())
  59. // commit depends on the input
  60. _, err = m.BroadcastTxCommit(badTx)
  61. require.NotNil(err)
  62. assert.Equal("bad tx", err.Error())
  63. bres, err := m.BroadcastTxCommit(goodTx)
  64. require.Nil(err, "%+v", err)
  65. assert.EqualValues(0, bres.CheckTx.Code)
  66. assert.EqualValues("stand", bres.CheckTx.Data)
  67. assert.EqualValues("deliver", bres.DeliverTx.Data)
  68. }
  69. func TestABCIRecorder(t *testing.T) {
  70. assert, require := assert.New(t), require.New(t)
  71. // This mock returns errors on everything but Query
  72. m := mock.ABCIMock{
  73. Info: mock.Call{Response: abci.ResponseInfo{
  74. Data: "data",
  75. Version: "v0.9.9",
  76. }},
  77. Query: mock.Call{Error: errors.New("query")},
  78. Broadcast: mock.Call{Error: errors.New("broadcast")},
  79. BroadcastCommit: mock.Call{Error: errors.New("broadcast_commit")},
  80. }
  81. r := mock.NewABCIRecorder(m)
  82. require.Equal(0, len(r.Calls))
  83. _, err := r.ABCIInfo()
  84. assert.Nil(err, "expected no err on info")
  85. _, err = r.ABCIQueryWithOptions("path", bytes.HexBytes("data"), client.ABCIQueryOptions{Prove: false})
  86. assert.NotNil(err, "expected error on query")
  87. require.Equal(2, len(r.Calls))
  88. info := r.Calls[0]
  89. assert.Equal("abci_info", info.Name)
  90. assert.Nil(info.Error)
  91. assert.Nil(info.Args)
  92. require.NotNil(info.Response)
  93. ir, ok := info.Response.(*ctypes.ResultABCIInfo)
  94. require.True(ok)
  95. assert.Equal("data", ir.Response.Data)
  96. assert.Equal("v0.9.9", ir.Response.Version)
  97. query := r.Calls[1]
  98. assert.Equal("abci_query", query.Name)
  99. assert.Nil(query.Response)
  100. require.NotNil(query.Error)
  101. assert.Equal("query", query.Error.Error())
  102. require.NotNil(query.Args)
  103. qa, ok := query.Args.(mock.QueryArgs)
  104. require.True(ok)
  105. assert.Equal("path", qa.Path)
  106. assert.EqualValues("data", qa.Data)
  107. assert.False(qa.Prove)
  108. // now add some broadcasts (should all err)
  109. txs := []types.Tx{{1}, {2}, {3}}
  110. _, err = r.BroadcastTxCommit(txs[0])
  111. assert.NotNil(err, "expected err on broadcast")
  112. _, err = r.BroadcastTxSync(txs[1])
  113. assert.NotNil(err, "expected err on broadcast")
  114. _, err = r.BroadcastTxAsync(txs[2])
  115. assert.NotNil(err, "expected err on broadcast")
  116. require.Equal(5, len(r.Calls))
  117. bc := r.Calls[2]
  118. assert.Equal("broadcast_tx_commit", bc.Name)
  119. assert.Nil(bc.Response)
  120. require.NotNil(bc.Error)
  121. assert.EqualValues(bc.Args, txs[0])
  122. bs := r.Calls[3]
  123. assert.Equal("broadcast_tx_sync", bs.Name)
  124. assert.Nil(bs.Response)
  125. require.NotNil(bs.Error)
  126. assert.EqualValues(bs.Args, txs[1])
  127. ba := r.Calls[4]
  128. assert.Equal("broadcast_tx_async", ba.Name)
  129. assert.Nil(ba.Response)
  130. require.NotNil(ba.Error)
  131. assert.EqualValues(ba.Args, txs[2])
  132. }
  133. func TestABCIApp(t *testing.T) {
  134. assert, require := assert.New(t), require.New(t)
  135. app := kvstore.NewApplication()
  136. m := mock.ABCIApp{app}
  137. // get some info
  138. info, err := m.ABCIInfo()
  139. require.Nil(err)
  140. assert.Equal(`{"size":0}`, info.Response.GetData())
  141. // add a key
  142. key, value := "foo", "bar"
  143. tx := fmt.Sprintf("%s=%s", key, value)
  144. res, err := m.BroadcastTxCommit(types.Tx(tx))
  145. require.Nil(err)
  146. assert.True(res.CheckTx.IsOK())
  147. require.NotNil(res.DeliverTx)
  148. assert.True(res.DeliverTx.IsOK())
  149. // commit
  150. // TODO: This may not be necessary in the future
  151. if res.Height == -1 {
  152. m.App.Commit()
  153. }
  154. // check the key
  155. _qres, err := m.ABCIQueryWithOptions("/key", bytes.HexBytes(key), client.ABCIQueryOptions{Prove: true})
  156. qres := _qres.Response
  157. require.Nil(err)
  158. assert.EqualValues(value, qres.Value)
  159. // XXX Check proof
  160. }