Skip to content

Commit

Permalink
Accommodations to make it all work
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Nov 22, 2024
1 parent 3a472d3 commit f980f8b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
28 changes: 19 additions & 9 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ export async function withPgPool<T>(
cb: (pool: pg.Pool) => Promise<T>,
): Promise<T> {
const { TEST_CONNECTION_STRING } = databaseDetails!;
const pool = new pg.Pool({
const pgPool = new pg.Pool({
connectionString: TEST_CONNECTION_STRING,
max: 100,
});
pgPool.on("error", () => {});
pgPool.on("connect", () => {});
try {
return await cb(pool);
return await cb(pgPool);
} finally {
pool.end();
pgPool.end();
}
}

Expand Down Expand Up @@ -298,14 +300,22 @@ export function makeMockJob(taskIdentifier: string): Job {

export async function makeSelectionOfJobs(
utils: WorkerUtils,
pgClient: pg.PoolClient,
pgClient: pg.PoolClient | pg.Pool,
) {
const future = new Date(Date.now() + 60 * 60 * 1000);
const failedJob: DbJob = await utils.addJob("job3", { a: 1, runAt: future });
const regularJob1 = await utils.addJob("job3", { a: 2, runAt: future });
const lockedJob: DbJob = await utils.addJob("job3", { a: 3, runAt: future });
const regularJob2 = await utils.addJob("job3", { a: 4, runAt: future });
const untouchedJob = await utils.addJob("job3", { a: 5, runAt: future });
const failedJob: DbJob = await utils.addJob(
"job3",
{ a: 1 },
{ runAt: future },
);
const regularJob1 = await utils.addJob("job3", { a: 2 }, { runAt: future });
const lockedJob: DbJob = await utils.addJob(
"job3",
{ a: 3 },
{ runAt: future },
);
const regularJob2 = await utils.addJob("job3", { a: 4 }, { runAt: future });
const untouchedJob = await utils.addJob("job3", { a: 5 }, { runAt: future });
const {
rows: [lockedJobUpdate],
} = await pgClient.query<DbJob>(
Expand Down
7 changes: 6 additions & 1 deletion __tests__/runner.runOnce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
databaseDetails,
getJobs,
makeSelectionOfJobs,
reset,
sleep,
sleepUntil,
withPgPool,
Expand Down Expand Up @@ -92,10 +93,13 @@ test("at least a connectionString, a pgPool, the DATABASE_URL or PGDATABASE envv
});

test("connectionString and a pgPool cannot provided a the same time", async () => {
const pgPool = new Pool();
pgPool.on("error", () => {});
pgPool.on("connect", () => {});
const options: RunnerOptions = {
taskList: { task: () => {} },
connectionString: databaseDetails!.TEST_CONNECTION_STRING,
pgPool: new Pool(),
pgPool,
};
await runOnceErrorAssertion(
options,
Expand Down Expand Up @@ -215,6 +219,7 @@ test("gracefulShutdown", async () =>
},
},
};
await reset(pgPool, options);
utils = await makeWorkerUtils(options);
await utils.addJob("job1", { id: "test sleep" });
expect(_allWorkerPools).toHaveLength(0);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"eslint_d": "^13.0.0",
"graphile": "^5.0.0-beta.16",
"jest": "^26.0.0",
"jest-time-helpers": "0.1.0",
"jest-time-helpers": "0.1.1",
"juice": "5.2.0",
"pg-connection-string": "^2.6.2",
"postcss-nested": "^6.0.1",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7714,10 +7714,10 @@ jest-snapshot@^26.6.2:
pretty-format "^26.6.2"
semver "^7.3.2"

[email protected].0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/jest-time-helpers/-/jest-time-helpers-0.1.0.tgz#0d28164b4109035ce5010bfc5375b78700bfe793"
integrity sha512-rj3g5CPey4t1n/6HCEWL5epe7b33bPKurGOFmdDG7A6PoO6jtPfaCWNtRangFUCX7Z9aPr7D6nQfga635j1Fuw==
[email protected].1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/jest-time-helpers/-/jest-time-helpers-0.1.1.tgz#93a6ee318ccd50a27f27898460f62d987f431bc9"
integrity sha512-FJdTB98OTD16HYe0Y+98FLYPlfWAcU0lj8OZ4AUpKkWSbqX3lL4aLjYZaH9gvQG4KcG7/cpBFwV9MhGQ71MD4Q==

jest-util@^26.1.0, jest-util@^26.6.2:
version "26.6.2"
Expand Down

0 comments on commit f980f8b

Please sign in to comment.