Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds delay enqueue hooks #737

Closed
wants to merge 1 commit into from
Closed
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
261 changes: 261 additions & 0 deletions __tests__/core/pluginRunner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
import specHelper from "../utils/specHelper";
import { Queue, Worker, Job } from "../../src";
import { HooksPlugin } from "../utils/custom-plugin";
import { RunPlugins } from "../../src/core/pluginRunner";

describe("plugin runner", () => {
const jobs = {
//@ts-ignore
myJob: {
plugins: [HooksPlugin],
perform: async () => {
throw new Error("should not get here");
},
} as Job<any>,
};

describe("queue hooks", () => {
const queue = new Queue(
{
connection: specHelper.cleanConnectionDetails(),
queue: specHelper.queue,
},
jobs
);

describe("beforeEnqueue", () => {
test("it will not permit adding properties to args", async () => {
HooksPlugin.prototype.beforeEnqueue = async function () {
this.args.push(3);
return true;
};

await expect(
RunPlugins(
queue,
"beforeEnqueue",
"myJob",
specHelper.queue,
jobs.myJob,
[1, 2]
)
).rejects.toThrow(TypeError);
});

test("it will not permit adding properties to nested args", async () => {
HooksPlugin.prototype.beforeEnqueue = async function () {
this.args[0].a.b = true;
return true;
};

await expect(
RunPlugins(
queue,
"beforeEnqueue",
"myJob",
specHelper.queue,
jobs.myJob,
[{ a: {} }]
)
).rejects.toThrow(TypeError);
});
});

describe("afterEnqueue", () => {
test("it will not permit adding properties to args", async () => {
HooksPlugin.prototype.afterEnqueue = async function () {
this.args.push(3);
return true;
};

await expect(
RunPlugins(
queue,
"afterEnqueue",
"myJob",
specHelper.queue,
jobs.myJob,
[1, 2]
)
).rejects.toThrow(TypeError);
});

test("it will not permit adding properties to nested args", async () => {
HooksPlugin.prototype.afterEnqueue = async function () {
this.args[0].a.b = true;
return true;
};

await expect(
RunPlugins(
queue,
"afterEnqueue",
"myJob",
specHelper.queue,
jobs.myJob,
[{ a: {} }]
)
).rejects.toThrow(TypeError);
});
});

describe("beforeDelayEnqueue", () => {
test("it will not permit adding properties to args", async () => {
HooksPlugin.prototype.beforeDelayEnqueue = async function () {
this.args.push(3);
return true;
};

await expect(
RunPlugins(
queue,
"beforeDelayEnqueue",
"myJob",
specHelper.queue,
jobs.myJob,
[1, 2]
)
).rejects.toThrow(TypeError);
});

test("it will not permit adding properties to nested args", async () => {
HooksPlugin.prototype.beforeDelayEnqueue = async function () {
this.args[0].a.b = true;
return true;
};

await expect(
RunPlugins(
queue,
"beforeDelayEnqueue",
"myJob",
specHelper.queue,
jobs.myJob,
[{ a: {} }]
)
).rejects.toThrow(TypeError);
});
});

describe("afterDelayEnqueue", () => {
test("it will not permit adding properties to args", async () => {
HooksPlugin.prototype.afterDelayEnqueue = async function () {
this.args.push(3);
return true;
};

await expect(
RunPlugins(
queue,
"afterDelayEnqueue",
"myJob",
specHelper.queue,
jobs.myJob,
[1, 2]
)
).rejects.toThrow(TypeError);
});

test("it will not permit adding properties to nested args", async () => {
HooksPlugin.prototype.afterDelayEnqueue = async function () {
this.args[0].a.b = true;
return true;
};

await expect(
RunPlugins(
queue,
"afterDelayEnqueue",
"myJob",
specHelper.queue,
jobs.myJob,
[{ a: {} }]
)
).rejects.toThrow(TypeError);
});
});
});

describe("worker hooks", () => {
const worker = new Worker(
{ connection: specHelper.connectionDetails, queues: [specHelper.queue] },
{}
);

describe("beforePerform", () => {
test("it will not permit adding properties to args", async () => {
HooksPlugin.prototype.beforePerform = async function () {
this.args.push(3);
return true;
};

await expect(
RunPlugins(
worker,
"beforePerform",
"myJob",
specHelper.queue,
jobs.myJob,
[1, 2]
)
).rejects.toThrow(TypeError);
});

test("it will not permit adding properties to nested args", async () => {
HooksPlugin.prototype.beforePerform = async function () {
this.args[0].a.b = true;
return true;
};

await expect(
RunPlugins(
worker,
"beforePerform",
"myJob",
specHelper.queue,
jobs.myJob,
[{ a: {} }]
)
).rejects.toThrow(TypeError);
});
});

describe("afterPerform", () => {
test("it will not permit adding properties to args", async () => {
HooksPlugin.prototype.afterPerform = async function () {
this.args.push(3);
return true;
};

await expect(
RunPlugins(
worker,
"afterPerform",
"myJob",
specHelper.queue,
jobs.myJob,
[1, 2]
)
).rejects.toThrow(TypeError);
});

test("it will not permit adding properties to nested args", async () => {
HooksPlugin.prototype.afterPerform = async function () {
this.args[0].a.b = true;
return true;
};

await expect(
RunPlugins(
worker,
"afterPerform",
"myJob",
specHelper.queue,
jobs.myJob,
[{ a: {} }]
)
).rejects.toThrow(TypeError);
});
});
});
});
36 changes: 36 additions & 0 deletions __tests__/integration/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { LockArgs, DeepMerge } from "../../src/utils/functions";

describe("utility helpers", () => {
describe("LockArgs", () => {
test("should return an array", () => {
expect(LockArgs({ a: 1 })).toEqual([{ a: 1 }]);
});

test("should return a sealed array", () => {
const locked = LockArgs({ a: 1 });
expect(Object.isSealed(locked)).toEqual(true);
});

test("should seal nested property values", () => {
const locked = LockArgs({ a: [{ b: true }] });
expect(Object.isSealed(locked[0].a[0].b)).toEqual(true);
});
});

describe("DeepMerge", () => {
test("should override existing properties on target object", () => {
const target = [{ a: 1, b: 2 }];
const merge = [{ a: 8, c: 3 }];
expect(DeepMerge(target, merge)).toEqual([{ a: 8, b: 2, c: 3 }]);
});

test("should override existing nested properties on target object", () => {
const target = [{ a: [{ b: 1 }] }];
const merge = [{ a: [{ b: 2 }] }, "new property"];
expect(DeepMerge(target, merge)).toEqual([
{ a: [{ b: 2 }] },
"new property",
]);
});
});
});
8 changes: 8 additions & 0 deletions __tests__/plugins/queueLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ class NeverRunPlugin extends Plugin {
return true;
}

async beforeDelayEnqueue() {
return true;
}

async afterEnqueue() {
return true;
}

async afterDelayEnqueue() {
return true;
}

async beforePerform() {
return false;
}
Expand Down
34 changes: 34 additions & 0 deletions __tests__/utils/custom-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ export class CustomPlugin extends Plugin {
return false;
}

async beforeDelayEnqueue() {
return false;
}

async afterEnqueue() {
return false;
}

async afterDelayEnqueue() {
return false;
}

async beforePerform() {
return false;
}
Expand All @@ -18,3 +26,29 @@ export class CustomPlugin extends Plugin {
return false;
}
}

export class HooksPlugin extends Plugin {
async beforeEnqueue() {
return true;
}

async beforeDelayEnqueue() {
return true;
}

async afterEnqueue() {
return true;
}

async afterDelayEnqueue() {
return true;
}

async beforePerform() {
return true;
}

async afterPerform() {
return true;
}
}
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading