"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
const runQuirrel_1 = require("../../api/test/runQuirrel");
const http_1 = __importDefault(require("http"));
const util_1 = require("./util");
test("retry", async () => {
    const server = await (0, runQuirrel_1.run)("Mock");
    let tries = 0;
    const $tries = (0, util_1.makeSignal)();
    const receiver = http_1.default
        .createServer((req, res) => {
        $tries.signal("" + ++tries);
        res.statusCode = 500;
        res.end();
    })
        .listen(0);
    const quirrel = new __1.QuirrelClient({
        route: "",
        async handler() { },
        config: {
            quirrelBaseUrl: (0, util_1.getAddress)(server.server),
            encryptionSecret: "4ws8syoOgeQX6WFvXuUneGNwy7QvLxpk",
            applicationBaseUrl: (0, util_1.getAddress)(receiver),
        },
    });
    const enqueueDate = Date.now();
    await quirrel.enqueue("hello world", {
        retry: ["10ms", "100ms", "200ms"],
        id: "retry",
    });
    await $tries("2");
    const secondTry = Date.now();
    (0, util_1.expectToBeInRange)(secondTry - enqueueDate, [5, 300]);
    const jobAfterFirstRetry = await quirrel.getById("retry");
    const nextIteration = jobAfterFirstRetry === null || jobAfterFirstRetry === void 0 ? void 0 : jobAfterFirstRetry.count;
    expect(nextIteration).toBe(3);
    await $tries("4");
    const lastTry = Date.now();
    expect(tries).toBe(4);
    (0, util_1.expectToBeInRange)(lastTry - enqueueDate, [195, 600]);
    const jobAfterAllRetries = await quirrel.getById("retry");
    expect(jobAfterAllRetries).toBeNull();
    server.teardown();
    receiver.close();
});
//# sourceMappingURL=retry.test.js.map