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