(t *testing.T)
| 269 | } |
| 270 | |
| 271 | func TestPool_Expiry(t *testing.T) { |
| 272 | t.Parallel() |
| 273 | |
| 274 | synctest.Test(t, func(t *testing.T) { |
| 275 | logger := slogtest.Make(t, nil) |
| 276 | ctrl := gomock.NewController(t) |
| 277 | client := mock.NewMockDRPCClient(ctrl) |
| 278 | mcpProxy := mcpmock.NewMockServerProxier(ctrl) |
| 279 | mcpProxy.EXPECT().Init(gomock.Any()).AnyTimes().Return(nil) |
| 280 | mcpProxy.EXPECT().Shutdown(gomock.Any()).AnyTimes().Return(nil) |
| 281 | |
| 282 | const ttl = time.Second |
| 283 | opts := aibridged.PoolOptions{MaxItems: 1, TTL: ttl} |
| 284 | pool, err := aibridged.NewCachedBridgePool(opts, nil, logger, nil, testTracer) |
| 285 | require.NoError(t, err) |
| 286 | t.Cleanup(func() { pool.Shutdown(context.Background()) }) |
| 287 | |
| 288 | req := aibridged.Request{ |
| 289 | SessionKey: "key", |
| 290 | InitiatorID: uuid.New(), |
| 291 | APIKeyID: uuid.New().String(), |
| 292 | } |
| 293 | clientFn := func() (aibridged.DRPCClient, error) { |
| 294 | return client, nil |
| 295 | } |
| 296 | |
| 297 | ctx := t.Context() |
| 298 | |
| 299 | // First acquire is a cache miss. |
| 300 | _, err = pool.Acquire(ctx, req, clientFn, newMockMCPFactory(mcpProxy)) |
| 301 | require.NoError(t, err) |
| 302 | |
| 303 | // Second acquire is a cache hit. |
| 304 | _, err = pool.Acquire(ctx, req, clientFn, newMockMCPFactory(mcpProxy)) |
| 305 | require.NoError(t, err) |
| 306 | |
| 307 | metrics := pool.CacheMetrics() |
| 308 | require.EqualValues(t, 1, metrics.Misses()) |
| 309 | require.EqualValues(t, 1, metrics.Hits()) |
| 310 | |
| 311 | // TTL expires |
| 312 | time.Sleep(ttl + time.Millisecond) |
| 313 | |
| 314 | // Third acquire is a cache miss because the entry expired. |
| 315 | _, err = pool.Acquire(ctx, req, clientFn, newMockMCPFactory(mcpProxy)) |
| 316 | require.NoError(t, err) |
| 317 | |
| 318 | metrics = pool.CacheMetrics() |
| 319 | require.EqualValues(t, 2, metrics.Misses()) |
| 320 | require.EqualValues(t, 1, metrics.Hits()) |
| 321 | |
| 322 | // Wait for all eviction goroutines to complete before gomock's ctrl.Finish() |
| 323 | // runs in test cleanup. ristretto's OnEvict callback spawns goroutines that |
| 324 | // need to finish calling mcpProxy.Shutdown() before ctrl.finish clears the |
| 325 | // expectations. |
| 326 | synctest.Wait() |
| 327 | }) |
| 328 | } |
nothing calls this directly
no test coverage detected