server_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "context"
  19. "encoding/json"
  20. "net"
  21. "reflect"
  22. "testing"
  23. "time"
  24. )
  25. type Service struct{}
  26. type Args struct {
  27. S string
  28. }
  29. func (s *Service) NoArgsRets() {
  30. }
  31. type Result struct {
  32. String string
  33. Int int
  34. Args *Args
  35. }
  36. func (s *Service) Echo(str string, i int, args *Args) Result {
  37. return Result{str, i, args}
  38. }
  39. func (s *Service) EchoWithCtx(ctx context.Context, str string, i int, args *Args) Result {
  40. return Result{str, i, args}
  41. }
  42. func (s *Service) Sleep(ctx context.Context, duration time.Duration) {
  43. select {
  44. case <-time.After(duration):
  45. case <-ctx.Done():
  46. }
  47. }
  48. func (s *Service) Rets() (string, error) {
  49. return "", nil
  50. }
  51. func (s *Service) InvalidRets1() (error, string) {
  52. return nil, ""
  53. }
  54. func (s *Service) InvalidRets2() (string, string) {
  55. return "", ""
  56. }
  57. func (s *Service) InvalidRets3() (string, string, error) {
  58. return "", "", nil
  59. }
  60. func (s *Service) Subscription(ctx context.Context) (*Subscription, error) {
  61. return nil, nil
  62. }
  63. func TestServerRegisterName(t *testing.T) {
  64. server := NewServer()
  65. service := new(Service)
  66. if err := server.RegisterName("calc", service); err != nil {
  67. t.Fatalf("%v", err)
  68. }
  69. if len(server.services) != 2 {
  70. t.Fatalf("Expected 2 service entries, got %d", len(server.services))
  71. }
  72. svc, ok := server.services["calc"]
  73. if !ok {
  74. t.Fatalf("Expected service calc to be registered")
  75. }
  76. if len(svc.callbacks) != 5 {
  77. t.Errorf("Expected 5 callbacks for service 'calc', got %d", len(svc.callbacks))
  78. }
  79. if len(svc.subscriptions) != 1 {
  80. t.Errorf("Expected 1 subscription for service 'calc', got %d", len(svc.subscriptions))
  81. }
  82. }
  83. func testServerMethodExecution(t *testing.T, method string) {
  84. server := NewServer()
  85. service := new(Service)
  86. if err := server.RegisterName("test", service); err != nil {
  87. t.Fatalf("%v", err)
  88. }
  89. stringArg := "string arg"
  90. intArg := 1122
  91. argsArg := &Args{"abcde"}
  92. params := []interface{}{stringArg, intArg, argsArg}
  93. request := map[string]interface{}{
  94. "id": 12345,
  95. "method": "test_" + method,
  96. "version": "2.0",
  97. "params": params,
  98. }
  99. clientConn, serverConn := net.Pipe()
  100. defer clientConn.Close()
  101. go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation)
  102. out := json.NewEncoder(clientConn)
  103. in := json.NewDecoder(clientConn)
  104. if err := out.Encode(request); err != nil {
  105. t.Fatal(err)
  106. }
  107. response := jsonSuccessResponse{Result: &Result{}}
  108. if err := in.Decode(&response); err != nil {
  109. t.Fatal(err)
  110. }
  111. if result, ok := response.Result.(*Result); ok {
  112. if result.String != stringArg {
  113. t.Errorf("expected %s, got : %s\n", stringArg, result.String)
  114. }
  115. if result.Int != intArg {
  116. t.Errorf("expected %d, got %d\n", intArg, result.Int)
  117. }
  118. if !reflect.DeepEqual(result.Args, argsArg) {
  119. t.Errorf("expected %v, got %v\n", argsArg, result)
  120. }
  121. } else {
  122. t.Fatalf("invalid response: expected *Result - got: %T", response.Result)
  123. }
  124. }
  125. func TestServerMethodExecution(t *testing.T) {
  126. testServerMethodExecution(t, "echo")
  127. }
  128. func TestServerMethodWithCtx(t *testing.T) {
  129. testServerMethodExecution(t, "echoWithCtx")
  130. }