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.

121 lines
2.6 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. // Start server
  37. s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
  38. if err != nil {
  39. Exit(err.Error())
  40. }
  41. defer s.Stop()
  42. // Start client
  43. cli, err := NewTMSPClientDefault(sockPath, SOCKET)
  44. if err != nil {
  45. Exit(err.Error())
  46. }
  47. proxy := NewAppConnTest(cli)
  48. t.Log("Connected")
  49. for i := 0; i < 1000; i++ {
  50. proxy.EchoAsync(Fmt("echo-%v", i))
  51. }
  52. proxy.FlushSync()
  53. }
  54. func BenchmarkEcho(b *testing.B) {
  55. b.StopTimer() // Initialize
  56. sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
  57. // Start server
  58. s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
  59. if err != nil {
  60. Exit(err.Error())
  61. }
  62. defer s.Stop()
  63. // Start client
  64. cli, err := NewTMSPClientDefault(sockPath, SOCKET)
  65. if err != nil {
  66. Exit(err.Error())
  67. }
  68. proxy := NewAppConnTest(cli)
  69. b.Log("Connected")
  70. echoString := strings.Repeat(" ", 200)
  71. b.StartTimer() // Start benchmarking tests
  72. for i := 0; i < b.N; i++ {
  73. proxy.EchoAsync(echoString)
  74. }
  75. proxy.FlushSync()
  76. b.StopTimer()
  77. // info := proxy.InfoSync()
  78. //b.Log("N: ", b.N, info)
  79. }
  80. func TestInfo(t *testing.T) {
  81. sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
  82. // Start server
  83. s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
  84. if err != nil {
  85. Exit(err.Error())
  86. }
  87. defer s.Stop()
  88. // Start client
  89. cli, err := NewTMSPClientDefault(sockPath, SOCKET)
  90. if err != nil {
  91. Exit(err.Error())
  92. }
  93. proxy := NewAppConnTest(cli)
  94. t.Log("Connected")
  95. res := proxy.InfoSync()
  96. if res.IsErr() {
  97. t.Errorf("Unexpected error: %v", err)
  98. }
  99. if string(res.Data) != "size:0" {
  100. t.Error("Expected ResponseInfo with one element 'size:0' but got something else")
  101. }
  102. }