github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/rpc/server_test.go (about) 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 17 package rpc 18 19 import ( 20 "encoding/json" 21 "net" 22 "reflect" 23 "testing" 24 "time" 25 26 "golang.org/x/net/context" 27 ) 28 29 type Service struct{} 30 31 type Args struct { 32 S string 33 } 34 35 func (s *Service) NoArgsRets() { 36 } 37 38 type Result struct { 39 String string 40 Int int 41 Args *Args 42 } 43 44 func (s *Service) Echo(str string, i int, args *Args) Result { 45 return Result{str, i, args} 46 } 47 48 func (s *Service) EchoWithCtx(ctx context.Context, str string, i int, args *Args) Result { 49 return Result{str, i, args} 50 } 51 52 func (s *Service) Sleep(ctx context.Context, duration time.Duration) { 53 select { 54 case <-time.After(duration): 55 case <-ctx.Done(): 56 } 57 } 58 59 func (s *Service) Rets() (string, error) { 60 return "", nil 61 } 62 63 func (s *Service) InvalidRets1() (error, string) { 64 return nil, "" 65 } 66 67 func (s *Service) InvalidRets2() (string, string) { 68 return "", "" 69 } 70 71 func (s *Service) InvalidRets3() (string, string, error) { 72 return "", "", nil 73 } 74 75 func (s *Service) Subscription(ctx context.Context) (*Subscription, error) { 76 return nil, nil 77 } 78 79 func TestServerRegisterName(t *testing.T) { 80 server := NewServer() 81 service := new(Service) 82 83 if err := server.RegisterName("calc", service); err != nil { 84 t.Fatalf("%v", err) 85 } 86 87 if len(server.services) != 2 { 88 t.Fatalf("Expected 2 service entries, got %d", len(server.services)) 89 } 90 91 svc, ok := server.services["calc"] 92 if !ok { 93 t.Fatalf("Expected service calc to be registered") 94 } 95 96 if len(svc.callbacks) != 5 { 97 t.Errorf("Expected 5 callbacks for service 'calc', got %d", len(svc.callbacks)) 98 } 99 100 if len(svc.subscriptions) != 1 { 101 t.Errorf("Expected 1 subscription for service 'calc', got %d", len(svc.subscriptions)) 102 } 103 } 104 105 func testServerMethodExecution(t *testing.T, method string) { 106 server := NewServer() 107 service := new(Service) 108 109 if err := server.RegisterName("test", service); err != nil { 110 t.Fatalf("%v", err) 111 } 112 113 stringArg := "string arg" 114 intArg := 1122 115 argsArg := &Args{"abcde"} 116 params := []interface{}{stringArg, intArg, argsArg} 117 118 request := map[string]interface{}{ 119 "id": 12345, 120 "method": "test_" + method, 121 "version": "2.0", 122 "params": params, 123 } 124 125 clientConn, serverConn := net.Pipe() 126 defer clientConn.Close() 127 128 go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation) 129 130 out := json.NewEncoder(clientConn) 131 in := json.NewDecoder(clientConn) 132 133 if err := out.Encode(request); err != nil { 134 t.Fatal(err) 135 } 136 137 response := jsonSuccessResponse{Result: &Result{}} 138 if err := in.Decode(&response); err != nil { 139 t.Fatal(err) 140 } 141 142 if result, ok := response.Result.(*Result); ok { 143 if result.String != stringArg { 144 t.Errorf("expected %s, got : %s\n", stringArg, result.String) 145 } 146 if result.Int != intArg { 147 t.Errorf("expected %d, got %d\n", intArg, result.Int) 148 } 149 if !reflect.DeepEqual(result.Args, argsArg) { 150 t.Errorf("expected %v, got %v\n", argsArg, result) 151 } 152 } else { 153 t.Fatalf("invalid response: expected *Result - got: %T", response.Result) 154 } 155 } 156 157 func TestServerMethodExecution(t *testing.T) { 158 testServerMethodExecution(t, "echo") 159 } 160 161 func TestServerMethodWithCtx(t *testing.T) { 162 testServerMethodExecution(t, "echoWithCtx") 163 }