github.com/nacos-group/nacos-sdk-go/v2@v2.2.5/clients/naming_client/naming_cache/subscribe_callback_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_cache
    18  
    19  import (
    20  	"fmt"
    21  	"log"
    22  	"strings"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/nacos-group/nacos-sdk-go/v2/model"
    27  	"github.com/nacos-group/nacos-sdk-go/v2/util"
    28  	"github.com/nacos-group/nacos-sdk-go/v2/vo"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestEventDispatcher_AddCallbackFuncs(t *testing.T) {
    33  	service := model.Service{
    34  		Clusters:    strings.Join([]string{"default"}, ","),
    35  		CacheMillis: 10000,
    36  		Checksum:    "abcd",
    37  		LastRefTime: uint64(time.Now().Unix()),
    38  	}
    39  	var hosts []model.Instance
    40  	host := model.Instance{
    41  		Enable:      true,
    42  		InstanceId:  "123",
    43  		Port:        8080,
    44  		Ip:          "127.0.0.1",
    45  		Weight:      10,
    46  		ServiceName: "public@@Test",
    47  		ClusterName: strings.Join([]string{"default"}, ","),
    48  	}
    49  	hosts = append(hosts, host)
    50  	service.Hosts = hosts
    51  
    52  	ed := NewSubscribeCallback()
    53  	param := vo.SubscribeParam{
    54  		ServiceName: "Test",
    55  		Clusters:    []string{"default"},
    56  		GroupName:   "public",
    57  		SubscribeCallback: func(services []model.Instance, err error) {
    58  			fmt.Println(util.ToJsonString(ed.callbackFuncMap))
    59  		},
    60  	}
    61  	ed.AddCallbackFunc(util.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","), &param.SubscribeCallback)
    62  	key := util.GetServiceCacheKey(util.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
    63  	for k, v := range ed.callbackFuncMap.Items() {
    64  		assert.Equal(t, key, k, "key should be equal!")
    65  		funcs := v.([]*func(services []model.Instance, err error))
    66  		assert.Equal(t, len(funcs), 1)
    67  		assert.Equal(t, funcs[0], &param.SubscribeCallback, "callback function must be equal!")
    68  
    69  	}
    70  }
    71  
    72  func TestEventDispatcher_RemoveCallbackFuncs(t *testing.T) {
    73  	service := model.Service{
    74  		Clusters:    strings.Join([]string{"default"}, ","),
    75  		CacheMillis: 10000,
    76  		Checksum:    "abcd",
    77  		LastRefTime: uint64(time.Now().Unix()),
    78  	}
    79  	var hosts []model.Instance
    80  	host := model.Instance{
    81  		Enable:      true,
    82  		InstanceId:  "123",
    83  		Port:        8080,
    84  		Ip:          "127.0.0.1",
    85  		Weight:      10,
    86  		ServiceName: "public@@Test",
    87  		ClusterName: strings.Join([]string{"default"}, ","),
    88  	}
    89  	hosts = append(hosts, host)
    90  	service.Hosts = hosts
    91  
    92  	ed := NewSubscribeCallback()
    93  	param := vo.SubscribeParam{
    94  		ServiceName: "Test",
    95  		Clusters:    []string{"default"},
    96  		GroupName:   "public",
    97  		SubscribeCallback: func(services []model.Instance, err error) {
    98  			fmt.Printf("func1:%s \n", util.ToJsonString(services))
    99  		},
   100  	}
   101  	ed.AddCallbackFunc(util.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","), &param.SubscribeCallback)
   102  	assert.Equal(t, len(ed.callbackFuncMap.Items()), 1, "callback funcs map length should be 1")
   103  
   104  	param2 := vo.SubscribeParam{
   105  		ServiceName: "Test",
   106  		Clusters:    []string{"default"},
   107  		GroupName:   "public",
   108  		SubscribeCallback: func(services []model.Instance, err error) {
   109  			fmt.Printf("func2:%s \n", util.ToJsonString(services))
   110  		},
   111  	}
   112  	ed.AddCallbackFunc(util.GetGroupName(param2.ServiceName, param2.GroupName), strings.Join(param2.Clusters, ","), &param2.SubscribeCallback)
   113  	assert.Equal(t, len(ed.callbackFuncMap.Items()), 1, "callback funcs map length should be 2")
   114  
   115  	for k, v := range ed.callbackFuncMap.Items() {
   116  		log.Printf("key:%s,%d", k, len(v.([]*func(services []model.Instance, err error))))
   117  	}
   118  
   119  	ed.RemoveCallbackFunc(util.GetGroupName(param2.ServiceName, param2.GroupName), strings.Join(param2.Clusters, ","), &param2.SubscribeCallback)
   120  
   121  	key := util.GetServiceCacheKey(util.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
   122  	for k, v := range ed.callbackFuncMap.Items() {
   123  		assert.Equal(t, key, k, "key should be equal!")
   124  		funcs := v.([]*func(services []model.Instance, err error))
   125  		assert.Equal(t, len(funcs), 1)
   126  		assert.Equal(t, funcs[0], &param.SubscribeCallback, "callback function must be equal!")
   127  
   128  	}
   129  }
   130  
   131  func TestSubscribeCallback_ServiceChanged(t *testing.T) {
   132  	service := model.Service{
   133  		Name:        "public@@Test",
   134  		Clusters:    strings.Join([]string{"default"}, ","),
   135  		CacheMillis: 10000,
   136  		Checksum:    "abcd",
   137  		LastRefTime: uint64(time.Now().Unix()),
   138  	}
   139  	var hosts []model.Instance
   140  	host := model.Instance{
   141  		Enable:      true,
   142  		InstanceId:  "123",
   143  		Port:        8080,
   144  		Ip:          "127.0.0.1",
   145  		Weight:      10,
   146  		ServiceName: "public@@Test",
   147  		ClusterName: strings.Join([]string{"default"}, ","),
   148  	}
   149  	hosts = append(hosts, host)
   150  	service.Hosts = hosts
   151  
   152  	ed := NewSubscribeCallback()
   153  	param := vo.SubscribeParam{
   154  		ServiceName: "Test",
   155  		Clusters:    []string{"default"},
   156  		GroupName:   "public",
   157  		SubscribeCallback: func(services []model.Instance, err error) {
   158  			log.Printf("func1:%s \n", util.ToJsonString(services))
   159  		},
   160  	}
   161  	ed.AddCallbackFunc(util.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","), &param.SubscribeCallback)
   162  
   163  	param2 := vo.SubscribeParam{
   164  		ServiceName: "Test",
   165  		Clusters:    []string{"default"},
   166  		GroupName:   "public",
   167  		SubscribeCallback: func(services []model.Instance, err error) {
   168  			log.Printf("func2:%s \n", util.ToJsonString(services))
   169  
   170  		},
   171  	}
   172  	ed.AddCallbackFunc(util.GetGroupName(param2.ServiceName, param2.GroupName), strings.Join(param2.Clusters, ","), &param2.SubscribeCallback)
   173  	cacheKey := util.GetServiceCacheKey(util.GetGroupName(service.Name, service.GroupName), service.Clusters)
   174  	ed.ServiceChanged(cacheKey, &service)
   175  }