github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/client/grpc/grpc_test.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/client/grpc/grpc_test.go 16 17 package grpc 18 19 import ( 20 "context" 21 "net" 22 "testing" 23 24 "github.com/tickoalcantara12/micro/v3/service/client" 25 "github.com/tickoalcantara12/micro/v3/service/errors" 26 "github.com/tickoalcantara12/micro/v3/service/registry" 27 "github.com/tickoalcantara12/micro/v3/service/registry/memory" 28 "github.com/tickoalcantara12/micro/v3/service/router" 29 regRouter "github.com/tickoalcantara12/micro/v3/service/router/registry" 30 pgrpc "google.golang.org/grpc" 31 pb "github.com/tickoalcantara12/micro/v3/service/client/grpc/proto" 32 ) 33 34 // server is used to implement helloworld.GreeterServer. 35 type greeterServer struct { 36 pb.UnimplementedGreeterServer 37 } 38 39 // SayHello implements helloworld.GreeterServer 40 func (g *greeterServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { 41 if in.Name == "Error" { 42 return nil, &errors.Error{Id: "1", Code: 99, Detail: "detail"} 43 } 44 return &pb.HelloReply{Message: "Hello " + in.Name}, nil 45 } 46 47 func TestGRPCClient(t *testing.T) { 48 l, err := net.Listen("tcp", ":0") 49 if err != nil { 50 t.Fatalf("failed to listen: %v", err) 51 } 52 defer l.Close() 53 54 s := pgrpc.NewServer() 55 pb.RegisterGreeterServer(s, &greeterServer{}) 56 57 go s.Serve(l) 58 defer s.Stop() 59 60 // create mock registry 61 r := memory.NewRegistry() 62 63 // register service 64 r.Register(®istry.Service{ 65 Name: "helloworld", 66 Version: "test", 67 Nodes: []*registry.Node{ 68 { 69 Id: "test-1", 70 Address: l.Addr().String(), 71 Metadata: map[string]string{ 72 "protocol": "grpc", 73 }, 74 }, 75 }, 76 }) 77 78 // create router 79 rtr := regRouter.NewRouter(router.Registry(r)) 80 81 // create client 82 c := NewClient(client.Router(rtr)) 83 84 testMethods := []string{ 85 "/helloworld.Greeter/SayHello", 86 "Greeter.SayHello", 87 } 88 89 for _, method := range testMethods { 90 req := c.NewRequest("helloworld", method, &pb.HelloRequest{ 91 Name: "John", 92 }) 93 94 rsp := pb.HelloReply{} 95 96 err = c.Call(context.TODO(), req, &rsp) 97 if err != nil { 98 t.Fatal(err) 99 } 100 101 if rsp.Message != "Hello John" { 102 t.Fatalf("Got unexpected response %v", rsp.Message) 103 } 104 } 105 106 req := c.NewRequest("helloworld", "/helloworld.Greeter/SayHello", &pb.HelloRequest{ 107 Name: "Error", 108 }) 109 110 rsp := pb.HelloReply{} 111 112 err = c.Call(context.TODO(), req, &rsp) 113 if err == nil { 114 t.Fatal("nil error received") 115 } 116 117 verr, ok := err.(*errors.Error) 118 if !ok { 119 t.Fatalf("invalid error received %#+v\n", err) 120 } 121 122 if verr.Code != 99 && verr.Id != "1" && verr.Detail != "detail" { 123 t.Fatalf("invalid error received %#+v\n", verr) 124 } 125 126 }