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.

130 lines
3.5 KiB

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