TestPool validates the published behavior of [aibridged.CachedBridgePool]. It is not meant to be an exhaustive test of the internal cache's functionality, since that is already covered by its library.
(t *testing.T)
| 29 | // It is not meant to be an exhaustive test of the internal cache's functionality, |
| 30 | // since that is already covered by its library. |
| 31 | func TestPool(t *testing.T) { |
| 32 | t.Parallel() |
| 33 | |
| 34 | logger := slogtest.Make(t, nil) |
| 35 | |
| 36 | ctrl := gomock.NewController(t) |
| 37 | client := mock.NewMockDRPCClient(ctrl) |
| 38 | mcpProxy := mcpmock.NewMockServerProxier(ctrl) |
| 39 | |
| 40 | opts := aibridged.PoolOptions{MaxItems: 1, TTL: time.Second} |
| 41 | pool, err := aibridged.NewCachedBridgePool(opts, nil, logger, nil, testTracer) |
| 42 | require.NoError(t, err) |
| 43 | t.Cleanup(func() { pool.Shutdown(context.Background()) }) |
| 44 | |
| 45 | id, id2, apiKeyID1, apiKeyID2 := uuid.New(), uuid.New(), uuid.New(), uuid.New() |
| 46 | clientFn := func() (aibridged.DRPCClient, error) { |
| 47 | return client, nil |
| 48 | } |
| 49 | |
| 50 | // Once a pool instance is initialized, it will try setup its MCP proxier(s). |
| 51 | // This is called exactly once since the instance below is only created once. |
| 52 | mcpProxy.EXPECT().Init(gomock.Any()).Times(1).Return(nil) |
| 53 | // This is part of the lifecycle. |
| 54 | mcpProxy.EXPECT().Shutdown(gomock.Any()).AnyTimes().Return(nil) |
| 55 | |
| 56 | // Acquiring a pool instance will create one the first time it sees an |
| 57 | // initiator ID... |
| 58 | inst, err := pool.Acquire(t.Context(), aibridged.Request{ |
| 59 | SessionKey: "key", |
| 60 | InitiatorID: id, |
| 61 | APIKeyID: apiKeyID1.String(), |
| 62 | }, clientFn, newMockMCPFactory(mcpProxy)) |
| 63 | require.NoError(t, err, "acquire pool instance") |
| 64 | |
| 65 | // ...and it will return it when acquired again. |
| 66 | instB, err := pool.Acquire(t.Context(), aibridged.Request{ |
| 67 | SessionKey: "key", |
| 68 | InitiatorID: id, |
| 69 | APIKeyID: apiKeyID1.String(), |
| 70 | }, clientFn, newMockMCPFactory(mcpProxy)) |
| 71 | require.NoError(t, err, "acquire pool instance") |
| 72 | require.Same(t, inst, instB) |
| 73 | |
| 74 | cacheMetrics := pool.CacheMetrics() |
| 75 | require.EqualValues(t, 1, cacheMetrics.KeysAdded()) |
| 76 | require.EqualValues(t, 0, cacheMetrics.KeysEvicted()) |
| 77 | require.EqualValues(t, 1, cacheMetrics.Hits()) |
| 78 | require.EqualValues(t, 1, cacheMetrics.Misses()) |
| 79 | |
| 80 | // This will get called again because a new instance will be created. |
| 81 | mcpProxy.EXPECT().Init(gomock.Any()).Times(1).Return(nil) |
| 82 | |
| 83 | // But that key will be evicted when a new initiator is seen (maxItems=1): |
| 84 | inst2, err := pool.Acquire(t.Context(), aibridged.Request{ |
| 85 | SessionKey: "key", |
| 86 | InitiatorID: id2, |
| 87 | APIKeyID: apiKeyID1.String(), |
| 88 | }, clientFn, newMockMCPFactory(mcpProxy)) |
nothing calls this directly
no test coverage detected