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.

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