dubbo.apache.org/dubbo-go/v3@v3.1.1/metadata/service/exporter/configurable/exporter_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 configurable
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  import (
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  import (
    29  	"dubbo.apache.org/dubbo-go/v3/common"
    30  	"dubbo.apache.org/dubbo-go/v3/config"
    31  	_ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl"
    32  	"dubbo.apache.org/dubbo-go/v3/metadata/service/local"
    33  	_ "dubbo.apache.org/dubbo-go/v3/metrics/prometheus"
    34  	_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo"
    35  	_ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory"
    36  	"dubbo.apache.org/dubbo-go/v3/remoting/getty"
    37  )
    38  
    39  func TestConfigurableExporter(t *testing.T) {
    40  	getty.SetServerConfig(getty.ServerConfig{
    41  		SessionNumber:  700,
    42  		SessionTimeout: "20s",
    43  		GettySessionParam: getty.GettySessionParam{
    44  			CompressEncoding: false,
    45  			TcpNoDelay:       true,
    46  			TcpKeepAlive:     true,
    47  			KeepAlivePeriod:  "120s",
    48  			TcpRBufSize:      262144,
    49  			TcpWBufSize:      65536,
    50  			TcpReadTimeout:   "1s",
    51  			TcpWriteTimeout:  "5s",
    52  			WaitTimeout:      "1s",
    53  			MaxMsgLen:        10240000000,
    54  			SessionName:      "server",
    55  		},
    56  	})
    57  	mockInitProviderWithSingleRegistry()
    58  	metadataService, _ := local.GetLocalMetadataService()
    59  	exported := NewMetadataServiceExporter(metadataService)
    60  
    61  	t.Run("configurableExporter", func(t *testing.T) {
    62  		registryURL, _ := common.NewURL("service-discovery://localhost:12345")
    63  		subURL, _ := common.NewURL("dubbo://localhost:20003")
    64  		registryURL.SubURL = subURL
    65  		assert.Equal(t, false, exported.IsExported())
    66  		assert.NoError(t, exported.Export(registryURL))
    67  		assert.Equal(t, true, exported.IsExported())
    68  		assert.Regexp(t, "dubbo://:[0-9]{1,}/org.apache.dubbo.metadata.MetadataService*", exported.GetExportedURLs()[0].String())
    69  		exported.Unexport()
    70  		assert.Equal(t, false, exported.IsExported())
    71  	})
    72  }
    73  
    74  // mockInitProviderWithSingleRegistry will init a mocked providerConfig
    75  func mockInitProviderWithSingleRegistry() {
    76  	providerConfig := config.NewProviderConfigBuilder().AddService("MockService", config.NewServiceConfigBuilder().Build()).Build()
    77  	providerConfig.Services["MockService"].InitExported()
    78  	config.SetRootConfig(config.RootConfig{
    79  		Application: &config.ApplicationConfig{
    80  			Organization: "dubbo_org",
    81  			Name:         "dubbo",
    82  			Module:       "module",
    83  			Version:      "1.0.0",
    84  			Owner:        "dubbo",
    85  			Environment:  "test",
    86  		},
    87  		Registries: map[string]*config.RegistryConfig{
    88  			"mock": {
    89  				Address:  "mock://127.0.0.1:2181",
    90  				Username: "user1",
    91  				Password: "pwd1",
    92  			},
    93  		},
    94  		Protocols: map[string]*config.ProtocolConfig{
    95  			"mock": {
    96  				Name: "mock",
    97  				Ip:   "127.0.0.1",
    98  				Port: "20000",
    99  			},
   100  		},
   101  
   102  		Provider: &config.ProviderConfig{
   103  			Services: map[string]*config.ServiceConfig{
   104  				"MockService": {
   105  					Interface:   "com.MockService",
   106  					ProtocolIDs: []string{"mock"},
   107  					Cluster:     "failover",
   108  					Loadbalance: "random",
   109  					Retries:     "3",
   110  					Group:       "huadong_idc",
   111  					Version:     "1.0.0",
   112  					Methods: []*config.MethodConfig{
   113  						{
   114  							Name:        "GetUser",
   115  							Retries:     "2",
   116  							LoadBalance: "random",
   117  							Weight:      200,
   118  						},
   119  						{
   120  							Name:        "GetUser1",
   121  							Retries:     "2",
   122  							LoadBalance: "random",
   123  							Weight:      200,
   124  						},
   125  					},
   126  				},
   127  			},
   128  		},
   129  	})
   130  }