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.

162 lines
4.0 KiB

  1. package rpctest
  2. import (
  3. "fmt"
  4. "testing"
  5. _ "github.com/tendermint/tendermint/config/tendermint_test"
  6. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. //--------------------------------------------------------------------------------
  10. // Test the HTTP client
  11. func TestURIStatus(t *testing.T) {
  12. tmResult := new(ctypes.TMResult)
  13. _, err := clientURI.Call("status", map[string]interface{}{}, tmResult)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. testStatus(t, tmResult)
  18. }
  19. func TestJSONStatus(t *testing.T) {
  20. tmResult := new(ctypes.TMResult)
  21. _, err := clientJSON.Call("status", []interface{}{}, tmResult)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. testStatus(t, tmResult)
  26. }
  27. func testStatus(t *testing.T, statusI interface{}) {
  28. tmRes := statusI.(*ctypes.TMResult)
  29. status := (*tmRes).(*ctypes.ResultStatus)
  30. if status.NodeInfo.Network != chainID {
  31. t.Fatal(fmt.Errorf("ChainID mismatch: got %s expected %s",
  32. status.NodeInfo.Network, chainID))
  33. }
  34. }
  35. /*func TestURIBroadcastTx(t *testing.T) {
  36. testBroadcastTx(t, "HTTP")
  37. }*/
  38. /*func TestJSONBroadcastTx(t *testing.T) {
  39. testBroadcastTx(t, "JSONRPC")
  40. }*/
  41. // TODO
  42. /*
  43. func testBroadcastTx(t *testing.T, typ string) {
  44. amt := int64(100)
  45. toAddr := user[1].Address
  46. tx := makeDefaultSendTxSigned(t, typ, toAddr, amt)
  47. receipt := broadcastTx(t, typ, tx)
  48. if receipt.CreatesContract > 0 {
  49. t.Fatal("This tx does not create a contract")
  50. }
  51. if len(receipt.TxHash) == 0 {
  52. t.Fatal("Failed to compute tx hash")
  53. }
  54. pool := node.MempoolReactor().Mempool
  55. txs := pool.GetProposalTxs()
  56. if len(txs) != mempoolCount {
  57. t.Fatalf("The mem pool has %d txs. Expected %d", len(txs), mempoolCount)
  58. }
  59. tx2 := txs[mempoolCount-1].(*types.SendTx)
  60. n, err := new(int64), new(error)
  61. buf1, buf2 := new(bytes.Buffer), new(bytes.Buffer)
  62. tx.WriteSignBytes(chainID, buf1, n, err)
  63. tx2.WriteSignBytes(chainID, buf2, n, err)
  64. if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 {
  65. t.Fatal("inconsistent hashes for mempool tx and sent tx")
  66. }
  67. }*/
  68. //--------------------------------------------------------------------------------
  69. // Test the websocket service
  70. var wsTyp = "JSONRPC"
  71. // make a simple connection to the server
  72. func TestWSConnect(t *testing.T) {
  73. wsc := newWSClient(t)
  74. wsc.Stop()
  75. }
  76. // receive a new block message
  77. func TestWSNewBlock(t *testing.T) {
  78. wsc := newWSClient(t)
  79. eid := types.EventStringNewBlock()
  80. subscribe(t, wsc, eid)
  81. defer func() {
  82. unsubscribe(t, wsc, eid)
  83. wsc.Stop()
  84. }()
  85. waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error {
  86. fmt.Println("Check:", b)
  87. return nil
  88. })
  89. }
  90. // receive a few new block messages in a row, with increasing height
  91. func TestWSBlockchainGrowth(t *testing.T) {
  92. if testing.Short() {
  93. t.Skip("skipping test in short mode.")
  94. }
  95. wsc := newWSClient(t)
  96. eid := types.EventStringNewBlock()
  97. subscribe(t, wsc, eid)
  98. defer func() {
  99. unsubscribe(t, wsc, eid)
  100. wsc.Stop()
  101. }()
  102. // listen for NewBlock, ensure height increases by 1
  103. var initBlockN int
  104. for i := 0; i < 3; i++ {
  105. waitForEvent(t, wsc, eid, true, func() {}, func(eid string, eventData interface{}) error {
  106. block := eventData.(types.EventDataNewBlock).Block
  107. if i == 0 {
  108. initBlockN = block.Header.Height
  109. } else {
  110. if block.Header.Height != initBlockN+i {
  111. return fmt.Errorf("Expected block %d, got block %d", initBlockN+i, block.Header.Height)
  112. }
  113. }
  114. return nil
  115. })
  116. }
  117. }
  118. /* TODO: this with dummy app..
  119. func TestWSDoubleFire(t *testing.T) {
  120. if testing.Short() {
  121. t.Skip("skipping test in short mode.")
  122. }
  123. con := newWSCon(t)
  124. eid := types.EventStringAccInput(user[0].Address)
  125. subscribe(t, con, eid)
  126. defer func() {
  127. unsubscribe(t, con, eid)
  128. con.Close()
  129. }()
  130. amt := int64(100)
  131. toAddr := user[1].Address
  132. // broadcast the transaction, wait to hear about it
  133. waitForEvent(t, con, eid, true, func() {
  134. tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
  135. broadcastTx(t, wsTyp, tx)
  136. }, func(eid string, b []byte) error {
  137. return nil
  138. })
  139. // but make sure we don't hear about it twice
  140. waitForEvent(t, con, eid, false, func() {
  141. }, func(eid string, b []byte) error {
  142. return nil
  143. })
  144. }*/