From 02efb1d6b5e6e83fa11232fb2361c1b60c827148 Mon Sep 17 00:00:00 2001 From: "lia-by-librechat[bot]" <328778573+lia-by-librechat[bot]@users.noreply.github.com> Date: Fri, 18 Sep 2026 06:05:39 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A5=20test:=20Hold=20Group=20Cache=20R?= =?UTF-8?q?eads=20Until=20Concurrent=20Callers=20Arrive=20(#16059)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `getUserPrincipals caching > deduplicates concurrent cache builds for the same member key` fails intermittently in CI, expecting one `cache.set` and receiving two. It failed that way on an unrelated pull request, on a `data-schemas` job whose diff touched nothing in this package. The test starts three concurrent lookups behind a fake cache whose reads resolve after 10ms and whose `get` never returns what `set` stored, then asserts a single build. `getMemberGroupIds` registers its in-flight entry only after the awaited cache read, so the three callers coalesce only while they overlap. When the scheduler serializes them, the first build finishes and clears its entry before a later caller reads, and that caller takes the miss path alone. In production it would find the value the first build wrote; against a fake that always misses it rebuilds and writes again, which is the second `set`. Reproduced deterministically by resolving the first read immediately and later reads after 50ms: expected 1, received 2, exactly as CI reported. The delay is now an arrival barrier that holds each read until all three are inside the miss window, so the test pins the concurrency it describes instead of depending on timing. Reads arriving after the barrier opens pass straight through, so the second read a lock holder performs cannot deadlock. Both concurrency tests share the store-backed fake cache, and a caller that does miss alone now behaves as it would in production. The tests also became stricter. With the in-flight check disabled they fail at three builds rather than two, so a genuine regression no longer looks like the flake it used to produce. Co-authored-by: Lia --- .../src/methods/userGroup.spec.ts | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/packages/data-schemas/src/methods/userGroup.spec.ts b/packages/data-schemas/src/methods/userGroup.spec.ts index 46ba4cdc7d..7a33ac633c 100644 --- a/packages/data-schemas/src/methods/userGroup.spec.ts +++ b/packages/data-schemas/src/methods/userGroup.spec.ts @@ -484,11 +484,14 @@ describe('userGroup methods', () => { }); describe('getUserPrincipals caching', () => { - function createFakeCache() { + function createFakeCache({ onRead }: { onRead?: () => Promise } = {}) { const store = new Map(); return { store, - get: jest.fn(async (key: string) => store.get(key)), + get: jest.fn(async (key: string) => { + await onRead?.(); + return store.get(key); + }), set: jest.fn(async (key: string, value: unknown) => { store.set(key, value); }), @@ -497,6 +500,29 @@ describe('userGroup methods', () => { }; } + /** + * Holds each cache read until `callers` of them are inside the miss window together, so a + * test about deduplicating concurrent builds actually gets concurrent builds. Delaying + * every read by a fixed few milliseconds instead leaves the overlap up to the scheduler: + * under load the first build can finish before a later caller reads, and that caller then + * takes the miss path on its own, which is correct behaviour but not what such a test is + * asserting. Reads after the barrier opens pass straight through, so a caller that reads + * again mid-build (after taking the lock, say) cannot deadlock. + */ + function arrivalBarrier(callers: number) { + let arrived = 0; + let open!: () => void; + const allArrived = new Promise((resolve) => { + open = resolve; + }); + return async () => { + if (++arrived >= callers) { + open(); + } + await allArrived; + }; + } + function createCachedMethods(cache: ReturnType) { return createUserGroupMethods(mongoose, { getCache: jest.fn(() => cache) }); } @@ -758,14 +784,8 @@ describe('userGroup methods', () => { it('deduplicates concurrent cache builds for the same member key', async () => { const user = await createTestUser({ idOnTheSource: 'dedup-ext-1' }); - const cache = { - get: jest.fn(async () => { - await new Promise((resolve) => setTimeout(resolve, 10)); - return undefined; - }), - set: jest.fn(async () => undefined), - }; - const cachedMethods = createUserGroupMethods(mongoose, { getCache: jest.fn(() => cache) }); + const cache = createFakeCache({ onRead: arrivalBarrier(3) }); + const cachedMethods = createCachedMethods(cache); const params = { userId: user._id.toString(), role: SystemRoles.USER, @@ -787,11 +807,7 @@ describe('userGroup methods', () => { it('shares one lock and DB build across concurrent same-process callers', async () => { const user = await createTestUser({ idOnTheSource: 'lock-ext-1' }); const cache = { - get: jest.fn(async () => { - await new Promise((resolve) => setTimeout(resolve, 10)); - return undefined; - }), - set: jest.fn(async () => undefined), + ...createFakeCache({ onRead: arrivalBarrier(3) }), acquireLock: jest.fn(async () => 'lock-token'), releaseLock: jest.fn(async () => undefined), lockWaitMs: 5000,