dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/grpc/grpc_invoker_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package grpc 19 20 import ( 21 "context" 22 "reflect" 23 "testing" 24 ) 25 26 import ( 27 "github.com/stretchr/testify/assert" 28 ) 29 30 import ( 31 "dubbo.apache.org/dubbo-go/v3/common" 32 "dubbo.apache.org/dubbo-go/v3/protocol/grpc/internal/helloworld" 33 "dubbo.apache.org/dubbo-go/v3/protocol/grpc/internal/routeguide" 34 "dubbo.apache.org/dubbo-go/v3/protocol/invocation" 35 ) 36 37 const ( 38 helloworldURL = "grpc://127.0.0.1:30000/GrpcGreeterImpl?accesslog=&anyhost=true&app.version=0.0.1&application=BDTService&async=false&bean.name=GrpcGreeterImpl" + 39 "&category=providers&cluster=failover&dubbo=dubbo-provider-golang-2.6.0&environment=dev&execute.limit=&execute.limit.rejected.handler=&generic=false&group=&interface=io.grpc.examples.helloworld.GreeterGrpc%24IGreeter" + 40 "&ip=192.168.1.106&loadbalance=random&methods.SayHello.loadbalance=random&methods.SayHello.retries=1&methods.SayHello.tps.limit.interval=&methods.SayHello.tps.limit.rate=&methods.SayHello.tps.limit.strategy=" + 41 "&methods.SayHello.weight=0&module=dubbogo+say-hello+client&name=BDTService&organization=ikurento.com&owner=ZX&pid=49427&reference.filter=cshutdown®istry.role=3&remote.timestamp=1576923717&retries=" + 42 "&service.filter=echo%2Ctoken%2Caccesslog%2Ctps%2Cexecute%2Cpshutdown&side=provider×tamp=1576923740&tps.limit.interval=&tps.limit.rate=&tps.limit.rejected.handler=&tps.limit.strategy=&tps.limiter=&version=&warmup=100!" 43 44 routeguideURL = "grpc://127.0.0.1:30000/routeGuideImpl?accesslog=&anyhost=true&app.version=0.0.1&application=BDTService&async=false&bean.name=routeGuideImpl" + 45 "&category=providers&cluster=failover&dubbo=dubbo-provider-golang-2.6.0&environment=dev&execute.limit=&execute.limit.rejected.handler=&generic=false&group=&interface=io.grpc.examples.helloworld.GreeterGrpc%24RouteGuide" + 46 "&ip=192.168.1.106&loadbalance=random&methods.SayHello.loadbalance=random&methods.SayHello.retries=1&methods.SayHello.tps.limit.interval=&methods.SayHello.tps.limit.rate=&methods.SayHello.tps.limit.strategy=" + 47 "&methods.SayHello.weight=0&module=dubbogo+say-hello+client&name=BDTService&organization=ikurento.com&owner=ZX&pid=49427&reference.filter=cshutdown®istry.role=3&remote.timestamp=1576923717&retries=" + 48 "&service.filter=echo%2Ctoken%2Caccesslog%2Ctps%2Cexecute%2Cpshutdown&side=provider×tamp=1576923740&tps.limit.interval=&tps.limit.rate=&tps.limit.rejected.handler=&tps.limit.strategy=&tps.limiter=&version=&warmup=100!" 49 ) 50 51 func TestUnaryInvoke(t *testing.T) { 52 server, err := helloworld.NewServer("127.0.0.1:30000") 53 assert.NoError(t, err) 54 go server.Start() 55 defer server.Stop() 56 57 url, err := common.NewURL(helloworldURL) 58 assert.NoError(t, err) 59 60 cli, err := NewClient(url) 61 assert.NoError(t, err) 62 63 var args []reflect.Value 64 args = append(args, reflect.ValueOf(&helloworld.HelloRequest{Name: "request name"})) 65 bizReply := &helloworld.HelloReply{} 66 invo := invocation.NewRPCInvocationWithOptions( 67 invocation.WithMethodName("SayHello"), 68 invocation.WithParameterValues(args), 69 invocation.WithReply(bizReply), 70 ) 71 72 invoker := NewGrpcInvoker(url, cli) 73 res := invoker.Invoke(context.Background(), invo) 74 assert.NoError(t, res.Error()) 75 assert.Equal(t, &helloworld.HelloReply{Message: "Hello request name"}, res.Result().(reflect.Value).Interface()) 76 assert.Equal(t, &helloworld.HelloReply{Message: "Hello request name"}, bizReply) 77 } 78 79 func TestStreamInvoke(t *testing.T) { 80 server, err := routeguide.NewServer("127.0.0.1:30000") 81 assert.NoError(t, err) 82 go server.Start() 83 defer server.Stop() 84 85 url, err := common.NewURL(routeguideURL) 86 assert.NoError(t, err) 87 88 cli, err := NewClient(url) 89 assert.NoError(t, err) 90 91 invoker := NewGrpcInvoker(url, cli) 92 93 args := []reflect.Value{ 94 reflect.ValueOf(&routeguide.Point{Latitude: 409146138, Longitude: -746188906}), 95 } 96 bizReply := &routeguide.Feature{} 97 invo := invocation.NewRPCInvocationWithOptions( 98 invocation.WithMethodName("GetFeature"), 99 invocation.WithParameterValues(args), 100 invocation.WithReply(bizReply), 101 ) 102 res := invoker.Invoke(context.Background(), invo) 103 assert.NoError(t, res.Error()) 104 assert.Equal(t, &routeguide.Feature{ 105 Name: "Berkshire Valley Management Area Trail, Jefferson, NJ, USA", 106 Location: &routeguide.Point{Latitude: 409146138, Longitude: -746188906}, 107 }, res.Result().(reflect.Value).Interface()) 108 assert.Equal(t, &routeguide.Feature{ 109 Name: "Berkshire Valley Management Area Trail, Jefferson, NJ, USA", 110 Location: &routeguide.Point{Latitude: 409146138, Longitude: -746188906}, 111 }, bizReply) 112 113 args = []reflect.Value{ 114 reflect.ValueOf(&routeguide.Rectangle{ 115 Lo: &routeguide.Point{Latitude: 400000000, Longitude: -750000000}, 116 Hi: &routeguide.Point{Latitude: 420000000, Longitude: -730000000}, 117 }), 118 } 119 invo = invocation.NewRPCInvocationWithOptions( 120 invocation.WithMethodName("ListFeatures"), 121 invocation.WithParameterValues(args), 122 ) 123 res = invoker.Invoke(context.Background(), invo) 124 assert.ErrorIs(t, res.Error(), errNoReply) 125 listFeaturesStream := res.Result().(reflect.Value).Interface().(routeguide.RouteGuide_ListFeaturesClient) 126 routeguide.PrintFeatures(listFeaturesStream) 127 128 args = make([]reflect.Value, 0) 129 invo = invocation.NewRPCInvocationWithOptions( 130 invocation.WithMethodName("RecordRoute"), 131 invocation.WithParameterValues(args), 132 ) 133 res = invoker.Invoke(context.Background(), invo) 134 assert.ErrorIs(t, res.Error(), errNoReply) 135 recordRouteStream := res.Result().(reflect.Value).Interface().(routeguide.RouteGuide_RecordRouteClient) 136 routeguide.RunRecordRoute(recordRouteStream) 137 138 args = make([]reflect.Value, 0) 139 invo = invocation.NewRPCInvocationWithOptions( 140 invocation.WithMethodName("RouteChat"), 141 invocation.WithParameterValues(args), 142 ) 143 res = invoker.Invoke(context.Background(), invo) 144 assert.ErrorIs(t, res.Error(), errNoReply) 145 routeChatStream := res.Result().(reflect.Value).Interface().(routeguide.RouteGuide_RouteChatClient) 146 routeguide.RunRouteChat(routeChatStream) 147 }