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,