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.

126 lines
3.2 KiB

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