Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,9 @@ jobs:
complete:
runs-on: ubuntu-latest
needs: [test, lint]
if: always()
steps:
- name: Fail if any required job did not succeed
if: ${{ needs.test.result != 'success' || needs.lint.result != 'success' }}
run: exit 1
- run: echo "Done!"
2 changes: 1 addition & 1 deletion __tests__/core/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("connection", () => {
let prefixedConnection: Connection;
let prefixedRedis: Redis;
beforeAll(async () => {
prefixedRedis = new Redis(null, null, {
prefixedRedis = new Redis({
keyPrefix: "customNamespace:",
db: db,
});
Expand Down
38 changes: 19 additions & 19 deletions __tests__/core/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ describe("queue", () => {

test("can add a normal job", async () => {
await queue.enqueue(specHelper.queue, "someJob", [1, 2, 3]);
let obj = await specHelper.popFromQueue();
expect(obj).toBeDefined();
obj = JSON.parse(obj);
const raw = await specHelper.popFromQueue();
expect(raw).toBeDefined();
const obj = JSON.parse(raw!);
expect(obj.class).toBe("someJob");
expect(obj.args).toEqual([1, 2, 3]);
});
Expand All @@ -60,7 +60,7 @@ describe("queue", () => {
specHelper.namespace + ":delayed:" + "10",
);
expect(str).toBeDefined();
const job = JSON.parse(str) as ParsedJob;
const job = JSON.parse(str!) as ParsedJob;
expect(job.class).toBe("someJob");
expect(job.args).toEqual([1, 2, 3]);
});
Expand All @@ -78,7 +78,7 @@ describe("queue", () => {
specHelper.namespace + ":delayed:" + "10",
);
expect(str).toBeDefined();
const job = JSON.parse(str) as ParsedJob;
const job = JSON.parse(str!) as ParsedJob;
expect(job.class).toBe("someJob");
expect(job.args).toEqual([1, 2, 3]);
});
Expand Down Expand Up @@ -122,7 +122,7 @@ describe("queue", () => {
specHelper.namespace + ":delayed:" + now,
);
expect(str).toBeDefined();
const job = JSON.parse(str) as ParsedJob;
const job = JSON.parse(str!) as ParsedJob;
expect(job.class).toBe("someJob");
expect(job.args).toEqual([1, 2, 3]);
});
Expand All @@ -142,7 +142,7 @@ describe("queue", () => {
specHelper.namespace + ":delayed:" + now,
);
expect(str).toBeDefined();
const job = JSON.parse(str) as ParsedJob;
const job = JSON.parse(str!) as ParsedJob;
expect(job.class).toBe("someJob");
expect(job.args).toEqual([1, 2, 3]);
});
Expand Down Expand Up @@ -244,15 +244,15 @@ describe("queue", () => {
// @ts-ignore
await queue.enqueue(specHelper.queue, "someJob", 1);
const obj = await specHelper.popFromQueue();
expect(JSON.parse(obj).args).toEqual([1]);
expect(JSON.parse(obj!).args).toEqual([1]);
});

test("allows omitting arguments when enqueuing", async () => {
await queue.enqueue(specHelper.queue, "noParams");
const length = await queue.length(specHelper.queue);
expect(length).toBe(1);
let obj = await specHelper.popFromQueue();
obj = JSON.parse(obj);
const raw = await specHelper.popFromQueue();
const obj = JSON.parse(raw!);
expect(obj.class).toBe("noParams");
expect(Array.isArray(obj.args)).toBe(true);
expect(obj.args).toHaveLength(0);
Expand Down Expand Up @@ -608,7 +608,7 @@ describe("queue", () => {
let str = await specHelper.redis.rpop(
specHelper.namespace + ":" + "failed",
);
const failedData = JSON.parse(str) as ParsedFailedJobPayload;
const failedData = JSON.parse(str!) as ParsedFailedJobPayload;
expect(failedData.queue).toBe(specHelper.queue);
expect(failedData.exception).toBe(
"Worker Timeout (killed manually)",
Expand Down Expand Up @@ -660,16 +660,16 @@ describe("queue", () => {

const errorPayload = await queue.forceCleanWorker(workerA.name);

expect(errorPayload.worker).toBe("workerA");
expect(errorPayload.queue).toBe("test_queue");
expect(errorPayload.payload.class).toBe("slowJob");
expect(errorPayload.exception).toBe(
expect(errorPayload!.worker).toBe("workerA");
expect(errorPayload!.queue).toBe("test_queue");
expect(errorPayload!.payload.class).toBe("slowJob");
expect(errorPayload!.exception).toBe(
"Worker Timeout (killed manually)",
);
expect(errorPayload.backtrace[0]).toMatch(/killed by/);
expect(errorPayload.backtrace[1]).toBe("queue#forceCleanWorker");
expect(errorPayload.backtrace[2]).toBe("node-resque");
expect(errorPayload.failed_at).toBeTruthy();
expect(errorPayload!.backtrace[0]).toMatch(/killed by/);
expect(errorPayload!.backtrace[1]).toBe("queue#forceCleanWorker");
expect(errorPayload!.backtrace[2]).toBe("node-resque");
expect(errorPayload!.failed_at).toBeTruthy();

return resolve(null);
});
Expand Down
12 changes: 6 additions & 6 deletions __tests__/core/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ describe("scheduler", () => {
[1, 2, 3],
);
await scheduler.poll();
let obj = await specHelper.popFromQueue();
expect(obj).toBeDefined();
obj = JSON.parse(obj);
const raw = await specHelper.popFromQueue();
expect(raw).toBeDefined();
const obj = JSON.parse(raw!);
expect(obj.class).toBe("someJob");
expect(obj.args).toEqual([1, 2, 3]);
await scheduler.end();
Expand All @@ -157,11 +157,11 @@ describe("scheduler", () => {
let worker: Worker;
const jobs = {
stuck: {
perform: async function () {
perform: async function (this: Worker) {
await new Promise((resolve) => {
// stop the worker from checking in, like the process crashed
// don't resolve
clearTimeout(this.pingTimer);
if (this.pingTimer) clearTimeout(this.pingTimer);
});
},
} as Job<any>,
Expand Down Expand Up @@ -213,7 +213,7 @@ describe("scheduler", () => {
const str = await specHelper.redis.rpop(
specHelper.namespace + ":" + "failed",
);
const failed = JSON.parse(str) as ParsedFailedJobPayload;
const failed = JSON.parse(str!) as ParsedFailedJobPayload;
expect(failed.queue).toBe("stuckJobs");
expect(failed.exception).toBe(
"Worker Timeout (killed manually)",
Expand Down
8 changes: 4 additions & 4 deletions __tests__/core/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ describe("worker", () => {
let str = await specHelper.redis.rpop(
specHelper.namespace + ":" + "failed",
);
const data = JSON.parse(str) as ParsedFailedJobPayload;
const data = JSON.parse(str!) as ParsedFailedJobPayload;
expect(data.queue).toBe(specHelper.queue);
expect(data.exception).toBe("Error");
expect(data.error).toBe('No job defined for class "somethingFake"');
Expand All @@ -324,15 +324,15 @@ describe("worker", () => {
const nowInSeconds = Math.round(new Date().getTime() / 1000);
await worker.start();
await new Promise((resolve) =>
setTimeout(resolve, worker.options.timeout * 2),
setTimeout(resolve, worker.options.timeout! * 2),
);
const pingKey = worker.connection.key(
"worker",
"ping",
worker.name,
);
const firstPayload = JSON.parse(
await specHelper.redis.get(pingKey),
(await specHelper.redis.get(pingKey))!,
);
expect(firstPayload.name).toEqual(worker.name);
expect(firstPayload.time).toBeGreaterThanOrEqual(nowInSeconds);
Expand All @@ -346,7 +346,7 @@ describe("worker", () => {
});

const secondPayload = JSON.parse(
await specHelper.redis.get(pingKey),
(await specHelper.redis.get(pingKey))!,
);
expect(secondPayload.name).toEqual(worker.name);
expect(secondPayload.time).toBeGreaterThanOrEqual(
Expand Down
2 changes: 1 addition & 1 deletion __tests__/plugins/jobLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe("plugins", () => {
Math.round(timestamps[0] / 1000),
);
expect(str).toBeDefined();
const dealyedJob = JSON.parse(str) as ParsedJob;
const dealyedJob = JSON.parse(str!) as ParsedJob;
expect(dealyedJob.class).toBe("slowAdd");
expect(dealyedJob.args).toEqual([1, 2]);

Expand Down
2 changes: 1 addition & 1 deletion __tests__/plugins/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ describe("plugins", () => {
`${specHelper.namespace}:failure-resque-retry:brokenJob:1-2`,
);
expect(String(retryAttempts)).toBe("0");
const failure = JSON.parse(failureData) as ParsedFailedJobPayload;
const failure = JSON.parse(failureData!) as ParsedFailedJobPayload;
expect(failure.payload).toEqual([1, 2]);
expect(failure.exception).toBe("Error: BUSTED");
expect(failure.worker).toBe("brokenJob");
Expand Down
68 changes: 47 additions & 21 deletions __tests__/utils/specHelper.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
import Redis from "ioredis";
import * as NodeResque from "../../src/index";
import { ConnectionOptions } from "../../src/types/options";

const namespace = `resque-test-${process.env.JEST_WORKER_ID || 0}`;
const queue = "test_queue";
const queueName = "test_queue";
const pkg = "ioredis";

interface SpecConnectionDetails extends ConnectionOptions {
pkg: string;
host: string;
password: string;
port: number;
database: number;
namespace: string;
options?: { [key: string]: any };
redis?: Redis;
}

interface SpecHelper {
pkg: string;
namespace: string;
queue: string;
timeout: number;
smallTimeout: number;
redis: Redis;
connectionDetails: SpecConnectionDetails;
worker: NodeResque.Worker;
scheduler: NodeResque.Scheduler;
connect: () => Promise<void>;
cleanup: () => Promise<void>;
disconnect: () => Promise<void>;
startAll: (jobs: NodeResque.Jobs) => Promise<void>;
endAll: () => Promise<void>;
popFromQueue: () => Promise<string | null>;
cleanConnectionDetails: () => { database: number; namespace: string };
}

const SpecHelper = {
pkg: pkg,
namespace: namespace,
queue: queue,
queue: queueName,
timeout: 500,
smallTimeout: 3,
redis: null as Redis,
redis: null as unknown as Redis,
connectionDetails: {
pkg: pkg,
host: process.env.REDIS_HOST || "127.0.0.1",
password: "",
port: 6379,
database: parseInt(process.env.JEST_WORKER_ID || "0"),
namespace: namespace,
// looping: true
},
} as SpecConnectionDetails,
worker: null as unknown as NodeResque.Worker,
scheduler: null as unknown as NodeResque.Scheduler,

connect: async function () {
connect: async function (this: SpecHelper) {
if (!this.connectionDetails.options) this.connectionDetails.options = {};
this.connectionDetails.options.db =
this.connectionDetails?.options?.database;
this.connectionDetails.options.db = this.connectionDetails.database;
this.redis = new Redis(
this.connectionDetails.port,
this.connectionDetails.host,
Expand All @@ -43,30 +74,28 @@ const SpecHelper = {
this.connectionDetails.redis = this.redis;
},

cleanup: async function () {
cleanup: async function (this: SpecHelper) {
const keys = await this.redis.keys(this.namespace + "*");
if (keys.length > 0) await this.redis.del(keys);
},

disconnect: async function () {
disconnect: async function (this: SpecHelper) {
if (typeof this.redis.disconnect === "function") {
await this.redis.disconnect();
this.redis.disconnect();
} else if (typeof this.redis.quit === "function") {
await this.redis.quit();
}

delete this.redis;
this.redis = null as unknown as Redis;
delete this.connectionDetails.redis;
},

startAll: async function (jobs: NodeResque.Jobs) {
startAll: async function (this: SpecHelper, jobs: NodeResque.Jobs) {
const Worker = NodeResque.Worker;
const Scheduler = NodeResque.Scheduler;
const Queue = NodeResque.Queue;

this.worker = new Worker(
{
//@ts-ignore
connection: { redis: this.redis },
queues: this.queue,
timeout: this.timeout,
Expand All @@ -81,21 +110,18 @@ const SpecHelper = {
});

await this.scheduler.connect();

this.queue = new Queue({ connection: { redis: this.redis } });
await this.queue.connect();
},

endAll: async function () {
endAll: async function (this: SpecHelper) {
await this.worker.end();
await this.scheduler.end();
},

popFromQueue: async function () {
popFromQueue: async function (this: SpecHelper) {
return this.redis.lpop(this.namespace + ":queue:" + this.queue);
},

cleanConnectionDetails: function () {
cleanConnectionDetails: function (this: SpecHelper) {
interface connectionDetails {
database: number;
namespace: string;
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
maxWorkers: "50%",
testPathIgnorePatterns: ["<rootDir>/__tests__/utils"],
transform: {
"^.+\\.ts?$": "ts-jest",
"^.+\\.ts?$": ["ts-jest", { tsconfig: "tsconfig.test.json" }],
},
};
8 changes: 8 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"types": ["jest", "node"]
},
"include": ["./src/**/*", "./__tests__/**/*"]
}