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

Function TestPoolCache

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 9

GetClientForMethod · 0.95
CountMethod · 0.95
RemoveClientForMethod · 0.95
RegisteredAddressesMethod · 0.95
StartAndAwaitRunningFunction · 0.92
StopAndAwaitTerminatedFunction · 0.92
PoolAddrFuncFuncType · 0.85
ErrorfMethod · 0.80
NewPoolFunction · 0.70

Tested by

no test coverage detected