dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/grpc/grpc_protocol_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  	"testing"
    22  	"time"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/common"
    31  	"dubbo.apache.org/dubbo-go/v3/config"
    32  	"dubbo.apache.org/dubbo-go/v3/protocol"
    33  	"dubbo.apache.org/dubbo-go/v3/protocol/grpc/internal/helloworld"
    34  )
    35  
    36  func doInitProvider() {
    37  	rootConfig := config.RootConfig{
    38  		Application: &config.ApplicationConfig{
    39  			Organization: "dubbo_org",
    40  			Name:         "BDTService",
    41  			Module:       "module",
    42  			Version:      "0.0.1",
    43  			Owner:        "dubbo",
    44  			Environment:  "test",
    45  		},
    46  		Provider: &config.ProviderConfig{
    47  			Services: map[string]*config.ServiceConfig{
    48  				"GrpcGreeterImpl": {
    49  					Interface:   "io.grpc.examples.helloworld.GreeterGrpc$IGreeter",
    50  					ProtocolIDs: []string{"grpc"},
    51  					RegistryIDs: []string{"shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2,hangzhou_service_discovery_reg"},
    52  					Cluster:     "failover",
    53  					Loadbalance: "random",
    54  					Retries:     "3",
    55  					Methods: []*config.MethodConfig{
    56  						{
    57  							Name:        "SayHello",
    58  							Retries:     "2",
    59  							LoadBalance: "random",
    60  							Weight:      200,
    61  						},
    62  					},
    63  				},
    64  			},
    65  		},
    66  	}
    67  	config.SetRootConfig(rootConfig)
    68  }
    69  
    70  func TestGrpcProtocolExport(t *testing.T) {
    71  	// Export
    72  	config.SetProviderService(helloworld.NewService())
    73  	doInitProvider()
    74  
    75  	url, err := common.NewURL(helloworldURL)
    76  	assert.NoError(t, err)
    77  
    78  	proto := GetProtocol()
    79  	exporter := proto.Export(protocol.NewBaseInvoker(url))
    80  	time.Sleep(time.Second)
    81  
    82  	// make sure url
    83  	eq := exporter.GetInvoker().GetURL().URLEqual(url)
    84  	assert.True(t, eq)
    85  
    86  	// make sure exporterMap after 'UnExport'
    87  	_, ok := proto.(*GrpcProtocol).ExporterMap().Load(url.ServiceKey())
    88  	assert.True(t, ok)
    89  	exporter.UnExport()
    90  	_, ok = proto.(*GrpcProtocol).ExporterMap().Load(url.ServiceKey())
    91  	assert.False(t, ok)
    92  
    93  	// make sure serverMap after 'Destroy'
    94  	_, ok = proto.(*GrpcProtocol).serverMap[url.Location]
    95  	assert.True(t, ok)
    96  	proto.Destroy()
    97  	_, ok = proto.(*GrpcProtocol).serverMap[url.Location]
    98  	assert.False(t, ok)
    99  }
   100  
   101  func TestGrpcProtocolRefer(t *testing.T) {
   102  	server, err := helloworld.NewServer("127.0.0.1:30000")
   103  	assert.NoError(t, err)
   104  	go server.Start()
   105  	defer server.Stop()
   106  
   107  	url, err := common.NewURL(helloworldURL)
   108  	assert.NoError(t, err)
   109  
   110  	proto := GetProtocol()
   111  	invoker := proto.Refer(url)
   112  
   113  	// make sure url
   114  	eq := invoker.GetURL().URLEqual(url)
   115  	assert.True(t, eq)
   116  
   117  	// make sure invokers after 'Destroy'
   118  	invokersLen := len(proto.(*GrpcProtocol).Invokers())
   119  	assert.Equal(t, 1, invokersLen)
   120  	proto.Destroy()
   121  	invokersLen = len(proto.(*GrpcProtocol).Invokers())
   122  	assert.Equal(t, 0, invokersLen)
   123  }