MCPcopy Index your code
hub / github.com/coder/coder / TestPool_Expiry

Function TestPool_Expiry

coderd/aibridged/pool_test.go:271–328  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

269}
270
271func 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}

Callers

nothing calls this directly

Calls 15

EXPECTMethod · 0.95
ShutdownMethod · 0.95
AcquireMethod · 0.95
CacheMetricsMethod · 0.95
NewMockServerProxierFunction · 0.92
NewCachedBridgePoolFunction · 0.92
newMockMCPFactoryFunction · 0.85
MissesMethod · 0.80
TestMethod · 0.65
InitMethod · 0.65
ShutdownMethod · 0.65
CleanupMethod · 0.65

Tested by

no test coverage detected