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.

212 lines
5.9 KiB

  1. package rpctest
  2. import (
  3. "fmt"
  4. "testing"
  5. _ "github.com/tendermint/tendermint/config/tendermint_test"
  6. "github.com/tendermint/tendermint/types"
  7. )
  8. var wsTyp = "JSONRPC"
  9. //--------------------------------------------------------------------------------
  10. // Test the websocket service
  11. // make a simple connection to the server
  12. func TestWSConnect(t *testing.T) {
  13. con := newWSCon(t)
  14. con.Close()
  15. }
  16. // receive a new block message
  17. func _TestWSNewBlock(t *testing.T) {
  18. con := newWSCon(t)
  19. eid := types.EventStringNewBlock()
  20. subscribe(t, con, eid)
  21. defer func() {
  22. unsubscribe(t, con, eid)
  23. con.Close()
  24. }()
  25. waitForEvent(t, con, eid, true, func() {}, func(eid string, b []byte) error {
  26. fmt.Println("Check:", string(b))
  27. return nil
  28. })
  29. }
  30. // receive a few new block messages in a row, with increasing height
  31. func TestWSBlockchainGrowth(t *testing.T) {
  32. if testing.Short() {
  33. t.Skip("skipping test in short mode.")
  34. }
  35. con := newWSCon(t)
  36. eid := types.EventStringNewBlock()
  37. subscribe(t, con, eid)
  38. defer func() {
  39. unsubscribe(t, con, eid)
  40. con.Close()
  41. }()
  42. // listen for NewBlock, ensure height increases by 1
  43. unmarshalValidateBlockchain(t, con, eid)
  44. }
  45. // send a transaction and validate the events from listening for both sender and receiver
  46. func TestWSSend(t *testing.T) {
  47. toAddr := user[1].Address
  48. amt := int64(100)
  49. con := newWSCon(t)
  50. eidInput := types.EventStringAccInput(user[0].Address)
  51. eidOutput := types.EventStringAccOutput(toAddr)
  52. subscribe(t, con, eidInput)
  53. subscribe(t, con, eidOutput)
  54. defer func() {
  55. unsubscribe(t, con, eidInput)
  56. unsubscribe(t, con, eidOutput)
  57. con.Close()
  58. }()
  59. waitForEvent(t, con, eidInput, true, func() {
  60. tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
  61. broadcastTx(t, wsTyp, tx)
  62. }, unmarshalValidateSend(amt, toAddr))
  63. waitForEvent(t, con, eidOutput, true, func() {}, unmarshalValidateSend(amt, toAddr))
  64. }
  65. // ensure events are only fired once for a given transaction
  66. func TestWSDoubleFire(t *testing.T) {
  67. if testing.Short() {
  68. t.Skip("skipping test in short mode.")
  69. }
  70. con := newWSCon(t)
  71. eid := types.EventStringAccInput(user[0].Address)
  72. subscribe(t, con, eid)
  73. defer func() {
  74. unsubscribe(t, con, eid)
  75. con.Close()
  76. }()
  77. amt := int64(100)
  78. toAddr := user[1].Address
  79. // broadcast the transaction, wait to hear about it
  80. waitForEvent(t, con, eid, true, func() {
  81. tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
  82. broadcastTx(t, wsTyp, tx)
  83. }, func(eid string, b []byte) error {
  84. return nil
  85. })
  86. // but make sure we don't hear about it twice
  87. waitForEvent(t, con, eid, false, func() {
  88. }, func(eid string, b []byte) error {
  89. return nil
  90. })
  91. }
  92. // create a contract, wait for the event, and send it a msg, validate the return
  93. func TestWSCallWait(t *testing.T) {
  94. if testing.Short() {
  95. t.Skip("skipping test in short mode.")
  96. }
  97. con := newWSCon(t)
  98. eid1 := types.EventStringAccInput(user[0].Address)
  99. subscribe(t, con, eid1)
  100. defer func() {
  101. unsubscribe(t, con, eid1)
  102. con.Close()
  103. }()
  104. amt, gasLim, fee := int64(10000), int64(1000), int64(1000)
  105. code, returnCode, returnVal := simpleContract()
  106. var contractAddr []byte
  107. // wait for the contract to be created
  108. waitForEvent(t, con, eid1, true, func() {
  109. tx := makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
  110. receipt := broadcastTx(t, wsTyp, tx)
  111. contractAddr = receipt.ContractAddr
  112. }, unmarshalValidateCall(amt, returnCode))
  113. // susbscribe to the new contract
  114. amt = int64(10001)
  115. eid2 := types.EventStringAccOutput(contractAddr)
  116. subscribe(t, con, eid2)
  117. defer func() {
  118. unsubscribe(t, con, eid2)
  119. }()
  120. // get the return value from a call
  121. data := []byte{0x1}
  122. waitForEvent(t, con, eid2, true, func() {
  123. tx := makeDefaultCallTx(t, wsTyp, contractAddr, data, amt, gasLim, fee)
  124. receipt := broadcastTx(t, wsTyp, tx)
  125. contractAddr = receipt.ContractAddr
  126. }, unmarshalValidateCall(amt, returnVal))
  127. }
  128. // create a contract and send it a msg without waiting. wait for contract event
  129. // and validate return
  130. func TestWSCallNoWait(t *testing.T) {
  131. if testing.Short() {
  132. t.Skip("skipping test in short mode.")
  133. }
  134. con := newWSCon(t)
  135. amt, gasLim, fee := int64(10000), int64(1000), int64(1000)
  136. code, _, returnVal := simpleContract()
  137. tx := makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
  138. receipt := broadcastTx(t, wsTyp, tx)
  139. contractAddr := receipt.ContractAddr
  140. // susbscribe to the new contract
  141. amt = int64(10001)
  142. eid := types.EventStringAccOutput(contractAddr)
  143. subscribe(t, con, eid)
  144. defer func() {
  145. unsubscribe(t, con, eid)
  146. con.Close()
  147. }()
  148. // get the return value from a call
  149. data := []byte{0x1}
  150. waitForEvent(t, con, eid, true, func() {
  151. tx := makeDefaultCallTx(t, wsTyp, contractAddr, data, amt, gasLim, fee)
  152. broadcastTx(t, wsTyp, tx)
  153. }, unmarshalValidateCall(amt, returnVal))
  154. }
  155. // create two contracts, one of which calls the other
  156. func TestWSCallCall(t *testing.T) {
  157. if testing.Short() {
  158. t.Skip("skipping test in short mode.")
  159. }
  160. con := newWSCon(t)
  161. amt, gasLim, fee := int64(10000), int64(1000), int64(1000)
  162. code, _, returnVal := simpleContract()
  163. txid := new([]byte)
  164. // deploy the two contracts
  165. tx := makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
  166. receipt := broadcastTx(t, wsTyp, tx)
  167. contractAddr1 := receipt.ContractAddr
  168. code, _, _ = simpleCallContract(contractAddr1)
  169. tx = makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
  170. receipt = broadcastTx(t, wsTyp, tx)
  171. contractAddr2 := receipt.ContractAddr
  172. // susbscribe to the new contracts
  173. amt = int64(10001)
  174. eid1 := types.EventStringAccReceive(contractAddr1)
  175. subscribe(t, con, eid1)
  176. defer func() {
  177. unsubscribe(t, con, eid1)
  178. con.Close()
  179. }()
  180. // call contract2, which should call contract1, and wait for ev1
  181. // let the contract get created first
  182. waitForEvent(t, con, eid1, true, func() {
  183. }, func(eid string, b []byte) error {
  184. return nil
  185. })
  186. // call it
  187. waitForEvent(t, con, eid1, true, func() {
  188. tx := makeDefaultCallTx(t, wsTyp, contractAddr2, nil, amt, gasLim, fee)
  189. broadcastTx(t, wsTyp, tx)
  190. *txid = types.TxID(chainID, tx)
  191. }, unmarshalValidateCallCall(user[0].Address, returnVal, txid))
  192. }