github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/rpc/server_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2015 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package rpc 26 27 import ( 28 "context" 29 "encoding/json" 30 "net" 31 "reflect" 32 "testing" 33 "time" 34 ) 35 36 type Service struct{} 37 38 type Args struct { 39 S string 40 } 41 42 func (s *Service) NoArgsRets() { 43 } 44 45 type Result struct { 46 String string 47 Int int 48 Args *Args 49 } 50 51 func (s *Service) Echo(str string, i int, args *Args) Result { 52 return Result{str, i, args} 53 } 54 55 func (s *Service) EchoWithCtx(ctx context.Context, str string, i int, args *Args) Result { 56 return Result{str, i, args} 57 } 58 59 func (s *Service) Sleep(ctx context.Context, duration time.Duration) { 60 select { 61 case <-time.After(duration): 62 case <-ctx.Done(): 63 } 64 } 65 66 func (s *Service) Rets() (string, error) { 67 return "", nil 68 } 69 70 func (s *Service) InvalidRets1() (error, string) { 71 return nil, "" 72 } 73 74 func (s *Service) InvalidRets2() (string, string) { 75 return "", "" 76 } 77 78 func (s *Service) InvalidRets3() (string, string, error) { 79 return "", "", nil 80 } 81 82 func (s *Service) Subscription(ctx context.Context) (*Subscription, error) { 83 return nil, nil 84 } 85 86 func TestServerRegisterName(t *testing.T) { 87 server := NewServer() 88 service := new(Service) 89 90 if err := server.RegisterName("calc", service); err != nil { 91 t.Fatalf("%v", err) 92 } 93 94 if len(server.services) != 2 { 95 t.Fatalf("Expected 2 service entries, got %d", len(server.services)) 96 } 97 98 svc, ok := server.services["calc"] 99 if !ok { 100 t.Fatalf("Expected service calc to be registered") 101 } 102 103 if len(svc.callbacks) != 5 { 104 t.Errorf("Expected 5 callbacks for service 'calc', got %d", len(svc.callbacks)) 105 } 106 107 if len(svc.subscriptions) != 1 { 108 t.Errorf("Expected 1 subscription for service 'calc', got %d", len(svc.subscriptions)) 109 } 110 } 111 112 func testServerMethodExecution(t *testing.T, method string) { 113 server := NewServer() 114 service := new(Service) 115 116 if err := server.RegisterName("test", service); err != nil { 117 t.Fatalf("%v", err) 118 } 119 120 stringArg := "string arg" 121 intArg := 1122 122 argsArg := &Args{"abcde"} 123 params := []interface{}{stringArg, intArg, argsArg} 124 125 request := map[string]interface{}{ 126 "id": 12345, 127 "method": "test_" + method, 128 "version": "2.0", 129 "params": params, 130 } 131 132 clientConn, serverConn := net.Pipe() 133 defer clientConn.Close() 134 135 go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation) 136 137 out := json.NewEncoder(clientConn) 138 in := json.NewDecoder(clientConn) 139 140 if err := out.Encode(request); err != nil { 141 t.Fatal(err) 142 } 143 144 response := jsonSuccessResponse{Result: &Result{}} 145 if err := in.Decode(&response); err != nil { 146 t.Fatal(err) 147 } 148 149 if result, ok := response.Result.(*Result); ok { 150 if result.String != stringArg { 151 t.Errorf("expected %s, got : %s\n", stringArg, result.String) 152 } 153 if result.Int != intArg { 154 t.Errorf("expected %d, got %d\n", intArg, result.Int) 155 } 156 if !reflect.DeepEqual(result.Args, argsArg) { 157 t.Errorf("expected %v, got %v\n", argsArg, result) 158 } 159 } else { 160 t.Fatalf("invalid response: expected *Result - got: %T", response.Result) 161 } 162 } 163 164 func TestServerMethodExecution(t *testing.T) { 165 testServerMethodExecution(t, "echo") 166 } 167 168 func TestServerMethodWithCtx(t *testing.T) { 169 testServerMethodExecution(t, "echoWithCtx") 170 }