Skip to content

Commit

Permalink
Make the test more reliable by polling the database
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Oct 19, 2023
1 parent f3302f5 commit b2e1812
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions __tests__/cron-timing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,31 @@ test("does not schedule duplicate jobs when a job key is supplied", () =>
// Allow the first copy of the job to get scheduled
await setTime(REFERENCE_TIMESTAMP + 4 * HOUR + 1 * SECOND); // 4:00:01am
expect(cronScheduleCalls.count).toEqual(1);

// Makes the test more reliable, due to separate connections to Postgres being slightly out of sync
await sleep(150);
const jobs = await getJobs(pgPool);
let jobs!: Awaited<ReturnType<typeof getJobs>>;
for (let i = 0; i < 10; i++) {
jobs = await getJobs(pgPool);
if (jobs.length > 0) break;

Check failure on line 170 in __tests__/cron-timing.test.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Expected { after 'if' condition

Check failure on line 170 in __tests__/cron-timing.test.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Expected { after 'if' condition
await sleep(100);
}

expect(jobs).toEqual([
expect.objectContaining({ task_identifier: "my_task", key: "foo" }),
]);

// Allow the system to reschedule the job after seeing it wasn't picked up
await setTime(REFERENCE_TIMESTAMP + 8 * HOUR + 1 * SECOND); // 8:00:01am
expect(cronScheduleCalls.count).toEqual(2);

// Makes the test more reliable, due to separate connections to Postgres being slightly out of sync
await sleep(150);
const jobs2 = await getJobs(pgPool);
let jobs2!: Awaited<ReturnType<typeof getJobs>>;
for (let i = 0; i < 10; i++) {
jobs2 = await getJobs(pgPool);
if (jobs2[0].payload._cron.ts !== "2021-01-01T04:00:00.000Z") break;

Check failure on line 186 in __tests__/cron-timing.test.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Expected { after 'if' condition

Check failure on line 186 in __tests__/cron-timing.test.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Expected { after 'if' condition
await sleep(100);
}

expect(jobs2).toEqual([
expect.objectContaining({
task_identifier: "my_task",
Expand Down

0 comments on commit b2e1812

Please sign in to comment.