mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-09 04:50:59 +00:00
test(integration): wait for the ledger row, not for fifty milliseconds
The delivery ledger is written without awaiting the result, so the row appears some time after the dispatch returns. The test slept a flat fifty milliseconds and then read, which holds on a warm machine and loses on a loaded runner: the assertion reported an empty ledger as though the dispatch had not recorded the skip. It polls for the rows now, up to five seconds, and fails with a message naming how many it found. The claim is that the row is written, which is what the code promises; that it is written within fifty milliseconds is not.
This commit is contained in:
1 parent
92ba0513d2
commit
83c54a6b79
1 file changed
+23
-4
@@ -59,9 +59,28 @@ async function dispatch() {
|
||||
});
|
||||
}
|
||||
|
||||
/** The ledger write is fire-and-forget; let its microtask land. */
|
||||
async function settleLedger(): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
/**
|
||||
* The ledger write is fire-and-forget, so the row appears some time after the
|
||||
* dispatch returns. This used to wait a flat 50 ms, which is enough on a warm
|
||||
* machine and not enough on a cold container: the suite failed here once with
|
||||
* an empty ledger while nothing about the dispatch had changed. It waits for
|
||||
* the rows instead, and gives up loudly rather than letting the assertion
|
||||
* below report the absence as a product defect.
|
||||
*/
|
||||
async function settleLedger(expected: number): Promise<void> {
|
||||
const deadline = Date.now() + 5_000;
|
||||
for (;;) {
|
||||
const seen = await getPrismaClient().pushAttempt.count({
|
||||
where: { userId: TEST_USER_ID },
|
||||
});
|
||||
if (seen >= expected) return;
|
||||
if (Date.now() > deadline) {
|
||||
throw new Error(
|
||||
`the delivery ledger still holds ${seen} of ${expected} rows after five seconds`,
|
||||
);
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 25));
|
||||
}
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
@@ -82,7 +101,7 @@ describe("instance-wide notification channel switch", () => {
|
||||
await setNtfyGlobal(false);
|
||||
|
||||
const outcome = await dispatch();
|
||||
await settleLedger();
|
||||
await settleLedger(1);
|
||||
|
||||
expect(ntfySendMock).not.toHaveBeenCalled();
|
||||
expect(outcome).toEqual({
|
||||
|
||||
Reference in new issue
Block a user