🚥 test: Hold Group Cache Reads Until Concurrent Callers Arrive (#16059)

`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 <lia@librechat.ai>
This commit is contained in:
lia-by-librechat[bot] 2026-09-18 06:05:39 -04:00 • committed by GitHub
parent db674eda60
commit 02efb1d6b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -484,11 +484,14 @@ describe('userGroup methods', () => {
});
describe('getUserPrincipals caching', () => {
function createFakeCache() {
function createFakeCache({ onRead }: { onRead?: () => Promise<void> } = {}) {
const store = new Map<string, unknown>();
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<void>((resolve) => {
open = resolve;
});
return async () => {
if (++arrived >= callers) {
open();
}
await allArrived;
};
}
function createCachedMethods(cache: ReturnType<typeof createFakeCache>) {
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,