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.

203 lines
5.6 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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
  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. "github.com/tendermint/tendermint/rpc/coretypes"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. func TestABCIMock(t *testing.T) {
  18. ctx, cancel := context.WithCancel(context.Background())
  19. defer cancel()
  20. key, value := []byte("foo"), []byte("bar")
  21. height := int64(10)
  22. goodTx := types.Tx{0x01, 0xff}
  23. badTx := types.Tx{0x12, 0x21}
  24. m := mock.ABCIMock{
  25. Info: mock.Call{Error: errors.New("foobar")},
  26. Query: mock.Call{Response: abci.ResponseQuery{
  27. Key: key,
  28. Value: value,
  29. Height: height,
  30. }},
  31. // Broadcast commit depends on call
  32. BroadcastCommit: mock.Call{
  33. Args: goodTx,
  34. Response: &coretypes.ResultBroadcastTxCommit{
  35. CheckTx: abci.ResponseCheckTx{Data: bytes.HexBytes("stand")},
  36. DeliverTx: abci.ResponseDeliverTx{Data: bytes.HexBytes("deliver")},
  37. },
  38. Error: errors.New("bad tx"),
  39. },
  40. Broadcast: mock.Call{Error: errors.New("must commit")},
  41. }
  42. // now, let's try to make some calls
  43. _, err := m.ABCIInfo(ctx)
  44. require.Error(t, err)
  45. assert.Equal(t, "foobar", err.Error())
  46. // query always returns the response
  47. _query, err := m.ABCIQueryWithOptions(ctx, "/", nil, client.ABCIQueryOptions{Prove: false})
  48. query := _query.Response
  49. require.NoError(t, err)
  50. require.NotNil(t, query)
  51. assert.EqualValues(t, key, query.Key)
  52. assert.EqualValues(t, value, query.Value)
  53. assert.Equal(t, height, query.Height)
  54. // non-commit calls always return errors
  55. _, err = m.BroadcastTxSync(ctx, goodTx)
  56. require.Error(t, err)
  57. assert.Equal(t, "must commit", err.Error())
  58. _, err = m.BroadcastTxAsync(ctx, goodTx)
  59. require.Error(t, err)
  60. assert.Equal(t, "must commit", err.Error())
  61. // commit depends on the input
  62. _, err = m.BroadcastTxCommit(ctx, badTx)
  63. require.Error(t, err)
  64. assert.Equal(t, "bad tx", err.Error())
  65. bres, err := m.BroadcastTxCommit(ctx, goodTx)
  66. require.NoError(t, err, "%+v", err)
  67. assert.EqualValues(t, 0, bres.CheckTx.Code)
  68. assert.EqualValues(t, "stand", bres.CheckTx.Data)
  69. assert.EqualValues(t, "deliver", bres.DeliverTx.Data)
  70. }
  71. func TestABCIRecorder(t *testing.T) {
  72. ctx, cancel := context.WithCancel(context.Background())
  73. defer cancel()
  74. // This mock returns errors on everything but Query
  75. m := mock.ABCIMock{
  76. Info: mock.Call{Response: abci.ResponseInfo{
  77. Data: "data",
  78. Version: "v0.9.9",
  79. }},
  80. Query: mock.Call{Error: errors.New("query")},
  81. Broadcast: mock.Call{Error: errors.New("broadcast")},
  82. BroadcastCommit: mock.Call{Error: errors.New("broadcast_commit")},
  83. }
  84. r := mock.NewABCIRecorder(m)
  85. require.Equal(t, 0, len(r.Calls))
  86. _, err := r.ABCIInfo(ctx)
  87. assert.NoError(t, err, "expected no err on info")
  88. _, err = r.ABCIQueryWithOptions(
  89. ctx,
  90. "path",
  91. bytes.HexBytes("data"),
  92. client.ABCIQueryOptions{Prove: false},
  93. )
  94. assert.Error(t, err, "expected error on query")
  95. require.Equal(t, 2, len(r.Calls))
  96. info := r.Calls[0]
  97. assert.Equal(t, "abci_info", info.Name)
  98. assert.Nil(t, info.Error)
  99. assert.Nil(t, info.Args)
  100. require.NotNil(t, info.Response)
  101. ir, ok := info.Response.(*coretypes.ResultABCIInfo)
  102. require.True(t, ok)
  103. assert.Equal(t, "data", ir.Response.Data)
  104. assert.Equal(t, "v0.9.9", ir.Response.Version)
  105. query := r.Calls[1]
  106. assert.Equal(t, "abci_query", query.Name)
  107. assert.Nil(t, query.Response)
  108. require.NotNil(t, query.Error)
  109. assert.Equal(t, "query", query.Error.Error())
  110. require.NotNil(t, query.Args)
  111. qa, ok := query.Args.(mock.QueryArgs)
  112. require.True(t, ok)
  113. assert.Equal(t, "path", qa.Path)
  114. assert.EqualValues(t, "data", qa.Data)
  115. assert.False(t, qa.Prove)
  116. // now add some broadcasts (should all err)
  117. txs := []types.Tx{{1}, {2}, {3}}
  118. _, err = r.BroadcastTxCommit(ctx, txs[0])
  119. assert.Error(t, err, "expected err on broadcast")
  120. _, err = r.BroadcastTxSync(ctx, txs[1])
  121. assert.Error(t, err, "expected err on broadcast")
  122. _, err = r.BroadcastTxAsync(ctx, txs[2])
  123. assert.Error(t, err, "expected err on broadcast")
  124. require.Equal(t, 5, len(r.Calls))
  125. bc := r.Calls[2]
  126. assert.Equal(t, "broadcast_tx_commit", bc.Name)
  127. assert.Nil(t, bc.Response)
  128. require.NotNil(t, bc.Error)
  129. assert.EqualValues(t, bc.Args, txs[0])
  130. bs := r.Calls[3]
  131. assert.Equal(t, "broadcast_tx_sync", bs.Name)
  132. assert.Nil(t, bs.Response)
  133. require.NotNil(t, bs.Error)
  134. assert.EqualValues(t, bs.Args, txs[1])
  135. ba := r.Calls[4]
  136. assert.Equal(t, "broadcast_tx_async", ba.Name)
  137. assert.Nil(t, ba.Response)
  138. require.NotNil(t, ba.Error)
  139. assert.EqualValues(t, ba.Args, txs[2])
  140. }
  141. func TestABCIApp(t *testing.T) {
  142. app := kvstore.NewApplication()
  143. m := mock.ABCIApp{app}
  144. ctx, cancel := context.WithCancel(context.Background())
  145. defer cancel()
  146. // get some info
  147. info, err := m.ABCIInfo(ctx)
  148. require.NoError(t, err)
  149. assert.Equal(t, `{"size":0}`, info.Response.GetData())
  150. // add a key
  151. key, value := "foo", "bar"
  152. tx := fmt.Sprintf("%s=%s", key, value)
  153. res, err := m.BroadcastTxCommit(ctx, types.Tx(tx))
  154. require.NoError(t, err)
  155. assert.True(t, res.CheckTx.IsOK())
  156. require.NotNil(t, res.DeliverTx)
  157. assert.True(t, res.DeliverTx.IsOK())
  158. // commit
  159. // TODO: This may not be necessary in the future
  160. if res.Height == -1 {
  161. m.App.Commit()
  162. }
  163. // check the key
  164. _qres, err := m.ABCIQueryWithOptions(
  165. ctx,
  166. "/key",
  167. bytes.HexBytes(key),
  168. client.ABCIQueryOptions{Prove: true},
  169. )
  170. qres := _qres.Response
  171. require.NoError(t, err)
  172. assert.EqualValues(t, value, qres.Value)
  173. // XXX Check proof
  174. }