var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
    if (value !== null && value !== void 0) {
        if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
        var dispose, inner;
        if (async) {
            if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
            dispose = value[Symbol.asyncDispose];
        }
        if (dispose === void 0) {
            if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
            dispose = value[Symbol.dispose];
            if (async) inner = dispose;
        }
        if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
        if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
        env.stack.push({ value: value, dispose: dispose, async: async });
    }
    else if (async) {
        env.stack.push({ async: true });
    }
    return value;
};
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
    return function (env) {
        function fail(e) {
            env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
            env.hasError = true;
        }
        var r, s = 0;
        function next() {
            while (r = env.stack.pop()) {
                try {
                    if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
                    if (r.dispose) {
                        var result = r.dispose.call(r.value);
                        if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
                    }
                    else s |= 1;
                }
                catch (e) {
                    fail(e);
                }
            }
            if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
            if (env.hasError) throw env.error;
        }
        return next();
    };
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
    var e = new Error(message);
    return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
});
import { describe, expect, it } from 'vitest';
import { AsyncLock } from '../async-lock.js';
describe('AsyncLock', () => {
    it('serializes critical sections in FIFO order', async () => {
        const lock = new AsyncLock();
        const order = [];
        async function task(id, delayMs) {
            const env_1 = { stack: [], error: void 0, hasError: false };
            try {
                const _release = __addDisposableResource(env_1, await lock.acquire(), false);
                order.push(id);
                await new Promise((resolve) => setTimeout(resolve, delayMs));
            }
            catch (e_1) {
                env_1.error = e_1;
                env_1.hasError = true;
            }
            finally {
                __disposeResources(env_1);
            }
        }
        // Start three tasks "concurrently". Despite descending delays, the lock
        // forces them to run one at a time in acquisition order.
        await Promise.all([task(1, 15), task(2, 10), task(3, 5)]);
        expect(order).toStrictEqual([1, 2, 3]);
    });
    it('prevents overlap of holders', async () => {
        const lock = new AsyncLock();
        let active = 0;
        let maxActive = 0;
        async function task() {
            const env_2 = { stack: [], error: void 0, hasError: false };
            try {
                const _release = __addDisposableResource(env_2, await lock.acquire(), false);
                active++;
                maxActive = Math.max(maxActive, active);
                await new Promise((resolve) => setTimeout(resolve, 5));
                active--;
            }
            catch (e_2) {
                env_2.error = e_2;
                env_2.hasError = true;
            }
            finally {
                __disposeResources(env_2);
            }
        }
        await Promise.all([task(), task(), task(), task()]);
        expect(maxActive).toBe(1);
    });
    it('releases the lock even if the holder throws', async () => {
        const env_3 = { stack: [], error: void 0, hasError: false };
        try {
            const lock = new AsyncLock();
            try {
                const env_4 = { stack: [], error: void 0, hasError: false };
                try {
                    const _release = __addDisposableResource(env_4, await lock.acquire(), false);
                    throw new Error('boom');
                }
                catch (e_3) {
                    env_4.error = e_3;
                    env_4.hasError = true;
                }
                finally {
                    __disposeResources(env_4);
                }
            }
            catch {
                // swallow; the `using` handle is disposed as the block unwinds
            }
            // A subsequent acquire must resolve (would hang if the lock leaked).
            const _release2 = __addDisposableResource(env_3, await lock.acquire(), false);
            expect(true).toBe(true);
        }
        catch (e_4) {
            env_3.error = e_4;
            env_3.hasError = true;
        }
        finally {
            __disposeResources(env_3);
        }
    });
});
//# sourceMappingURL=async-lock.test.js.map