MCPcopy
hub / github.com/grafana/dskit / TestCleanUnhealthyWithGracePeriod

Function TestCleanUnhealthyWithGracePeriod

ring/client/pool_test.go:174–214  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

172}
173
174func TestCleanUnhealthyWithGracePeriod(t *testing.T) {
175 const alwaysGood = "always-good"
176 const flapping = "flapping"
177 const neverGood = "never-good"
178 flappingClient := &mockClient{happy: true, status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}
179
180 clients := map[string]*poolMember{
181 alwaysGood: {client: &mockClient{happy: true, status: grpc_health_v1.HealthCheckResponse_SERVING}},
182 flapping: {client: flappingClient},
183 neverGood: {client: &mockClient{happy: true, status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}},
184 }
185
186 cfg := PoolConfig{
187 CheckInterval: 500 * time.Millisecond,
188 HealthCheckTimeout: 5 * time.Millisecond,
189 HealthCheckGracePeriod: time.Second,
190 }
191
192 pool := NewPool("test", cfg, nil, nil, nil, log.NewNopLogger())
193 pool.members = clients
194
195 pool.cleanUnhealthy()
196 require.ElementsMatch(t, []string{alwaysGood, flapping, neverGood}, pool.RegisteredAddresses(), "no clients should be removed before minimum failure period has expired")
197
198 time.Sleep(cfg.HealthCheckGracePeriod / 2)
199 pool.cleanUnhealthy()
200 require.ElementsMatch(t, []string{alwaysGood, flapping, neverGood}, pool.RegisteredAddresses(), "no clients should be removed before minimum failure period has expired, even after a subsequent failure")
201
202 flappingClient.status = grpc_health_v1.HealthCheckResponse_SERVING
203 time.Sleep((cfg.HealthCheckGracePeriod / 2) + time.Millisecond)
204 pool.cleanUnhealthy()
205 require.ElementsMatch(t, []string{alwaysGood, flapping}, pool.RegisteredAddresses(), "only client that has consistently failed all health checks during minimum failure period should be removed")
206
207 flappingClient.status = grpc_health_v1.HealthCheckResponse_NOT_SERVING
208 pool.cleanUnhealthy()
209 require.ElementsMatch(t, []string{alwaysGood, flapping}, pool.RegisteredAddresses(), "no clients should be removed if they have just become unhealthy")
210
211 time.Sleep(cfg.HealthCheckGracePeriod + time.Millisecond)
212 pool.cleanUnhealthy()
213 require.ElementsMatch(t, []string{alwaysGood}, pool.RegisteredAddresses(), "client should be removed if still unhealthy after minimum failure period has expired")
214}
215
216func TestRemoveClient(t *testing.T) {
217 const (

Callers

nothing calls this directly

Calls 4

cleanUnhealthyMethod · 0.95
RegisteredAddressesMethod · 0.95
NewPoolFunction · 0.70
SleepMethod · 0.65

Tested by

no test coverage detected