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.

199 lines
5.5 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. "context"
  4. "errors"
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "github.com/tendermint/tendermint/abci/example/kvstore"
  10. abci "github.com/tendermint/tendermint/abci/types"
  11. "github.com/tendermint/tendermint/libs/bytes"
  12. "github.com/tendermint/tendermint/rpc/client"
  13. "github.com/tendermint/tendermint/rpc/client/mock"
  14. ctypes "github.com/tendermint/tendermint/rpc/coretypes"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. func TestABCIMock(t *testing.T) {
  18. assert, require := assert.New(t), require.New(t)
  19. key, value := []byte("foo"), []byte("bar")
  20. height := int64(10)
  21. goodTx := types.Tx{0x01, 0xff}
  22. badTx := types.Tx{0x12, 0x21}
  23. m := mock.ABCIMock{
  24. Info: mock.Call{Error: errors.New("foobar")},
  25. Query: mock.Call{Response: abci.ResponseQuery{
  26. Key: key,
  27. Value: value,
  28. Height: height,
  29. }},
  30. // Broadcast commit depends on call
  31. BroadcastCommit: mock.Call{
  32. Args: goodTx,
  33. Response: &ctypes.ResultBroadcastTxCommit{
  34. CheckTx: abci.ResponseCheckTx{Data: bytes.HexBytes("stand")},
  35. DeliverTx: abci.ResponseDeliverTx{Data: bytes.HexBytes("deliver")},
  36. },
  37. Error: errors.New("bad tx"),
  38. },
  39. Broadcast: mock.Call{Error: errors.New("must commit")},
  40. }
  41. // now, let's try to make some calls
  42. _, err := m.ABCIInfo(context.Background())
  43. require.NotNil(err)
  44. assert.Equal("foobar", err.Error())
  45. // query always returns the response
  46. _query, err := m.ABCIQueryWithOptions(context.Background(), "/", nil, client.ABCIQueryOptions{Prove: false})
  47. query := _query.Response
  48. require.Nil(err)
  49. require.NotNil(query)
  50. assert.EqualValues(key, query.Key)
  51. assert.EqualValues(value, query.Value)
  52. assert.Equal(height, query.Height)
  53. // non-commit calls always return errors
  54. _, err = m.BroadcastTxSync(context.Background(), goodTx)
  55. require.NotNil(err)
  56. assert.Equal("must commit", err.Error())
  57. _, err = m.BroadcastTxAsync(context.Background(), goodTx)
  58. require.NotNil(err)
  59. assert.Equal("must commit", err.Error())
  60. // commit depends on the input
  61. _, err = m.BroadcastTxCommit(context.Background(), badTx)
  62. require.NotNil(err)
  63. assert.Equal("bad tx", err.Error())
  64. bres, err := m.BroadcastTxCommit(context.Background(), goodTx)
  65. require.Nil(err, "%+v", err)
  66. assert.EqualValues(0, bres.CheckTx.Code)
  67. assert.EqualValues("stand", bres.CheckTx.Data)
  68. assert.EqualValues("deliver", bres.DeliverTx.Data)
  69. }
  70. func TestABCIRecorder(t *testing.T) {
  71. assert, require := assert.New(t), require.New(t)
  72. // This mock returns errors on everything but Query
  73. m := mock.ABCIMock{
  74. Info: mock.Call{Response: abci.ResponseInfo{
  75. Data: "data",
  76. Version: "v0.9.9",
  77. }},
  78. Query: mock.Call{Error: errors.New("query")},
  79. Broadcast: mock.Call{Error: errors.New("broadcast")},
  80. BroadcastCommit: mock.Call{Error: errors.New("broadcast_commit")},
  81. }
  82. r := mock.NewABCIRecorder(m)
  83. require.Equal(0, len(r.Calls))
  84. _, err := r.ABCIInfo(context.Background())
  85. assert.Nil(err, "expected no err on info")
  86. _, err = r.ABCIQueryWithOptions(
  87. context.Background(),
  88. "path",
  89. bytes.HexBytes("data"),
  90. client.ABCIQueryOptions{Prove: false},
  91. )
  92. assert.NotNil(err, "expected error on query")
  93. require.Equal(2, len(r.Calls))
  94. info := r.Calls[0]
  95. assert.Equal("abci_info", info.Name)
  96. assert.Nil(info.Error)
  97. assert.Nil(info.Args)
  98. require.NotNil(info.Response)
  99. ir, ok := info.Response.(*ctypes.ResultABCIInfo)
  100. require.True(ok)
  101. assert.Equal("data", ir.Response.Data)
  102. assert.Equal("v0.9.9", ir.Response.Version)
  103. query := r.Calls[1]
  104. assert.Equal("abci_query", query.Name)
  105. assert.Nil(query.Response)
  106. require.NotNil(query.Error)
  107. assert.Equal("query", query.Error.Error())
  108. require.NotNil(query.Args)
  109. qa, ok := query.Args.(mock.QueryArgs)
  110. require.True(ok)
  111. assert.Equal("path", qa.Path)
  112. assert.EqualValues("data", qa.Data)
  113. assert.False(qa.Prove)
  114. // now add some broadcasts (should all err)
  115. txs := []types.Tx{{1}, {2}, {3}}
  116. _, err = r.BroadcastTxCommit(context.Background(), txs[0])
  117. assert.NotNil(err, "expected err on broadcast")
  118. _, err = r.BroadcastTxSync(context.Background(), txs[1])
  119. assert.NotNil(err, "expected err on broadcast")
  120. _, err = r.BroadcastTxAsync(context.Background(), txs[2])
  121. assert.NotNil(err, "expected err on broadcast")
  122. require.Equal(5, len(r.Calls))
  123. bc := r.Calls[2]
  124. assert.Equal("broadcast_tx_commit", bc.Name)
  125. assert.Nil(bc.Response)
  126. require.NotNil(bc.Error)
  127. assert.EqualValues(bc.Args, txs[0])
  128. bs := r.Calls[3]
  129. assert.Equal("broadcast_tx_sync", bs.Name)
  130. assert.Nil(bs.Response)
  131. require.NotNil(bs.Error)
  132. assert.EqualValues(bs.Args, txs[1])
  133. ba := r.Calls[4]
  134. assert.Equal("broadcast_tx_async", ba.Name)
  135. assert.Nil(ba.Response)
  136. require.NotNil(ba.Error)
  137. assert.EqualValues(ba.Args, txs[2])
  138. }
  139. func TestABCIApp(t *testing.T) {
  140. assert, require := assert.New(t), require.New(t)
  141. app := kvstore.NewApplication()
  142. m := mock.ABCIApp{app}
  143. // get some info
  144. info, err := m.ABCIInfo(context.Background())
  145. require.Nil(err)
  146. assert.Equal(`{"size":0}`, info.Response.GetData())
  147. // add a key
  148. key, value := "foo", "bar"
  149. tx := fmt.Sprintf("%s=%s", key, value)
  150. res, err := m.BroadcastTxCommit(context.Background(), types.Tx(tx))
  151. require.Nil(err)
  152. assert.True(res.CheckTx.IsOK())
  153. require.NotNil(res.DeliverTx)
  154. assert.True(res.DeliverTx.IsOK())
  155. // commit
  156. // TODO: This may not be necessary in the future
  157. if res.Height == -1 {
  158. m.App.Commit()
  159. }
  160. // check the key
  161. _qres, err := m.ABCIQueryWithOptions(
  162. context.Background(),
  163. "/key",
  164. bytes.HexBytes(key),
  165. client.ABCIQueryOptions{Prove: true},
  166. )
  167. qres := _qres.Response
  168. require.Nil(err)
  169. assert.EqualValues(value, qres.Value)
  170. // XXX Check proof
  171. }