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.

214 lines
5.4 KiB

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