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.

153 lines
4.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package proxy
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. abcicli "github.com/tendermint/tendermint/abci/client"
  7. "github.com/tendermint/tendermint/abci/example/kvstore"
  8. "github.com/tendermint/tendermint/abci/server"
  9. "github.com/tendermint/tendermint/abci/types"
  10. cmn "github.com/tendermint/tendermint/libs/common"
  11. "github.com/tendermint/tendermint/libs/log"
  12. )
  13. //----------------------------------------
  14. type AppConnTest interface {
  15. EchoAsync(string) *abcicli.ReqRes
  16. FlushSync() error
  17. InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
  18. }
  19. type appConnTest struct {
  20. appConn abcicli.Client
  21. }
  22. func NewAppConnTest(appConn abcicli.Client) AppConnTest {
  23. return &appConnTest{appConn}
  24. }
  25. func (app *appConnTest) EchoAsync(msg string) *abcicli.ReqRes {
  26. return app.appConn.EchoAsync(msg)
  27. }
  28. func (app *appConnTest) FlushSync() error {
  29. return app.appConn.FlushSync()
  30. }
  31. func (app *appConnTest) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
  32. return app.appConn.InfoSync(req)
  33. }
  34. //----------------------------------------
  35. var SOCKET = "socket"
  36. func TestEcho(t *testing.T) {
  37. sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
  38. clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
  39. // Start server
  40. s := server.NewSocketServer(sockPath, kvstore.NewKVStoreApplication())
  41. s.SetLogger(log.TestingLogger().With("module", "abci-server"))
  42. if err := s.Start(); err != nil {
  43. t.Fatalf("Error starting socket server: %v", err.Error())
  44. }
  45. defer s.Stop()
  46. // Start client
  47. cli, err := clientCreator.NewABCIClient()
  48. if err != nil {
  49. t.Fatalf("Error creating ABCI client: %v", err.Error())
  50. }
  51. cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
  52. if err := cli.Start(); err != nil {
  53. t.Fatalf("Error starting ABCI client: %v", err.Error())
  54. }
  55. proxy := NewAppConnTest(cli)
  56. t.Log("Connected")
  57. for i := 0; i < 1000; i++ {
  58. proxy.EchoAsync(fmt.Sprintf("echo-%v", i))
  59. }
  60. if err := proxy.FlushSync(); err != nil {
  61. t.Error(err)
  62. }
  63. }
  64. func BenchmarkEcho(b *testing.B) {
  65. b.StopTimer() // Initialize
  66. sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
  67. clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
  68. // Start server
  69. s := server.NewSocketServer(sockPath, kvstore.NewKVStoreApplication())
  70. s.SetLogger(log.TestingLogger().With("module", "abci-server"))
  71. if err := s.Start(); err != nil {
  72. b.Fatalf("Error starting socket server: %v", err.Error())
  73. }
  74. defer s.Stop()
  75. // Start client
  76. cli, err := clientCreator.NewABCIClient()
  77. if err != nil {
  78. b.Fatalf("Error creating ABCI client: %v", err.Error())
  79. }
  80. cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
  81. if err := cli.Start(); err != nil {
  82. b.Fatalf("Error starting ABCI client: %v", err.Error())
  83. }
  84. proxy := NewAppConnTest(cli)
  85. b.Log("Connected")
  86. echoString := strings.Repeat(" ", 200)
  87. b.StartTimer() // Start benchmarking tests
  88. for i := 0; i < b.N; i++ {
  89. proxy.EchoAsync(echoString)
  90. }
  91. if err := proxy.FlushSync(); err != nil {
  92. b.Error(err)
  93. }
  94. b.StopTimer()
  95. // info := proxy.InfoSync(types.RequestInfo{""})
  96. //b.Log("N: ", b.N, info)
  97. }
  98. func TestInfo(t *testing.T) {
  99. sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
  100. clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
  101. // Start server
  102. s := server.NewSocketServer(sockPath, kvstore.NewKVStoreApplication())
  103. s.SetLogger(log.TestingLogger().With("module", "abci-server"))
  104. if err := s.Start(); err != nil {
  105. t.Fatalf("Error starting socket server: %v", err.Error())
  106. }
  107. defer s.Stop()
  108. // Start client
  109. cli, err := clientCreator.NewABCIClient()
  110. if err != nil {
  111. t.Fatalf("Error creating ABCI client: %v", err.Error())
  112. }
  113. cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
  114. if err := cli.Start(); err != nil {
  115. t.Fatalf("Error starting ABCI client: %v", err.Error())
  116. }
  117. proxy := NewAppConnTest(cli)
  118. t.Log("Connected")
  119. resInfo, err := proxy.InfoSync(types.RequestInfo{Version: ""})
  120. if err != nil {
  121. t.Errorf("Unexpected error: %v", err)
  122. }
  123. if string(resInfo.Data) != "{\"size\":0}" {
  124. t.Error("Expected ResponseInfo with one element '{\"size\":0}' but got something else")
  125. }
  126. }