dubbo.apache.org/dubbo-go/v3@v3.1.1/metadata/service/remote/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 remote
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	gxset "github.com/dubbogo/gost/container/set"
    27  	"github.com/dubbogo/gost/log/logger"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  import (
    33  	"dubbo.apache.org/dubbo-go/v3/common"
    34  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    35  	"dubbo.apache.org/dubbo-go/v3/config/instance"
    36  	"dubbo.apache.org/dubbo-go/v3/metadata/definition"
    37  	"dubbo.apache.org/dubbo-go/v3/metadata/identifier"
    38  	"dubbo.apache.org/dubbo-go/v3/metadata/report"
    39  	"dubbo.apache.org/dubbo-go/v3/metadata/report/factory"
    40  	"dubbo.apache.org/dubbo-go/v3/metadata/service/local"
    41  	"dubbo.apache.org/dubbo-go/v3/registry"
    42  )
    43  
    44  var (
    45  	serviceMetadata    = make(map[*identifier.ServiceMetadataIdentifier]*common.URL, 4)
    46  	subscribedMetadata = make(map[*identifier.SubscriberMetadataIdentifier]string, 4)
    47  )
    48  
    49  func getMetadataReportFactory() factory.MetadataReportFactory {
    50  	return &metadataReportFactory{}
    51  }
    52  
    53  type metadataReportFactory struct{}
    54  
    55  func (mrf *metadataReportFactory) CreateMetadataReport(*common.URL) report.MetadataReport {
    56  	return &metadataReport{}
    57  }
    58  
    59  type metadataReport struct{}
    60  
    61  func (mr metadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) {
    62  	//TODO implement me
    63  	panic("implement me")
    64  }
    65  
    66  func (mr metadataReport) RegisterServiceAppMapping(string, string, string) error {
    67  	panic("implement me")
    68  }
    69  
    70  func (mr metadataReport) GetServiceAppMapping(string, string, registry.MappingListener) (*gxset.HashSet, error) {
    71  	panic("implement me")
    72  }
    73  
    74  func (mr metadataReport) RemoveServiceAppMappingListener(string, string) error {
    75  	panic("implement me")
    76  }
    77  
    78  func (mr metadataReport) GetAppMetadata(*identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) {
    79  	panic("implement me")
    80  }
    81  
    82  func (mr metadataReport) PublishAppMetadata(*identifier.SubscriberMetadataIdentifier, *common.MetadataInfo) error {
    83  	panic("implement me")
    84  }
    85  
    86  func (metadataReport) StoreProviderMetadata(*identifier.MetadataIdentifier, string) error {
    87  	return nil
    88  }
    89  
    90  func (metadataReport) StoreConsumerMetadata(*identifier.MetadataIdentifier, string) error {
    91  	return nil
    92  }
    93  
    94  func (mr *metadataReport) SaveServiceMetadata(id *identifier.ServiceMetadataIdentifier, url *common.URL) error {
    95  	logger.Infof("SaveServiceMetadata , url is %v", url)
    96  	serviceMetadata[id] = url
    97  	return nil
    98  }
    99  
   100  func (metadataReport) RemoveServiceMetadata(*identifier.ServiceMetadataIdentifier) error {
   101  	return nil
   102  }
   103  
   104  func (metadataReport) GetExportedURLs(*identifier.ServiceMetadataIdentifier) ([]string, error) {
   105  	return nil, nil
   106  }
   107  
   108  func (mr *metadataReport) SaveSubscribedData(id *identifier.SubscriberMetadataIdentifier, urls string) error {
   109  	logger.Infof("SaveSubscribedData, , url is %v", urls)
   110  	subscribedMetadata[id] = urls
   111  	return nil
   112  }
   113  
   114  func (metadataReport) GetSubscribedURLs(*identifier.SubscriberMetadataIdentifier) ([]string, error) {
   115  	return nil, nil
   116  }
   117  
   118  func (metadataReport) GetServiceDefinition(*identifier.MetadataIdentifier) (string, error) {
   119  	return "", nil
   120  }
   121  
   122  func TestMetadataService(t *testing.T) {
   123  	extension.SetMetadataReportFactory("mock", getMetadataReportFactory)
   124  	u, err := common.NewURL("mock://127.0.0.1:20000/?sync.report=true")
   125  	assert.NoError(t, err)
   126  	instance.SetMetadataReportInstance(u)
   127  	mts, err := GetRemoteMetadataService()
   128  	assert.NoError(t, err)
   129  	assert.NotNil(t, mts)
   130  }
   131  
   132  func TestMockInmemoryProc(t *testing.T) {
   133  	mts, _ := local.GetLocalMetadataService()
   134  	serviceName := "com.ikurento.user.UserProvider"
   135  	group := "group1"
   136  	version := "0.0.1"
   137  	protocol := "dubbo"
   138  	beanName := "UserProvider"
   139  	userProvider := &definition.UserProvider{}
   140  
   141  	u, err := common.NewURL(fmt.Sprintf(
   142  		"%v://127.0.0.1:20000/com.ikurento.user.UserProvider1?anyhost=true&"+
   143  			"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
   144  			"environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+
   145  			"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
   146  		protocol, serviceName, group, version, beanName))
   147  	assert.NoError(t, err)
   148  
   149  	_, err = mts.ExportURL(u)
   150  	assert.NoError(t, err)
   151  	_, err = mts.SubscribeURL(u)
   152  	assert.NoError(t, err)
   153  
   154  	_, err = common.ServiceMap.Register(serviceName, protocol, group, version, userProvider)
   155  	assert.NoError(t, err)
   156  	err = mts.PublishServiceDefinition(u)
   157  	assert.NoError(t, err)
   158  
   159  	expected := "{\"parameters\":{\"anyhost\":\"true\",\"application\":\"BDTService\"," +
   160  		"\"bean.name\":\"UserProvider\",\"category\":\"providers\",\"default.timeout\":\"10000\"," +
   161  		"\"dubbo\":\"dubbo-provider-golang-1.0.0\",\"environment\":\"dev\",\"group\":\"group1\"," +
   162  		"\"interface\":\"com.ikurento.user.UserProvider\",\"ip\":\"192.168.56.1\"," +
   163  		"\"methods\":\"GetUser\",\"module\":\"dubbogo user-info server\",\"org\":\"ikurento.com\"," +
   164  		"\"owner\":\"ZX\",\"pid\":\"1447\",\"revision\":\"0.0.1\",\"side\":\"provider\"," +
   165  		"\"timeout\":\"3000\",\"timestamp\":\"1556509797245\",\"version\":\"0.0.1\"}," +
   166  		"\"canonicalName\":\"com.ikurento.user.UserProvider\",\"codeSource\":\"\"," +
   167  		"\"methods\":[{\"name\":\"GetUser\",\"parameterTypes\":[\"slice\"],\"returnType\":\"ptr\"," +
   168  		"\"parameters\":null}],\"types\":null}"
   169  	def1, _ := mts.GetServiceDefinition(serviceName, group, version)
   170  	assert.Equal(t, expected, def1)
   171  	serviceKey := definition.ServiceDescriperBuild(serviceName, group, version)
   172  	def2, _ := mts.GetServiceDefinitionByServiceKey(serviceKey)
   173  	assert.Equal(t, expected, def2)
   174  }