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.

139 lines
3.7 KiB

  1. package client_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. cmn "github.com/tendermint/tmlibs/common"
  7. "github.com/tendermint/tendermint/rpc/client"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. // MakeTxKV returns a text transaction, allong with expected key, value pair
  11. func MakeTxKV() ([]byte, []byte, []byte) {
  12. k := []byte(cmn.RandStr(8))
  13. v := []byte(cmn.RandStr(8))
  14. return k, v, append(k, append([]byte("="), v...)...)
  15. }
  16. func TestHeaderEvents(t *testing.T) {
  17. require := require.New(t)
  18. for i, c := range GetClients() {
  19. // start for this test it if it wasn't already running
  20. if !c.IsRunning() {
  21. // if so, then we start it, listen, and stop it.
  22. st, err := c.Start()
  23. require.Nil(err, "%d: %+v", i, err)
  24. require.True(st, "%d", i)
  25. defer c.Stop()
  26. }
  27. evtTyp := types.EventStringNewBlockHeader()
  28. evt, err := client.WaitForOneEvent(c, evtTyp, 1*time.Second)
  29. require.Nil(err, "%d: %+v", i, err)
  30. _, ok := evt.Unwrap().(types.EventDataNewBlockHeader)
  31. require.True(ok, "%d: %#v", i, evt)
  32. // TODO: more checks...
  33. }
  34. }
  35. func TestBlockEvents(t *testing.T) {
  36. require := require.New(t)
  37. for i, c := range GetClients() {
  38. // start for this test it if it wasn't already running
  39. if !c.IsRunning() {
  40. // if so, then we start it, listen, and stop it.
  41. st, err := c.Start()
  42. require.Nil(err, "%d: %+v", i, err)
  43. require.True(st, "%d", i)
  44. defer c.Stop()
  45. }
  46. // listen for a new block; ensure height increases by 1
  47. var firstBlockHeight int
  48. for i := 0; i < 3; i++ {
  49. evtTyp := types.EventStringNewBlock()
  50. evt, err := client.WaitForOneEvent(c, evtTyp, 1*time.Second)
  51. require.Nil(err, "%d: %+v", i, err)
  52. blockEvent, ok := evt.Unwrap().(types.EventDataNewBlock)
  53. require.True(ok, "%d: %#v", i, evt)
  54. block := blockEvent.Block
  55. if i == 0 {
  56. firstBlockHeight = block.Header.Height
  57. continue
  58. }
  59. require.Equal(block.Header.Height, firstBlockHeight+i)
  60. }
  61. }
  62. }
  63. func TestTxEventsSentWithBroadcastTxAsync(t *testing.T) {
  64. require := require.New(t)
  65. for i, c := range GetClients() {
  66. // start for this test it if it wasn't already running
  67. if !c.IsRunning() {
  68. // if so, then we start it, listen, and stop it.
  69. st, err := c.Start()
  70. require.Nil(err, "%d: %+v", i, err)
  71. require.True(st, "%d", i)
  72. defer c.Stop()
  73. }
  74. // make the tx
  75. _, _, tx := MakeTxKV()
  76. evtTyp := types.EventStringTx(types.Tx(tx))
  77. // send async
  78. txres, err := c.BroadcastTxAsync(tx)
  79. require.Nil(err, "%+v", err)
  80. require.True(txres.Code.IsOK())
  81. // and wait for confirmation
  82. evt, err := client.WaitForOneEvent(c, evtTyp, 1*time.Second)
  83. require.Nil(err, "%d: %+v", i, err)
  84. // and make sure it has the proper info
  85. txe, ok := evt.Unwrap().(types.EventDataTx)
  86. require.True(ok, "%d: %#v", i, evt)
  87. // make sure this is the proper tx
  88. require.EqualValues(tx, txe.Tx)
  89. require.True(txe.Code.IsOK())
  90. }
  91. }
  92. func TestTxEventsSentWithBroadcastTxSync(t *testing.T) {
  93. require := require.New(t)
  94. for i, c := range GetClients() {
  95. // start for this test it if it wasn't already running
  96. if !c.IsRunning() {
  97. // if so, then we start it, listen, and stop it.
  98. st, err := c.Start()
  99. require.Nil(err, "%d: %+v", i, err)
  100. require.True(st, "%d", i)
  101. defer c.Stop()
  102. }
  103. // make the tx
  104. _, _, tx := MakeTxKV()
  105. evtTyp := types.EventStringTx(types.Tx(tx))
  106. // send async
  107. txres, err := c.BroadcastTxSync(tx)
  108. require.Nil(err, "%+v", err)
  109. require.True(txres.Code.IsOK())
  110. // and wait for confirmation
  111. evt, err := client.WaitForOneEvent(c, evtTyp, 1*time.Second)
  112. require.Nil(err, "%d: %+v", i, err)
  113. // and make sure it has the proper info
  114. txe, ok := evt.Unwrap().(types.EventDataTx)
  115. require.True(ok, "%d: %#v", i, evt)
  116. // make sure this is the proper tx
  117. require.EqualValues(tx, txe.Tx)
  118. require.True(txe.Code.IsOK())
  119. }
  120. }