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.

148 lines
3.8 KiB

8 years ago
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/abci/client"
  6. "github.com/tendermint/abci/example/dummy"
  7. "github.com/tendermint/abci/server"
  8. "github.com/tendermint/abci/types"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. "github.com/tendermint/tmlibs/log"
  11. )
  12. //----------------------------------------
  13. type AppConnTest interface {
  14. EchoAsync(string) *abcicli.ReqRes
  15. FlushSync() error
  16. InfoSync() (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() (types.ResponseInfo, error) {
  31. return app.appConn.InfoSync()
  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, dummy.NewDummyApplication())
  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. proxy.FlushSync()
  60. }
  61. func BenchmarkEcho(b *testing.B) {
  62. b.StopTimer() // Initialize
  63. sockPath := cmn.Fmt("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
  64. clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
  65. // Start server
  66. s := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
  67. s.SetLogger(log.TestingLogger().With("module", "abci-server"))
  68. if _, err := s.Start(); err != nil {
  69. b.Fatalf("Error starting socket server: %v", err.Error())
  70. }
  71. defer s.Stop()
  72. // Start client
  73. cli, err := clientCreator.NewABCIClient()
  74. if err != nil {
  75. b.Fatalf("Error creating ABCI client: %v", err.Error())
  76. }
  77. cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
  78. if _, err := cli.Start(); err != nil {
  79. b.Fatalf("Error starting ABCI client: %v", err.Error())
  80. }
  81. proxy := NewAppConnTest(cli)
  82. b.Log("Connected")
  83. echoString := strings.Repeat(" ", 200)
  84. b.StartTimer() // Start benchmarking tests
  85. for i := 0; i < b.N; i++ {
  86. proxy.EchoAsync(echoString)
  87. }
  88. proxy.FlushSync()
  89. b.StopTimer()
  90. // info := proxy.InfoSync()
  91. //b.Log("N: ", b.N, info)
  92. }
  93. func TestInfo(t *testing.T) {
  94. sockPath := cmn.Fmt("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
  95. clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
  96. // Start server
  97. s := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
  98. s.SetLogger(log.TestingLogger().With("module", "abci-server"))
  99. if _, err := s.Start(); err != nil {
  100. t.Fatalf("Error starting socket server: %v", err.Error())
  101. }
  102. defer s.Stop()
  103. // Start client
  104. cli, err := clientCreator.NewABCIClient()
  105. if err != nil {
  106. t.Fatalf("Error creating ABCI client: %v", err.Error())
  107. }
  108. cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
  109. if _, err := cli.Start(); err != nil {
  110. t.Fatalf("Error starting ABCI client: %v", err.Error())
  111. }
  112. proxy := NewAppConnTest(cli)
  113. t.Log("Connected")
  114. resInfo, err := proxy.InfoSync()
  115. if err != nil {
  116. t.Errorf("Unexpected error: %v", err)
  117. }
  118. if string(resInfo.Data) != "{\"size\":0}" {
  119. t.Error("Expected ResponseInfo with one element '{\"size\":0}' but got something else")
  120. }
  121. }