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.

171 lines
4.7 KiB

8 years ago
8 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/abci/example/dummy"
  9. abci "github.com/tendermint/abci/types"
  10. data "github.com/tendermint/go-wire/data"
  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 := uint64(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.Result{Data: data.Bytes("stand")},
  34. DeliverTx: abci.Result{Data: data.Bytes("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{Trusted: true})
  46. require.Nil(err)
  47. require.NotNil(query)
  48. assert.EqualValues(key, query.Key)
  49. assert.EqualValues(value, query.Value)
  50. assert.Equal(height, query.Height)
  51. // non-commit calls always return errors
  52. _, err = m.BroadcastTxSync(goodTx)
  53. require.NotNil(err)
  54. assert.Equal("must commit", err.Error())
  55. _, err = m.BroadcastTxAsync(goodTx)
  56. require.NotNil(err)
  57. assert.Equal("must commit", err.Error())
  58. // commit depends on the input
  59. _, err = m.BroadcastTxCommit(badTx)
  60. require.NotNil(err)
  61. assert.Equal("bad tx", err.Error())
  62. bres, err := m.BroadcastTxCommit(goodTx)
  63. require.Nil(err, "%+v", err)
  64. assert.EqualValues(0, bres.CheckTx.Code)
  65. assert.EqualValues("stand", bres.CheckTx.Data)
  66. assert.EqualValues("deliver", bres.DeliverTx.Data)
  67. }
  68. func TestABCIRecorder(t *testing.T) {
  69. assert, require := assert.New(t), require.New(t)
  70. m := mock.ABCIMock{
  71. Info: mock.Call{Response: abci.ResponseInfo{
  72. Data: "data",
  73. Version: "v0.9.9",
  74. }},
  75. Query: mock.Call{Error: errors.New("query")},
  76. Broadcast: mock.Call{Error: errors.New("broadcast")},
  77. BroadcastCommit: mock.Call{Error: errors.New("broadcast_commit")},
  78. }
  79. r := mock.NewABCIRecorder(m)
  80. require.Equal(0, len(r.Calls))
  81. r.ABCIInfo()
  82. r.ABCIQueryWithOptions("path", data.Bytes("data"), client.ABCIQueryOptions{Trusted: false})
  83. require.Equal(2, len(r.Calls))
  84. info := r.Calls[0]
  85. assert.Equal("abci_info", info.Name)
  86. assert.Nil(info.Error)
  87. assert.Nil(info.Args)
  88. require.NotNil(info.Response)
  89. ir, ok := info.Response.(*ctypes.ResultABCIInfo)
  90. require.True(ok)
  91. assert.Equal("data", ir.Response.Data)
  92. assert.Equal("v0.9.9", ir.Response.Version)
  93. query := r.Calls[1]
  94. assert.Equal("abci_query", query.Name)
  95. assert.Nil(query.Response)
  96. require.NotNil(query.Error)
  97. assert.Equal("query", query.Error.Error())
  98. require.NotNil(query.Args)
  99. qa, ok := query.Args.(mock.QueryArgs)
  100. require.True(ok)
  101. assert.Equal("path", qa.Path)
  102. assert.EqualValues("data", qa.Data)
  103. assert.False(qa.Trusted)
  104. // now add some broadcasts
  105. txs := []types.Tx{{1}, {2}, {3}}
  106. r.BroadcastTxCommit(txs[0])
  107. r.BroadcastTxSync(txs[1])
  108. r.BroadcastTxAsync(txs[2])
  109. require.Equal(5, len(r.Calls))
  110. bc := r.Calls[2]
  111. assert.Equal("broadcast_tx_commit", bc.Name)
  112. assert.Nil(bc.Response)
  113. require.NotNil(bc.Error)
  114. assert.EqualValues(bc.Args, txs[0])
  115. bs := r.Calls[3]
  116. assert.Equal("broadcast_tx_sync", bs.Name)
  117. assert.Nil(bs.Response)
  118. require.NotNil(bs.Error)
  119. assert.EqualValues(bs.Args, txs[1])
  120. ba := r.Calls[4]
  121. assert.Equal("broadcast_tx_async", ba.Name)
  122. assert.Nil(ba.Response)
  123. require.NotNil(ba.Error)
  124. assert.EqualValues(ba.Args, txs[2])
  125. }
  126. func TestABCIApp(t *testing.T) {
  127. assert, require := assert.New(t), require.New(t)
  128. app := dummy.NewDummyApplication()
  129. m := mock.ABCIApp{app}
  130. // get some info
  131. info, err := m.ABCIInfo()
  132. require.Nil(err)
  133. assert.Equal(`{"size":0}`, info.Response.GetData())
  134. // add a key
  135. key, value := "foo", "bar"
  136. tx := fmt.Sprintf("%s=%s", key, value)
  137. res, err := m.BroadcastTxCommit(types.Tx(tx))
  138. require.Nil(err)
  139. assert.True(res.CheckTx.Code.IsOK())
  140. require.NotNil(res.DeliverTx)
  141. assert.True(res.DeliverTx.Code.IsOK())
  142. // check the key
  143. qres, err := m.ABCIQueryWithOptions("/key", data.Bytes(key), client.ABCIQueryOptions{Trusted: true})
  144. require.Nil(err)
  145. assert.EqualValues(value, qres.Value)
  146. }