github.com/polarismesh/polaris@v1.17.8/apiserver/eurekaserver/replicate_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package eurekaserver
    19  
    20  import (
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	api "github.com/polarismesh/polaris/common/api/v1"
    27  	testsuit "github.com/polarismesh/polaris/test/suit"
    28  )
    29  
    30  func TestDispatchHeartbeat(t *testing.T) {
    31  	discoverSuit := &testsuit.DiscoverTestSuit{}
    32  	if err := discoverSuit.Initialize(); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	defer discoverSuit.Destroy()
    36  
    37  	options := map[string]interface{}{optionRefreshInterval: 5, optionDeltaExpireInterval: 120}
    38  	eurekaSrv, err := createEurekaServerForTest(discoverSuit, options)
    39  	assert.Nil(t, err)
    40  	eurekaSrv.workers = NewApplicationsWorkers(eurekaSrv.refreshInterval, eurekaSrv.deltaExpireInterval,
    41  		eurekaSrv.enableSelfPreservation, eurekaSrv.namingServer, eurekaSrv.healthCheckServer, eurekaSrv.namespace)
    42  
    43  	namespace := "default"
    44  	appId := "TESTAPP"
    45  	startPort := 8900
    46  	host := "127.0.1.1"
    47  	total := 30
    48  	instances := batchBuildInstances(appId, host, startPort, &LeaseInfo{
    49  		RenewalIntervalInSecs: 30,
    50  		DurationInSecs:        120,
    51  	}, total)
    52  
    53  	var replicateInstances = &ReplicationList{}
    54  
    55  	for i, instance := range instances {
    56  		eurekalog.Infof("replicate test: register %d", i)
    57  		replicateInstances.ReplicationList = append(replicateInstances.ReplicationList, &ReplicationInstance{
    58  			AppName:      appId,
    59  			Id:           instance.InstanceId,
    60  			InstanceInfo: instance,
    61  			Action:       actionRegister,
    62  		})
    63  	}
    64  	_, code := eurekaSrv.doBatchReplicate(replicateInstances, "", namespace)
    65  	assert.Equal(t, api.ExecuteSuccess, code)
    66  
    67  	time.Sleep(10 * time.Second)
    68  	for i := 0; i < 5; i++ {
    69  		eurekalog.Infof("replicate test: heartbeat %d", i)
    70  		replicateInstances = &ReplicationList{}
    71  		for _, instance := range instances {
    72  			replicateInstances.ReplicationList = append(replicateInstances.ReplicationList, &ReplicationInstance{
    73  				AppName: appId,
    74  				Id:      instance.InstanceId,
    75  				Action:  actionHeartbeat,
    76  			})
    77  		}
    78  		_, code := eurekaSrv.doBatchReplicate(replicateInstances, "", namespace)
    79  		assert.Equal(t, api.ExecuteSuccess, code)
    80  	}
    81  }