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.

131 lines
3.3 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "time"
  8. . "github.com/tendermint/go-common"
  9. "github.com/tendermint/go-process"
  10. "github.com/tendermint/tmsp/client"
  11. "github.com/tendermint/tmsp/types"
  12. )
  13. var tmspPtr = flag.String("tmsp", "socket", "socket or grpc")
  14. func main() {
  15. flag.Parse()
  16. // Run tests
  17. testBasic()
  18. fmt.Println("Success!")
  19. }
  20. func testBasic() {
  21. fmt.Println("Running basic tests")
  22. appProc := startApp()
  23. defer appProc.StopProcess(true)
  24. client := startClient()
  25. defer client.Stop()
  26. setOption(client, "serial", "on")
  27. commit(client, nil)
  28. appendTx(client, []byte("abc"), types.CodeType_BadNonce, nil)
  29. commit(client, nil)
  30. appendTx(client, []byte{0x00}, types.CodeType_OK, nil)
  31. commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1})
  32. appendTx(client, []byte{0x00}, types.CodeType_BadNonce, nil)
  33. appendTx(client, []byte{0x01}, types.CodeType_OK, nil)
  34. appendTx(client, []byte{0x00, 0x02}, types.CodeType_OK, nil)
  35. appendTx(client, []byte{0x00, 0x03}, types.CodeType_OK, nil)
  36. appendTx(client, []byte{0x00, 0x00, 0x04}, types.CodeType_OK, nil)
  37. appendTx(client, []byte{0x00, 0x00, 0x06}, types.CodeType_BadNonce, nil)
  38. commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5})
  39. }
  40. //----------------------------------------
  41. func startApp() *process.Process {
  42. counterApp := os.Getenv("COUNTER_APP")
  43. if counterApp == "" {
  44. panic("No COUNTER_APP specified")
  45. }
  46. // Start the app
  47. //outBuf := NewBufferCloser(nil)
  48. proc, err := process.StartProcess("counter_app",
  49. "bash",
  50. []string{"-c", counterApp},
  51. nil,
  52. os.Stdout,
  53. )
  54. if err != nil {
  55. panic("running counter_app: " + err.Error())
  56. }
  57. // TODO a better way to handle this?
  58. time.Sleep(time.Second)
  59. return proc
  60. }
  61. func startClient() tmspcli.Client {
  62. // Start client
  63. client, err := tmspcli.NewClient("tcp://127.0.0.1:46658", *tmspPtr, true)
  64. if err != nil {
  65. panic("connecting to counter_app: " + err.Error())
  66. }
  67. return client
  68. }
  69. func setOption(client tmspcli.Client, key, value string) {
  70. res := client.SetOptionSync(key, value)
  71. _, _, log := res.Code, res.Data, res.Log
  72. if res.IsErr() {
  73. panic(Fmt("setting %v=%v: \nlog: %v", key, value, log))
  74. }
  75. }
  76. func commit(client tmspcli.Client, hashExp []byte) {
  77. res := client.CommitSync()
  78. _, data, log := res.Code, res.Data, res.Log
  79. if res.IsErr() {
  80. panic(Fmt("committing %v\nlog: %v", log))
  81. }
  82. if !bytes.Equal(res.Data, hashExp) {
  83. panic(Fmt("Commit hash was unexpected. Got %X expected %X",
  84. data, hashExp))
  85. }
  86. }
  87. func appendTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
  88. res := client.AppendTxSync(txBytes)
  89. code, data, log := res.Code, res.Data, res.Log
  90. if code != codeExp {
  91. panic(Fmt("AppendTx response code was unexpected. Got %v expected %v. Log: %v",
  92. code, codeExp, log))
  93. }
  94. if !bytes.Equal(data, dataExp) {
  95. panic(Fmt("AppendTx response data was unexpected. Got %X expected %X",
  96. data, dataExp))
  97. }
  98. }
  99. func checkTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
  100. res := client.CheckTxSync(txBytes)
  101. code, data, log := res.Code, res.Data, res.Log
  102. if res.IsErr() {
  103. panic(Fmt("checking tx %X: %v\nlog: %v", txBytes, log))
  104. }
  105. if code != codeExp {
  106. panic(Fmt("CheckTx response code was unexpected. Got %v expected %v. Log: %v",
  107. code, codeExp, log))
  108. }
  109. if !bytes.Equal(data, dataExp) {
  110. panic(Fmt("CheckTx response data was unexpected. Got %X expected %X",
  111. data, dataExp))
  112. }
  113. }