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