import { EventEmitter } from "events";
export function describeAcrossBackends(topic, runTests) {
    describe(topic, () => {
        describe("Redis", () => {
            runTests("Redis");
        });
        describe("Mock", () => {
            runTests("Mock");
        });
    });
}
export function makeSignal() {
    const emitter = new EventEmitter();
    function waitForSignal(key = "") {
        return new Promise((resolve) => {
            emitter.on(key, resolve);
        });
    }
    waitForSignal.signal = (key = "") => {
        emitter.emit(key);
    };
    return waitForSignal;
}
function removeFirstStackLine(string) {
    return string.replace(/\n.*\n/, "");
}
export function waitUntil(predicate, butMax, interval = 20) {
    const potentialError = new Error(`Predicate was not fulfilled on time (${predicate.toString()})`);
    potentialError.showDiff = false;
    potentialError.stack = removeFirstStackLine(potentialError.stack);
    return new Promise((resolve, reject) => {
        const check = setInterval(() => {
            if (predicate()) {
                clearInterval(check);
                clearTimeout(max);
                resolve();
            }
        }, interval);
        const max = setTimeout(() => {
            clearInterval(check);
            reject(potentialError);
        }, butMax);
    });
}
export function expectToBeInRange(value, [from, to]) {
    expect(value).toBeGreaterThanOrEqual(from);
    expect(value).toBeLessThanOrEqual(to);
}
export function expectToHaveEqualMembers(actual, expected) {
    expect(actual).toHaveLength(expected.length);
    for (const e of expected) {
        expect(actual).toContainEqual(e);
    }
}
export async function stopTime(doIt) {
    const start = Date.now();
    await doIt();
    const end = Date.now();
    return end - start;
}
export function getAddress(server) {
    let { address, port } = server.address();
    if (address.startsWith("::")) {
        address = "localhost";
    }
    return `http://${address}:${port}`;
}
//# sourceMappingURL=util.js.map