github.com/polarismesh/polaris@v1.17.8/service/instance_check_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 service_test 19 20 import ( 21 "context" 22 "fmt" 23 "testing" 24 "time" 25 26 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 27 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 28 "github.com/stretchr/testify/assert" 29 "google.golang.org/protobuf/types/known/wrapperspb" 30 ) 31 32 func TestInstanceCheck(t *testing.T) { 33 discoverSuit := &DiscoverTestSuit{} 34 if err := discoverSuit.Initialize(); err != nil { 35 t.Fatal(err) 36 } 37 defer discoverSuit.Destroy() 38 39 instanceId1 := "inst_111" 40 instanceId2 := "inst_222" 41 42 discoverSuit.addInstance(t, &apiservice.Instance{ 43 Service: wrapperspb.String("polaris.checker"), 44 Namespace: wrapperspb.String("Polaris"), 45 Host: wrapperspb.String("127.0.0.1"), 46 Port: wrapperspb.UInt32(8091), 47 Protocol: wrapperspb.String("grpc"), 48 Metadata: map[string]string{"polaris_service": "polaris.checker"}, 49 }) 50 instanceIds := map[string]bool{instanceId1: true, instanceId2: true} 51 for id := range instanceIds { 52 resp := discoverSuit.DiscoverServer().RegisterInstance(context.Background(), &apiservice.Instance{Id: wrapperspb.String(id), 53 Service: wrapperspb.String("testSvc"), Namespace: wrapperspb.String("default"), 54 Host: wrapperspb.String("127.0.0.1"), Port: wrapperspb.UInt32(8888), Weight: wrapperspb.UInt32(100), 55 HealthCheck: &apiservice.HealthCheck{Type: apiservice.HealthCheck_HEARTBEAT, Heartbeat: &apiservice.HeartbeatHealthCheck{ 56 Ttl: wrapperspb.UInt32(2), 57 }}, 58 }) 59 assert.Equal(t, uint32(apimodel.Code_ExecuteSuccess), resp.GetCode().GetValue()) 60 } 61 //time.Sleep(20 * time.Second) 62 for i := 0; i < 50; i++ { 63 for instanceId := range instanceIds { 64 discoverSuit.HealthCheckServer().Report( 65 context.Background(), &apiservice.Instance{Id: &wrapperspb.StringValue{Value: instanceId}}) 66 } 67 time.Sleep(1 * time.Second) 68 } 69 70 instance1 := discoverSuit.DiscoverServer().Cache().Instance().GetInstance(instanceId1) 71 assert.NotNil(t, instance1) 72 assert.Equal(t, true, instance1.Proto.GetHealthy().GetValue()) 73 instance2 := discoverSuit.DiscoverServer().Cache().Instance().GetInstance(instanceId2) 74 assert.NotNil(t, instance2) 75 assert.Equal(t, true, instance2.Proto.GetHealthy().GetValue()) 76 77 delete(instanceIds, instanceId2) 78 for i := 0; i < 50; i++ { 79 for instanceId := range instanceIds { 80 fmt.Printf("%d report instance for %s, round 2\n", i, instanceId) 81 discoverSuit.HealthCheckServer().Report( 82 context.Background(), &apiservice.Instance{Id: &wrapperspb.StringValue{Value: instanceId}}) 83 } 84 time.Sleep(1 * time.Second) 85 } 86 instance1 = discoverSuit.DiscoverServer().Cache().Instance().GetInstance(instanceId1) 87 assert.NotNil(t, instance1) 88 assert.Equal(t, true, instance1.Proto.GetHealthy().GetValue()) 89 instance2 = discoverSuit.DiscoverServer().Cache().Instance().GetInstance(instanceId2) 90 assert.NotNil(t, instance2) 91 assert.Equal(t, false, instance2.Proto.GetHealthy().GetValue()) 92 } 93 94 func TestInstanceImmediatelyCheck(t *testing.T) { 95 discoverSuit := &DiscoverTestSuit{} 96 if err := discoverSuit.Initialize(); err != nil { 97 t.Fatal(err) 98 } 99 defer discoverSuit.Destroy() 100 101 instanceId1 := "inst_333" 102 instanceId2 := "inst_444" 103 104 discoverSuit.addInstance(t, &apiservice.Instance{ 105 Service: wrapperspb.String("polaris.checker"), 106 Namespace: wrapperspb.String("Polaris"), 107 Host: wrapperspb.String("127.0.0.1"), 108 Port: wrapperspb.UInt32(8091), 109 Protocol: wrapperspb.String("grpc"), 110 Metadata: map[string]string{"polaris_service": "polaris.checker"}, 111 }) 112 instanceIds := map[string]bool{instanceId1: true, instanceId2: true} 113 for id := range instanceIds { 114 resp := discoverSuit.DiscoverServer().RegisterInstance(context.Background(), &apiservice.Instance{Id: wrapperspb.String(id), 115 Service: wrapperspb.String("testSvc"), Namespace: wrapperspb.String("default"), 116 Host: wrapperspb.String("127.0.0.1"), Port: wrapperspb.UInt32(8888), Weight: wrapperspb.UInt32(100), 117 HealthCheck: &apiservice.HealthCheck{Type: apiservice.HealthCheck_HEARTBEAT, Heartbeat: &apiservice.HeartbeatHealthCheck{ 118 Ttl: wrapperspb.UInt32(2), 119 }}, 120 }) 121 assert.Equal(t, uint32(apimodel.Code_ExecuteSuccess), resp.GetCode().GetValue()) 122 } 123 for i := 0; i < 10; i++ { 124 for instanceId := range instanceIds { 125 fmt.Printf("%d report instance for %s, round 2\n", i, instanceId) 126 hbResp := discoverSuit.HealthCheckServer().Report( 127 context.Background(), &apiservice.Instance{Id: &wrapperspb.StringValue{Value: instanceId}}) 128 assert.Equal(t, uint32(apimodel.Code_ExecuteSuccess), hbResp.GetCode().GetValue()) 129 } 130 time.Sleep(1 * time.Second) 131 } 132 } 133 134 func TestInstanceCheckSuspended(t *testing.T) { 135 discoverSuit := &DiscoverTestSuit{} 136 if err := discoverSuit.Initialize(); err != nil { 137 t.Fatal(err) 138 } 139 defer discoverSuit.Destroy() 140 141 instanceId1 := "inst_555" 142 instanceId2 := "inst_666" 143 144 discoverSuit.addInstance(t, &apiservice.Instance{ 145 Service: wrapperspb.String("polaris.checker"), 146 Namespace: wrapperspb.String("Polaris"), 147 Host: wrapperspb.String("127.0.0.1"), 148 Port: wrapperspb.UInt32(8091), 149 Protocol: wrapperspb.String("grpc"), 150 Metadata: map[string]string{"polaris_service": "polaris.checker"}, 151 }) 152 instanceIds := map[string]bool{instanceId1: true, instanceId2: true} 153 for id := range instanceIds { 154 resp := discoverSuit.DiscoverServer().RegisterInstance(context.Background(), &apiservice.Instance{Id: wrapperspb.String(id), 155 Service: wrapperspb.String("testSvc"), Namespace: wrapperspb.String("default"), 156 Host: wrapperspb.String("127.0.0.1"), Port: wrapperspb.UInt32(8888), Weight: wrapperspb.UInt32(100), 157 HealthCheck: &apiservice.HealthCheck{Type: apiservice.HealthCheck_HEARTBEAT, Heartbeat: &apiservice.HeartbeatHealthCheck{ 158 Ttl: wrapperspb.UInt32(2), 159 }}, 160 }) 161 assert.Equal(t, uint32(apimodel.Code_ExecuteSuccess), resp.GetCode().GetValue()) 162 } 163 time.Sleep(5 * time.Second) 164 discoverSuit.addInstance(t, &apiservice.Instance{ 165 Service: wrapperspb.String("polaris.checker"), 166 Namespace: wrapperspb.String("Polaris"), 167 Host: wrapperspb.String("127.0.0.1"), 168 Port: wrapperspb.UInt32(8091), 169 Protocol: wrapperspb.String("grpc"), 170 Isolate: wrapperspb.Bool(true), 171 Metadata: map[string]string{"polaris_service": "polaris.checker"}, 172 }) 173 time.Sleep(5 * time.Second) 174 discoverSuit.addInstance(t, &apiservice.Instance{ 175 Service: wrapperspb.String("polaris.checker"), 176 Namespace: wrapperspb.String("Polaris"), 177 Host: wrapperspb.String("127.0.0.1"), 178 Port: wrapperspb.UInt32(8091), 179 Protocol: wrapperspb.String("grpc"), 180 Isolate: wrapperspb.Bool(false), 181 Metadata: map[string]string{"polaris_service": "polaris.checker"}, 182 }) 183 time.Sleep(5 * time.Second) 184 checkers := discoverSuit.HealthCheckServer().Checkers() 185 for _, checker := range checkers { 186 suspendTimeSec := checker.SuspendTimeSec() 187 assert.True(t, suspendTimeSec > 0) 188 } 189 190 } 191 192 func TestSelfInstanceCheck(t *testing.T) { 193 discoverSuit := &DiscoverTestSuit{} 194 if err := discoverSuit.Initialize(); err != nil { 195 t.Fatal(err) 196 } 197 defer discoverSuit.Destroy() 198 199 instanceId1 := "inst_self_1" 200 instanceId2 := "inst_self_2" 201 202 instanceIds := map[string]bool{instanceId1: true, instanceId2: true} 203 hbInterval := 4 204 205 for instanceId := range instanceIds { 206 discoverSuit.addInstance(t, &apiservice.Instance{ 207 Id: wrapperspb.String(instanceId), 208 Service: wrapperspb.String("polaris.checker"), 209 Namespace: wrapperspb.String("Polaris"), 210 Host: wrapperspb.String(instanceId), 211 Port: wrapperspb.UInt32(8091), 212 Protocol: wrapperspb.String("grpc"), 213 EnableHealthCheck: wrapperspb.Bool(true), 214 HealthCheck: &apiservice.HealthCheck{ 215 Type: apiservice.HealthCheck_HEARTBEAT, 216 Heartbeat: &apiservice.HeartbeatHealthCheck{ 217 Ttl: &wrapperspb.UInt32Value{Value: uint32(hbInterval)}, 218 }, 219 }, 220 Metadata: map[string]string{"polaris_service": "polaris.checker"}, 221 }) 222 } 223 224 for i := 0; i < 5; i++ { 225 for instanceId := range instanceIds { 226 fmt.Printf("%d report self instance for %s, round 1\n", i, instanceId) 227 discoverSuit.HealthCheckServer().Report( 228 context.Background(), &apiservice.Instance{ 229 Id: wrapperspb.String(instanceId), 230 }) 231 } 232 time.Sleep(2 * time.Second) 233 } 234 235 cacheProvider, _ := discoverSuit.HealthCheckServer().CacheProvider() 236 instance1 := cacheProvider.GetSelfServiceInstance(instanceId1) 237 assert.NotNil(t, instance1) 238 assert.Equal(t, true, instance1.Proto.GetHealthy().GetValue()) 239 instance2 := cacheProvider.GetSelfServiceInstance(instanceId2) 240 assert.NotNil(t, instance2) 241 assert.Equal(t, true, instance2.Proto.GetHealthy().GetValue()) 242 243 delete(instanceIds, instanceId2) 244 for i := 0; i < 10; i++ { 245 for instanceId := range instanceIds { 246 fmt.Printf("%d report instance for %s, round 2\n", i, instanceId) 247 discoverSuit.HealthCheckServer().Report( 248 context.Background(), &apiservice.Instance{ 249 Id: &wrapperspb.StringValue{Value: instanceId}, 250 }) 251 } 252 time.Sleep(2 * time.Second) 253 } 254 instance1 = cacheProvider.GetSelfServiceInstance(instanceId1) 255 assert.NotNil(t, instance1) 256 assert.Equal(t, true, instance1.Proto.GetHealthy().GetValue()) 257 instance2 = cacheProvider.GetSelfServiceInstance(instanceId2) 258 assert.NotNil(t, instance2) 259 assert.Equal(t, false, instance2.Proto.GetHealthy().GetValue()) 260 } 261 262 func TestSelfInstanceImmediatelyCheck(t *testing.T) { 263 discoverSuit := &DiscoverTestSuit{} 264 if err := discoverSuit.Initialize(); err != nil { 265 t.Fatal(err) 266 } 267 defer discoverSuit.Destroy() 268 269 instanceId1 := "inst_self_1" 270 instanceId2 := "inst_self_2" 271 272 hbInterval := 1 273 discoverSuit.addInstance(t, &apiservice.Instance{ 274 Id: wrapperspb.String(instanceId1), 275 Service: wrapperspb.String("polaris.checker"), 276 Namespace: wrapperspb.String("Polaris"), 277 Host: wrapperspb.String(instanceId1), 278 Port: wrapperspb.UInt32(8091), 279 Protocol: wrapperspb.String("grpc"), 280 EnableHealthCheck: wrapperspb.Bool(true), 281 HealthCheck: &apiservice.HealthCheck{ 282 Type: apiservice.HealthCheck_HEARTBEAT, 283 Heartbeat: &apiservice.HeartbeatHealthCheck{ 284 Ttl: &wrapperspb.UInt32Value{Value: uint32(hbInterval)}, 285 }, 286 }, 287 Metadata: map[string]string{"polaris_service": "polaris.checker"}, 288 }) 289 290 discoverSuit.addInstance(t, &apiservice.Instance{ 291 Id: wrapperspb.String(instanceId2), 292 Service: wrapperspb.String("polaris.checker"), 293 Namespace: wrapperspb.String("Polaris"), 294 Host: wrapperspb.String(instanceId2), 295 Port: wrapperspb.UInt32(8091), 296 Protocol: wrapperspb.String("grpc"), 297 EnableHealthCheck: wrapperspb.Bool(false), 298 Metadata: map[string]string{"polaris_service": "polaris.checker"}, 299 }) 300 301 time.Sleep(10 * time.Second) 302 _ = discoverSuit.DiscoverServer().Cache().Instance().Update() 303 304 cacheProvider, _ := discoverSuit.HealthCheckServer().CacheProvider() 305 instance1 := cacheProvider.GetSelfServiceInstance(instanceId1) 306 assert.NotNil(t, instance1) 307 assert.Equal(t, false, instance1.Proto.GetHealthy().GetValue()) 308 instance2 := cacheProvider.GetSelfServiceInstance(instanceId2) 309 assert.NotNil(t, instance2) 310 assert.Equal(t, true, instance2.Proto.GetHealthy().GetValue()) 311 312 }