forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjobs.test.js
More file actions
66 lines (55 loc) · 1.91 KB
/
Copy pathjobs.test.js
File metadata and controls
66 lines (55 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { jest } from "@jest/globals";
import request from "supertest";
import app from "../app.js";
import {
enqueue,
registerJob,
resetInlineQueueForTests,
waitForIdle,
} from "../src/jobs/queue.js";
describe("background job queue", () => {
beforeEach(() => {
process.env.QUEUE_DRIVER = "inline";
resetInlineQueueForTests();
});
afterEach(() => {
jest.useRealTimers();
});
it("deduplicates jobs with the same idempotency key", async () => {
const handler = jest.fn();
registerJob("test-idempotency", handler);
const first = await enqueue("test-idempotency", { recordId: "one" }, { idempotencyKey: "same" });
const second = await enqueue("test-idempotency", { recordId: "one" }, { idempotencyKey: "same" });
await waitForIdle();
expect(first.queued).toBe(true);
expect(second.duplicate).toBe(true);
expect(handler).toHaveBeenCalledTimes(1);
});
it("retries failed work with backoff", async () => {
jest.useFakeTimers();
const handler = jest
.fn()
.mockRejectedValueOnce(new Error("temporary outage"))
.mockRejectedValueOnce(new Error("temporary outage"))
.mockResolvedValueOnce();
registerJob("test-retry", handler);
await enqueue(
"test-retry",
{ recordId: "retry-me" },
{ attempts: 3, backoffMs: 100, idempotencyKey: "retry-once" }
);
await jest.runAllTimersAsync();
await waitForIdle();
expect(handler).toHaveBeenCalledTimes(3);
expect(handler.mock.calls.map((call) => call[1].attempt)).toEqual([1, 2, 3]);
});
it("protects the jobs dashboard with a bearer token", async () => {
process.env.JOBS_DASHBOARD_TOKEN = "dashboard-secret";
const missing = await request(app).get("/admin/jobs");
const wrong = await request(app)
.get("/admin/jobs")
.set("Authorization", "Bearer wrong");
expect(missing.statusCode).toBe(401);
expect(wrong.statusCode).toBe(401);
});
});