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.

124 lines
2.7 KiB

  1. package proxy
  2. import (
  3. "strings"
  4. "testing"
  5. . "github.com/tendermint/go-common"
  6. tmspcli "github.com/tendermint/tmsp/client"
  7. "github.com/tendermint/tmsp/example/dummy"
  8. "github.com/tendermint/tmsp/server"
  9. "github.com/tendermint/tmsp/types"
  10. )
  11. //----------------------------------------
  12. type AppConnTest interface {
  13. EchoAsync(string) *tmspcli.ReqRes
  14. FlushSync() error
  15. InfoSync() (res types.Result)
  16. }
  17. type appConnTest struct {
  18. appConn tmspcli.Client
  19. }
  20. func NewAppConnTest(appConn tmspcli.Client) AppConnTest {
  21. return &appConnTest{appConn}
  22. }
  23. func (app *appConnTest) EchoAsync(msg string) *tmspcli.ReqRes {
  24. return app.appConn.EchoAsync(msg)
  25. }
  26. func (app *appConnTest) FlushSync() error {
  27. return app.appConn.FlushSync()
  28. }
  29. func (app *appConnTest) InfoSync() types.Result {
  30. return app.appConn.InfoSync()
  31. }
  32. //----------------------------------------
  33. var SOCKET = "socket"
  34. func TestEcho(t *testing.T) {
  35. sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
  36. clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
  37. // Start server
  38. s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
  39. if err != nil {
  40. Exit(err.Error())
  41. }
  42. defer s.Stop()
  43. // Start client
  44. cli, err := clientCreator.NewTMSPClient()
  45. if err != nil {
  46. Exit(err.Error())
  47. }
  48. proxy := NewAppConnTest(cli)
  49. t.Log("Connected")
  50. for i := 0; i < 1000; i++ {
  51. proxy.EchoAsync(Fmt("echo-%v", i))
  52. }
  53. proxy.FlushSync()
  54. }
  55. func BenchmarkEcho(b *testing.B) {
  56. b.StopTimer() // Initialize
  57. sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
  58. clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
  59. // Start server
  60. s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
  61. if err != nil {
  62. Exit(err.Error())
  63. }
  64. defer s.Stop()
  65. // Start client
  66. cli, err := clientCreator.NewTMSPClient()
  67. if err != nil {
  68. Exit(err.Error())
  69. }
  70. proxy := NewAppConnTest(cli)
  71. b.Log("Connected")
  72. echoString := strings.Repeat(" ", 200)
  73. b.StartTimer() // Start benchmarking tests
  74. for i := 0; i < b.N; i++ {
  75. proxy.EchoAsync(echoString)
  76. }
  77. proxy.FlushSync()
  78. b.StopTimer()
  79. // info := proxy.InfoSync()
  80. //b.Log("N: ", b.N, info)
  81. }
  82. func TestInfo(t *testing.T) {
  83. sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
  84. clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
  85. // Start server
  86. s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
  87. if err != nil {
  88. Exit(err.Error())
  89. }
  90. defer s.Stop()
  91. // Start client
  92. cli, err := clientCreator.NewTMSPClient()
  93. if err != nil {
  94. Exit(err.Error())
  95. }
  96. proxy := NewAppConnTest(cli)
  97. t.Log("Connected")
  98. res := proxy.InfoSync()
  99. if res.IsErr() {
  100. t.Errorf("Unexpected error: %v", err)
  101. }
  102. if string(res.Data) != "size:0" {
  103. t.Error("Expected ResponseInfo with one element 'size:0' but got something else")
  104. }
  105. }