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.

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