server_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "encoding/json"
  19. "net"
  20. "reflect"
  21. "testing"
  22. "golang.org/x/net/context"
  23. )
  24. type Service struct{}
  25. type Args struct {
  26. S string
  27. }
  28. func (s *Service) NoArgsRets() {
  29. }
  30. type Result struct {
  31. String string
  32. Int int
  33. Args *Args
  34. }
  35. func (s *Service) Echo(str string, i int, args *Args) Result {
  36. return Result{str, i, args}
  37. }
  38. func (s *Service) EchoWithCtx(ctx context.Context, str string, i int, args *Args) Result {
  39. return Result{str, i, args}
  40. }
  41. func (s *Service) Rets() (string, error) {
  42. return "", nil
  43. }
  44. func (s *Service) InvalidRets1() (error, string) {
  45. return nil, ""
  46. }
  47. func (s *Service) InvalidRets2() (string, string) {
  48. return "", ""
  49. }
  50. func (s *Service) InvalidRets3() (string, string, error) {
  51. return "", "", nil
  52. }
  53. func (s *Service) Subscription(ctx context.Context) (Subscription, error) {
  54. return nil, nil
  55. }
  56. func TestServerRegisterName(t *testing.T) {
  57. server := NewServer()
  58. service := new(Service)
  59. if err := server.RegisterName("calc", service); err != nil {
  60. t.Fatalf("%v", err)
  61. }
  62. if len(server.services) != 2 {
  63. t.Fatalf("Expected 2 service entries, got %d", len(server.services))
  64. }
  65. svc, ok := server.services["calc"]
  66. if !ok {
  67. t.Fatalf("Expected service calc to be registered")
  68. }
  69. if len(svc.callbacks) != 4 {
  70. t.Errorf("Expected 4 callbacks for service 'calc', got %d", len(svc.callbacks))
  71. }
  72. if len(svc.subscriptions) != 1 {
  73. t.Errorf("Expected 1 subscription for service 'calc', got %d", len(svc.subscriptions))
  74. }
  75. }
  76. func testServerMethodExecution(t *testing.T, method string) {
  77. server := NewServer()
  78. service := new(Service)
  79. if err := server.RegisterName("test", service); err != nil {
  80. t.Fatalf("%v", err)
  81. }
  82. stringArg := "string arg"
  83. intArg := 1122
  84. argsArg := &Args{"abcde"}
  85. params := []interface{}{stringArg, intArg, argsArg}
  86. request := map[string]interface{}{
  87. "id": 12345,
  88. "method": "test_" + method,
  89. "version": "2.0",
  90. "params": params,
  91. }
  92. clientConn, serverConn := net.Pipe()
  93. defer clientConn.Close()
  94. go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation)
  95. out := json.NewEncoder(clientConn)
  96. in := json.NewDecoder(clientConn)
  97. if err := out.Encode(request); err != nil {
  98. t.Fatal(err)
  99. }
  100. response := JSONSuccessResponse{Result: &Result{}}
  101. if err := in.Decode(&response); err != nil {
  102. t.Fatal(err)
  103. }
  104. if result, ok := response.Result.(*Result); ok {
  105. if result.String != stringArg {
  106. t.Errorf("expected %s, got : %s\n", stringArg, result.String)
  107. }
  108. if result.Int != intArg {
  109. t.Errorf("expected %d, got %d\n", intArg, result.Int)
  110. }
  111. if !reflect.DeepEqual(result.Args, argsArg) {
  112. t.Errorf("expected %v, got %v\n", argsArg, result)
  113. }
  114. } else {
  115. t.Fatalf("invalid response: expected *Result - got: %T", response.Result)
  116. }
  117. }
  118. func TestServerMethodExecution(t *testing.T) {
  119. testServerMethodExecution(t, "echo")
  120. }
  121. func TestServerMethodWithCtx(t *testing.T) {
  122. testServerMethodExecution(t, "echoWithCtx")
  123. }