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

Function TestRemoveClient

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

Source from the content-addressed store, hash-verified

214}
215
216func TestRemoveClient(t *testing.T) {
217 const (
218 addr1 = "localhost:123"
219 addr2 = "localhost:234"
220 )
221
222 assertNotEqual := func(t *testing.T, clientA, clientB PoolClient, msg string) {
223 t.Helper()
224 // Using golang equality. `assert.NotEqual` inspects the value of the interface implementation.
225 if clientA == clientB {
226 t.Error(msg)
227 }
228 }
229
230 assertEqual := func(t *testing.T, clientA, clientB PoolClient, msg string) {
231 t.Helper()
232 // Using golang equality. `assert.Equal` inspects the value of the interface implementation.
233 if clientA != clientB {
234 t.Error(msg)
235 }
236 }
237
238 clientHappy := true
239 factory := PoolAddrFunc(func(string) (PoolClient, error) {
240 return &mockClient{happy: clientHappy, status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
241 })
242
243 cfg := PoolConfig{
244 CheckInterval: time.Second,
245 HealthCheckEnabled: false,
246 }
247 pool := NewPool("test", cfg, nil, factory, nil, log.NewNopLogger())
248 require.NoError(t, services.StartAndAwaitRunning(context.Background(), pool))
249 defer services.StopAndAwaitTerminated(context.Background(), pool) //nolint:errcheck
250
251 c1a, err := pool.GetClientFor(addr1)
252 require.NoError(t, err)
253
254 c2a, err := pool.GetClientFor(addr2)
255 require.NoError(t, err)
256 assertNotEqual(t, c1a, c2a, "clients for different addresses should be different")
257
258 // Remove without providing address
259 pool.RemoveClient(c1a, "")
260
261 c1b, err := pool.GetClientFor(addr1)
262 require.NoError(t, err)
263 assertNotEqual(t, c1a, c1b, "c1a should have been removed and c1b should be a different instance")
264
265 c2b, err := pool.GetClientFor(addr2)
266 require.NoError(t, err)
267 assertEqual(t, c2b, c2a, "c2b should be the same")
268
269 // Remove with providing an address
270 pool.RemoveClient(c1b, addr1)
271
272 // Prepare for cleaning up unhealthy instances later
273 clientHappy = false

Callers

nothing calls this directly

Calls 9

GetClientForMethod · 0.95
RemoveClientMethod · 0.95
cleanUnhealthyMethod · 0.95
StartAndAwaitRunningFunction · 0.92
StopAndAwaitTerminatedFunction · 0.92
PoolAddrFuncFuncType · 0.85
HelperMethod · 0.80
NewPoolFunction · 0.70
ErrorMethod · 0.45

Tested by

no test coverage detected