github.com/polarismesh/polaris@v1.17.8/plugin/healthchecker/memory/checker_memory_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 heartbeatmemory 19 20 import ( 21 "context" 22 "sync/atomic" 23 "testing" 24 "time" 25 26 "github.com/stretchr/testify/assert" 27 28 commontime "github.com/polarismesh/polaris/common/time" 29 "github.com/polarismesh/polaris/common/utils" 30 "github.com/polarismesh/polaris/plugin" 31 ) 32 33 func TestMemoryHealthChecker_Query(t *testing.T) { 34 mhc := MemoryHealthChecker{ 35 hbRecords: utils.NewSyncMap[string, *HeartbeatRecord](), 36 } 37 id := "key1" 38 reportRequest := &plugin.ReportRequest{ 39 QueryRequest: plugin.QueryRequest{ 40 InstanceId: id, 41 }, 42 LocalHost: "127.0.0.1", 43 CurTimeSec: 1, 44 Count: 5, 45 } 46 err := mhc.Report(context.Background(), reportRequest) 47 assert.Nil(t, err) 48 49 queryRequest := plugin.QueryRequest{ 50 InstanceId: id, 51 } 52 qr, err := mhc.Query(context.Background(), &queryRequest) 53 assert.Nil(t, err) 54 assert.Equal(t, reportRequest.LocalHost, qr.Server) 55 assert.Equal(t, reportRequest.Count, qr.Count) 56 assert.Equal(t, reportRequest.CurTimeSec, qr.LastHeartbeatSec) 57 } 58 59 func TestMemoryHealthChecker_Check(t *testing.T) { 60 mhc := MemoryHealthChecker{ 61 hbRecords: utils.NewSyncMap[string, *HeartbeatRecord](), 62 } 63 test := &HeartbeatRecord{ 64 Server: "127.0.0.1", 65 CurTimeSec: 1, 66 } 67 mhc.hbRecords.Store("key", test) 68 69 queryRequest := plugin.CheckRequest{ 70 QueryRequest: plugin.QueryRequest{ 71 InstanceId: "key", 72 Host: "127.0.0.2", 73 Port: 80, 74 Healthy: true, 75 }, 76 CurTimeSec: func() int64 { 77 return time.Now().Unix() 78 }, 79 ExpireDurationSec: 15, 80 } 81 qr, err := mhc.Check(&queryRequest) 82 assert.NoError(t, err) 83 assert.False(t, qr.StayUnchanged) 84 85 queryRequest = plugin.CheckRequest{ 86 QueryRequest: plugin.QueryRequest{ 87 InstanceId: "key", 88 Host: "127.0.0.2", 89 Port: 80, 90 Healthy: false, 91 }, 92 CurTimeSec: func() int64 { 93 return time.Now().Unix() 94 }, 95 ExpireDurationSec: 15, 96 } 97 qr, err = mhc.Check(&queryRequest) 98 assert.NoError(t, err) 99 assert.True(t, qr.StayUnchanged) 100 101 test = &HeartbeatRecord{ 102 Server: "127.0.0.1", 103 CurTimeSec: time.Now().Unix(), 104 } 105 mhc.hbRecords.Store("key", test) 106 107 queryRequest = plugin.CheckRequest{ 108 QueryRequest: plugin.QueryRequest{ 109 InstanceId: "key", 110 Host: "127.0.0.2", 111 Port: 80, 112 Healthy: false, 113 }, 114 CurTimeSec: func() int64 { 115 return time.Now().Unix() 116 }, 117 ExpireDurationSec: 15, 118 } 119 qr, err = mhc.Check(&queryRequest) 120 assert.NoError(t, err) 121 assert.False(t, qr.StayUnchanged) 122 } 123 124 func TestReportAndCheck(t *testing.T) { 125 checker := MemoryHealthChecker{ 126 hbRecords: utils.NewSyncMap[string, *HeartbeatRecord](), 127 } 128 startTime := commontime.CurrentMillisecond() / 1000 129 instanceId := "testId" 130 host := "localhost" 131 var count int64 132 var port uint32 = 8888 133 reportReq := &plugin.ReportRequest{ 134 QueryRequest: plugin.QueryRequest{ 135 InstanceId: instanceId, 136 Host: host, 137 Port: port, 138 }, 139 LocalHost: "127.0.0.1", 140 CurTimeSec: startTime, 141 Count: atomic.AddInt64(&count, 1), 142 } 143 err := checker.Report(context.Background(), reportReq) 144 assert.Nil(t, err) 145 146 queryResp, err := checker.Query(context.Background(), &reportReq.QueryRequest) 147 assert.Nil(t, err) 148 assert.Equal(t, reportReq.CurTimeSec, queryResp.LastHeartbeatSec) 149 150 // after 3 seconds 151 curTimeSec := startTime + 3 152 checkReq := &plugin.CheckRequest{ 153 QueryRequest: plugin.QueryRequest{ 154 InstanceId: instanceId, 155 Host: host, 156 Port: port, 157 Healthy: true, 158 }, 159 ExpireDurationSec: 2, 160 CurTimeSec: func() int64 { 161 return curTimeSec 162 }, 163 } 164 resp, err := checker.Check(checkReq) 165 assert.Nil(t, err) 166 assert.False(t, resp.StayUnchanged) 167 assert.False(t, resp.Healthy) 168 169 reportReq.CurTimeSec = curTimeSec 170 reportReq.Count = atomic.AddInt64(&count, 1) 171 err = checker.Report(context.Background(), reportReq) 172 assert.Nil(t, err) 173 174 time.Sleep(3 * time.Second) 175 checker.Suspend() 176 startTime = commontime.CurrentMillisecond() / 1000 177 reportReq.CurTimeSec = startTime 178 reportReq.Count = atomic.AddInt64(&count, 1) 179 err = checker.Report(context.Background(), reportReq) 180 assert.Nil(t, err) 181 182 checkReq = &plugin.CheckRequest{ 183 QueryRequest: plugin.QueryRequest{ 184 InstanceId: instanceId, 185 Host: host, 186 Port: port, 187 Healthy: true, 188 }, 189 ExpireDurationSec: 2, 190 CurTimeSec: func() int64 { 191 return startTime 192 }, 193 } 194 resp, err = checker.Check(checkReq) 195 assert.Nil(t, err) 196 assert.True(t, resp.StayUnchanged) 197 198 // after 4 seconds 199 time.Sleep(4 * time.Second) 200 checkReq = &plugin.CheckRequest{ 201 QueryRequest: plugin.QueryRequest{ 202 InstanceId: instanceId, 203 Host: host, 204 Port: port, 205 Healthy: true, 206 }, 207 ExpireDurationSec: 2, 208 CurTimeSec: func() int64 { 209 return commontime.CurrentMillisecond() / 1000 210 }, 211 } 212 resp, err = checker.Check(checkReq) 213 assert.Nil(t, err) 214 assert.False(t, resp.StayUnchanged) 215 assert.False(t, resp.Healthy) 216 }