"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleep = void 0;
const __1 = require("..");
const runQuirrel_1 = require("../../api/test/runQuirrel");
const http_1 = __importDefault(require("http"));
const util_1 = require("./util");
function sleep(ms) {
    return new Promise((res) => setTimeout(res, ms));
}
exports.sleep = sleep;
test("exclusive (repro #717)", async () => {
    const server = await (0, runQuirrel_1.run)("Mock");
    const endpoint = http_1.default
        .createServer((req, res) => {
        let body = "";
        req.on("data", (data) => {
            body += data;
        });
        req.on("end", () => {
            quirrel.respondTo(body, req.headers).then(({ status, body }) => {
                res.write(body);
                res.statusCode === status;
                res.end();
            });
        });
    })
        .listen(0);
    const log = [];
    const quirrel = new __1.QuirrelClient({
        route: "",
        async handler(payload) {
            log.push("started " + payload);
            await sleep(100);
            log.push("finished " + payload);
        },
        config: {
            quirrelBaseUrl: (0, util_1.getAddress)(server.server),
            encryptionSecret: "4ws8syoOgeQX6WFvXuUneGNwy7QvLxpk",
            applicationBaseUrl: (0, util_1.getAddress)(endpoint),
        },
    });
    const inFiftyMS = new Date(Date.now() + 100);
    for (let i = 0; i < 3; i++) {
        await quirrel.enqueue(i, {
            exclusive: true,
            runAt: inFiftyMS,
        });
    }
    await (0, util_1.waitUntil)(() => log.length === 6, 1000);
    const i = (s) => log.indexOf(s);
    expect(i("finished 0") - i("started 0")).toBe(1);
    expect(i("finished 1") - i("started 1")).toBe(1);
    expect(i("finished 2") - i("started 2")).toBe(1);
    server.teardown();
    endpoint.close();
});
//# sourceMappingURL=exclusive.test.js.map