dubbo.apache.org/dubbo-go/v3@v3.1.1/metadata/service/local/service_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 local
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    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/metadata/definition"
    32  )
    33  
    34  func TestMetadataService(t *testing.T) {
    35  	mts, _ := GetLocalMetadataService()
    36  	serviceName := "com.ikurento.user.UserProvider"
    37  	group := "group1"
    38  	version := "0.0.1"
    39  	protocol := "dubbo"
    40  	beanName := "UserProvider"
    41  
    42  	u2, err := common.NewURL(fmt.Sprintf(
    43  		"%v://127.0.0.1:20000/com.ikurento.user.UserProvider2?anyhost=true&"+
    44  			"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
    45  			"environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+
    46  			"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
    47  		protocol, serviceName, group, version, beanName))
    48  	assert.NoError(t, err)
    49  	ok, err := mts.ExportURL(u2)
    50  	assert.True(t, ok)
    51  	assert.NoError(t, err)
    52  
    53  	u3, err := common.NewURL(fmt.Sprintf(
    54  		"%v://127.0.0.1:20000/com.ikurento.user.UserProvider3?anyhost=true&"+
    55  			"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
    56  			"environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+
    57  			"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
    58  		protocol, serviceName, group, version, beanName))
    59  	assert.NoError(t, err)
    60  	ok, err = mts.ExportURL(u3)
    61  	assert.True(t, ok)
    62  	assert.NoError(t, err)
    63  
    64  	u, err := common.NewURL(fmt.Sprintf(
    65  		"%v://127.0.0.1:20000/com.ikurento.user.UserProvider1?anyhost=true&"+
    66  			"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
    67  			"environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+
    68  			"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
    69  		protocol, serviceName, group, version, beanName))
    70  	assert.NoError(t, err)
    71  	ok, err = mts.ExportURL(u)
    72  	assert.True(t, ok)
    73  	assert.NoError(t, err)
    74  	list, _ := mts.GetExportedURLs(serviceName, group, version, protocol)
    75  	assert.Equal(t, 3, len(list))
    76  	ok, err = mts.SubscribeURL(u)
    77  	assert.True(t, ok)
    78  	assert.NoError(t, err)
    79  
    80  	ok, err = mts.SubscribeURL(u)
    81  	assert.False(t, ok)
    82  	assert.NoError(t, err)
    83  	list2, err := mts.GetSubscribedURLs()
    84  	assert.Equal(t, 1, len(list2))
    85  	assert.NoError(t, err)
    86  
    87  	err = mts.UnexportURL(u)
    88  	assert.NoError(t, err)
    89  	list3, _ := mts.GetExportedURLs(serviceName, group, version, protocol)
    90  	assert.Equal(t, 2, len(list3))
    91  
    92  	err = mts.UnsubscribeURL(u)
    93  	assert.NoError(t, err)
    94  	list4, _ := mts.GetSubscribedURLs()
    95  	assert.Equal(t, 0, len(list4))
    96  
    97  	userProvider := &definition.UserProvider{}
    98  	_, err = common.ServiceMap.Register(serviceName, protocol, group, version, userProvider)
    99  	assert.NoError(t, err)
   100  	err = mts.PublishServiceDefinition(u)
   101  	assert.NoError(t, err)
   102  	expected := "{\"parameters\":{\"anyhost\":\"true\",\"application\":\"BDTService\"," +
   103  		"\"bean.name\":\"UserProvider\",\"category\":\"providers\",\"default.timeout\":\"10000\"," +
   104  		"\"dubbo\":\"dubbo-provider-golang-1.0.0\",\"environment\":\"dev\",\"group\":\"group1\"," +
   105  		"\"interface\":\"com.ikurento.user.UserProvider\",\"ip\":\"192.168.56.1\"," +
   106  		"\"methods\":\"GetUser\",\"module\":\"dubbogo user-info server\",\"org\":\"ikurento.com\"," +
   107  		"\"owner\":\"ZX\",\"pid\":\"1447\",\"revision\":\"0.0.1\",\"side\":\"provider\"," +
   108  		"\"timeout\":\"3000\",\"timestamp\":\"1556509797245\",\"version\":\"0.0.1\"}," +
   109  		"\"canonicalName\":\"com.ikurento.user.UserProvider\",\"codeSource\":\"\"," +
   110  		"\"methods\":[{\"name\":\"GetUser\",\"parameterTypes\":[\"slice\"],\"returnType\":\"ptr\"," +
   111  		"\"parameters\":null}],\"types\":null}"
   112  	def1, err := mts.GetServiceDefinition(serviceName, group, version)
   113  	assert.Equal(t, expected, def1)
   114  	assert.NoError(t, err)
   115  	serviceKey := definition.ServiceDescriperBuild(serviceName, group, version)
   116  	def2, err := mts.GetServiceDefinitionByServiceKey(serviceKey)
   117  	assert.Equal(t, expected, def2)
   118  	assert.NoError(t, err)
   119  }