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.

170 lines
4.5 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. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  12. "github.com/tendermint/tendermint/types"
  13. "github.com/tendermint/tendermint/rpc/client/mock"
  14. )
  15. func TestABCIMock(t *testing.T) {
  16. assert, require := assert.New(t), require.New(t)
  17. key, value := []byte("foo"), []byte("bar")
  18. height := uint64(10)
  19. goodTx := types.Tx{0x01, 0xff}
  20. badTx := types.Tx{0x12, 0x21}
  21. m := mock.ABCIMock{
  22. Info: mock.Call{Error: errors.New("foobar")},
  23. Query: mock.Call{Response: abci.ResponseQuery{
  24. Key: key,
  25. Value: value,
  26. Height: height,
  27. }},
  28. // Broadcast commit depends on call
  29. BroadcastCommit: mock.Call{
  30. Args: goodTx,
  31. Response: &ctypes.ResultBroadcastTxCommit{
  32. CheckTx: abci.Result{Data: data.Bytes("stand")},
  33. DeliverTx: abci.Result{Data: data.Bytes("deliver")},
  34. },
  35. Error: errors.New("bad tx"),
  36. },
  37. Broadcast: mock.Call{Error: errors.New("must commit")},
  38. }
  39. // now, let's try to make some calls
  40. _, err := m.ABCIInfo()
  41. require.NotNil(err)
  42. assert.Equal("foobar", err.Error())
  43. // query always returns the response
  44. query, err := m.ABCIQuery("/", nil, false)
  45. require.Nil(err)
  46. require.NotNil(query)
  47. assert.EqualValues(key, query.Key)
  48. assert.EqualValues(value, query.Value)
  49. assert.Equal(height, query.Height)
  50. // non-commit calls always return errors
  51. _, err = m.BroadcastTxSync(goodTx)
  52. require.NotNil(err)
  53. assert.Equal("must commit", err.Error())
  54. _, err = m.BroadcastTxAsync(goodTx)
  55. require.NotNil(err)
  56. assert.Equal("must commit", err.Error())
  57. // commit depends on the input
  58. _, err = m.BroadcastTxCommit(badTx)
  59. require.NotNil(err)
  60. assert.Equal("bad tx", err.Error())
  61. bres, err := m.BroadcastTxCommit(goodTx)
  62. require.Nil(err, "%+v", err)
  63. assert.EqualValues(0, bres.CheckTx.Code)
  64. assert.EqualValues("stand", bres.CheckTx.Data)
  65. assert.EqualValues("deliver", bres.DeliverTx.Data)
  66. }
  67. func TestABCIRecorder(t *testing.T) {
  68. assert, require := assert.New(t), require.New(t)
  69. m := mock.ABCIMock{
  70. Info: mock.Call{Response: abci.ResponseInfo{
  71. Data: "data",
  72. Version: "v0.9.9",
  73. }},
  74. Query: mock.Call{Error: errors.New("query")},
  75. Broadcast: mock.Call{Error: errors.New("broadcast")},
  76. BroadcastCommit: mock.Call{Error: errors.New("broadcast_commit")},
  77. }
  78. r := mock.NewABCIRecorder(m)
  79. require.Equal(0, len(r.Calls))
  80. r.ABCIInfo()
  81. r.ABCIQuery("path", data.Bytes("data"), true)
  82. require.Equal(2, len(r.Calls))
  83. info := r.Calls[0]
  84. assert.Equal("abci_info", info.Name)
  85. assert.Nil(info.Error)
  86. assert.Nil(info.Args)
  87. require.NotNil(info.Response)
  88. ir, ok := info.Response.(*ctypes.ResultABCIInfo)
  89. require.True(ok)
  90. assert.Equal("data", ir.Response.Data)
  91. assert.Equal("v0.9.9", ir.Response.Version)
  92. query := r.Calls[1]
  93. assert.Equal("abci_query", query.Name)
  94. assert.Nil(query.Response)
  95. require.NotNil(query.Error)
  96. assert.Equal("query", query.Error.Error())
  97. require.NotNil(query.Args)
  98. qa, ok := query.Args.(mock.QueryArgs)
  99. require.True(ok)
  100. assert.Equal("path", qa.Path)
  101. assert.EqualValues("data", qa.Data)
  102. assert.True(qa.Prove)
  103. // now add some broadcasts
  104. txs := []types.Tx{{1}, {2}, {3}}
  105. r.BroadcastTxCommit(txs[0])
  106. r.BroadcastTxSync(txs[1])
  107. r.BroadcastTxAsync(txs[2])
  108. require.Equal(5, len(r.Calls))
  109. bc := r.Calls[2]
  110. assert.Equal("broadcast_tx_commit", bc.Name)
  111. assert.Nil(bc.Response)
  112. require.NotNil(bc.Error)
  113. assert.EqualValues(bc.Args, txs[0])
  114. bs := r.Calls[3]
  115. assert.Equal("broadcast_tx_sync", bs.Name)
  116. assert.Nil(bs.Response)
  117. require.NotNil(bs.Error)
  118. assert.EqualValues(bs.Args, txs[1])
  119. ba := r.Calls[4]
  120. assert.Equal("broadcast_tx_async", ba.Name)
  121. assert.Nil(ba.Response)
  122. require.NotNil(ba.Error)
  123. assert.EqualValues(ba.Args, txs[2])
  124. }
  125. func TestABCIApp(t *testing.T) {
  126. assert, require := assert.New(t), require.New(t)
  127. app := dummy.NewDummyApplication()
  128. m := mock.ABCIApp{app}
  129. // get some info
  130. info, err := m.ABCIInfo()
  131. require.Nil(err)
  132. assert.Equal(`{"size":0}`, info.Response.GetData())
  133. // add a key
  134. key, value := "foo", "bar"
  135. tx := fmt.Sprintf("%s=%s", key, value)
  136. res, err := m.BroadcastTxCommit(types.Tx(tx))
  137. require.Nil(err)
  138. assert.True(res.CheckTx.Code.IsOK())
  139. require.NotNil(res.DeliverTx)
  140. assert.True(res.DeliverTx.Code.IsOK())
  141. // check the key
  142. qres, err := m.ABCIQuery("/key", data.Bytes(key), false)
  143. require.Nil(err)
  144. assert.EqualValues(value, qres.Value)
  145. }