github.com/nacos-group/nacos-sdk-go@v1.1.4/clients/naming_client/beat_reactor_test.go (about)

     1  /*
     2   * Copyright 1999-2020 Alibaba Group Holding Ltd.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package naming_client
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/nacos-group/nacos-sdk-go/common/nacos_server"
    24  
    25  	"github.com/nacos-group/nacos-sdk-go/model"
    26  	"github.com/nacos-group/nacos-sdk-go/util"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestBeatReactor_AddBeatInfo(t *testing.T) {
    31  	br := NewBeatReactor(NamingProxy{nacosServer: &nacos_server.NacosServer{}}, 5000)
    32  	serviceName := "Test"
    33  	groupName := "public"
    34  	beatInfo := &model.BeatInfo{
    35  		Ip:          "127.0.0.1",
    36  		Port:        8080,
    37  		Metadata:    map[string]string{},
    38  		ServiceName: util.GetGroupName(serviceName, groupName),
    39  		Cluster:     "default",
    40  		Weight:      1,
    41  		Period:      time.Second * 5,
    42  	}
    43  	br.AddBeatInfo(util.GetGroupName(serviceName, groupName), beatInfo)
    44  	key := buildKey(util.GetGroupName(serviceName, groupName), beatInfo.Ip, beatInfo.Port)
    45  	result, ok := br.beatMap.Get(key)
    46  	assert.Equal(t, ok, true, "key should exists!")
    47  	assert.ObjectsAreEqual(result.(*model.BeatInfo), beatInfo)
    48  }
    49  
    50  func TestBeatReactor_RemoveBeatInfo(t *testing.T) {
    51  	br := NewBeatReactor(NamingProxy{nacosServer: &nacos_server.NacosServer{}}, 5000)
    52  	serviceName := "Test"
    53  	groupName := "public"
    54  	beatInfo1 := &model.BeatInfo{
    55  		Ip:          "127.0.0.1",
    56  		Port:        8080,
    57  		Metadata:    map[string]string{},
    58  		ServiceName: util.GetGroupName(serviceName, groupName),
    59  		Cluster:     "default",
    60  		Weight:      1,
    61  		Period:      time.Second * 5,
    62  	}
    63  	br.AddBeatInfo(util.GetGroupName(serviceName, groupName), beatInfo1)
    64  	beatInfo2 := &model.BeatInfo{
    65  		Ip:          "127.0.0.2",
    66  		Port:        8080,
    67  		Metadata:    map[string]string{},
    68  		ServiceName: util.GetGroupName(serviceName, groupName),
    69  		Cluster:     "default",
    70  		Weight:      1,
    71  		Period:      time.Second * 5,
    72  	}
    73  	br.AddBeatInfo(util.GetGroupName(serviceName, groupName), beatInfo2)
    74  	br.RemoveBeatInfo(util.GetGroupName(serviceName, groupName), "127.0.0.1", 8080)
    75  	key := buildKey(util.GetGroupName(serviceName, groupName), beatInfo2.Ip, beatInfo2.Port)
    76  	result, ok := br.beatMap.Get(key)
    77  	assert.Equal(t, br.beatMap.Count(), 1, "beatinfo map length should be 1")
    78  	assert.Equal(t, ok, true, "key should exists!")
    79  	assert.ObjectsAreEqual(result.(*model.BeatInfo), beatInfo2)
    80  
    81  }