MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestPoolCache

Function TestPoolCache

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

Source from the content-addressed store, hash-verified

58}
59
60func TestPoolCache(t *testing.T) {
61 buildCount := 0
62 factory := func(addr string) (PoolClient, error) {
63 if addr == "bad" {
64 return nil, fmt.Errorf("Fail")
65 }
66 buildCount++
67 return mockClient{happy: true, status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
68 }
69
70 cfg := PoolConfig{
71 HealthCheckTimeout: 50 * time.Millisecond,
72 CheckInterval: 10 * time.Second,
73 }
74
75 pool := NewPool("test", cfg, nil, factory, nil, log.NewNopLogger())
76 require.NoError(t, services.StartAndAwaitRunning(context.Background(), pool))
77 defer services.StopAndAwaitTerminated(context.Background(), pool) //nolint:errcheck
78
79 _, err := pool.GetClientFor("1")
80 require.NoError(t, err)
81 if buildCount != 1 {
82 t.Errorf("Did not create client")
83 }
84
85 _, err = pool.GetClientFor("1")
86 require.NoError(t, err)
87 if buildCount != 1 {
88 t.Errorf("Created client that should have been cached")
89 }
90
91 _, err = pool.GetClientFor("2")
92 require.NoError(t, err)
93 if pool.Count() != 2 {
94 t.Errorf("Expected Count() = 2, got %d", pool.Count())
95 }
96
97 pool.RemoveClientFor("1")
98 if pool.Count() != 1 {
99 t.Errorf("Expected Count() = 1, got %d", pool.Count())
100 }
101
102 _, err = pool.GetClientFor("1")
103 require.NoError(t, err)
104 if buildCount != 3 || pool.Count() != 2 {
105 t.Errorf("Did not re-create client correctly")
106 }
107
108 _, err = pool.GetClientFor("bad")
109 if err == nil {
110 t.Errorf("Bad create should have thrown an error")
111 }
112 if pool.Count() != 2 {
113 t.Errorf("Bad create should not have been added to cache")
114 }
115
116 addrs := pool.RegisteredAddresses()
117 if len(addrs) != pool.Count() {

Callers

nothing calls this directly

Calls 7

GetClientForMethod · 0.95
CountMethod · 0.95
RemoveClientForMethod · 0.95
RegisteredAddressesMethod · 0.95
StartAndAwaitRunningFunction · 0.92
StopAndAwaitTerminatedFunction · 0.92
NewPoolFunction · 0.70

Tested by

no test coverage detected