dubbo.apache.org/dubbo-go/v3@v3.1.1/metadata/report/delegate/delegate_report_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 delegate
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  import (
    27  	"github.com/dubbogo/gost/log/logger"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  
    31  	"go.uber.org/atomic"
    32  )
    33  
    34  import (
    35  	"dubbo.apache.org/dubbo-go/v3/common"
    36  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    37  	"dubbo.apache.org/dubbo-go/v3/config/instance"
    38  	"dubbo.apache.org/dubbo-go/v3/metadata/definition"
    39  	"dubbo.apache.org/dubbo-go/v3/metadata/identifier"
    40  )
    41  
    42  func TestMetadataReport_MetadataReportRetry(t *testing.T) {
    43  	counter := atomic.NewInt64(1)
    44  
    45  	retry, err := newMetadataReportRetry(1, 10, func() bool {
    46  		counter.Add(1)
    47  		return true
    48  	})
    49  	assert.NoError(t, err)
    50  	retry.startRetryTask()
    51  	<-time.After(2000 * time.Millisecond)
    52  	retry.scheduler.Clear()
    53  	assert.Equal(t, int64(3), counter.Load())
    54  	logger.Info("over")
    55  }
    56  
    57  func TestMetadataReport_MetadataReportRetryWithLimit(t *testing.T) {
    58  	counter := atomic.NewInt64(1)
    59  
    60  	retry, err := newMetadataReportRetry(1, 1, func() bool {
    61  		counter.Add(1)
    62  		return true
    63  	})
    64  	assert.NoError(t, err)
    65  	retry.startRetryTask()
    66  	<-time.After(2000 * time.Millisecond)
    67  	retry.scheduler.Clear()
    68  	assert.Equal(t, int64(2), counter.Load())
    69  	logger.Info("over")
    70  }
    71  
    72  func mockNewMetadataReport(t *testing.T) *MetadataReport {
    73  	syncReportKey := "false"
    74  	retryPeriodKey := "3"
    75  	retryTimesKey := "100"
    76  	cycleReportKey := "true"
    77  
    78  	url, err := common.NewURL(fmt.Sprintf(
    79  		"test://127.0.0.1:20000/?"+constant.SyncReportKey+"=%v&"+constant.RetryPeriodKey+"=%v&"+
    80  			constant.RetryTimesKey+"=%v&"+constant.CycleReportKey+"=%v",
    81  		syncReportKey, retryPeriodKey, retryTimesKey, cycleReportKey))
    82  	assert.NoError(t, err)
    83  	instance.SetMetadataReportUrl(url)
    84  	mtr, err := NewMetadataReport()
    85  	assert.NoError(t, err)
    86  	assert.NotNil(t, mtr)
    87  	return mtr
    88  }
    89  
    90  func TestMetadataReport_StoreProviderMetadata(t *testing.T) {
    91  	mtr := mockNewMetadataReport(t)
    92  	metadataId := &identifier.MetadataIdentifier{
    93  		Application: "app",
    94  		BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{
    95  			ServiceInterface: "com.ikurento.user.UserProvider",
    96  			Version:          "0.0.1",
    97  			Group:            "group1",
    98  			Side:             "provider",
    99  		},
   100  	}
   101  
   102  	mtr.StoreProviderMetadata(metadataId, getMockDefinition(metadataId, t))
   103  }
   104  
   105  func getMockDefinition(id *identifier.MetadataIdentifier, t *testing.T) *definition.ServiceDefinition {
   106  	protocol := "dubbo"
   107  	beanName := "UserProvider"
   108  	url, err := common.NewURL(fmt.Sprintf(
   109  		"%v://127.0.0.1:20000/com.ikurento.user.UserProvider1?anyhost=true&"+
   110  			"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
   111  			"environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+
   112  			"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
   113  		protocol, id.ServiceInterface, id.Group, id.Version, beanName))
   114  	assert.NoError(t, err)
   115  	_, err = common.ServiceMap.Register(id.ServiceInterface, protocol, id.Group, id.Version, &definition.UserProvider{})
   116  	assert.NoError(t, err)
   117  	service := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
   118  	return definition.BuildServiceDefinition(*service, url)
   119  }