test: Mock bull, fix setInterval capturing memory in tests

Towards #3939
This commit is contained in:
Tom Moor
2022-08-08 13:15:06 +02:00
parent ba385e1507
commit e97cc61e2f
3 changed files with 53 additions and 4 deletions

43
server/__mocks__/bull.ts Normal file
View File

@@ -0,0 +1,43 @@
export default class Queue {
done() {
//
}
on() {
//
}
count() {
return 0;
}
getDelayedCount() {
return 0;
}
add = function (data: any) {
const job = this.createJob(data);
if (!this.handler) {
throw Error(
"Mocking version requires handler to be set before first add()"
);
}
this.handler(job, this.done);
};
process = function (handler: any) {
if (this.handler) {
throw Error("Cannot define a handler more than once per Queue instance");
}
this.handler = handler;
};
createJob = function (data: any) {
return {
data: data,
};
};
}

View File

@@ -17,6 +17,8 @@ if (process.env.DATABASE_URL_TEST) {
// so that sequelize uses the test config variables
require("@server/database/sequelize");
jest.mock("bull");
// This is needed for the relative manual mock to be picked up
jest.mock("../queues");

View File

@@ -41,9 +41,13 @@ export function createQueue(
queue.on("failed", () => {
Metrics.increment(`${prefix}.jobs.failed`);
});
setInterval(async () => {
Metrics.gauge(`${prefix}.count`, await queue.count());
Metrics.gauge(`${prefix}.delayed_count`, await queue.getDelayedCount());
}, 5 * 1000);
if (env.ENVIRONMENT !== "test") {
setInterval(async () => {
Metrics.gauge(`${prefix}.count`, await queue.count());
Metrics.gauge(`${prefix}.delayed_count`, await queue.getDelayedCount());
}, 5 * 1000);
}
return queue;
}