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.

149 lines
4.1 KiB

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