UNPKG

@x5e/gink

Version:

an eventually consistent database

33 lines 1.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("../implementation/utils"); const PromiseChainLock_1 = require("../implementation/PromiseChainLock"); function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } it("test lock/unlock ", async () => { const promiseChainLock = new PromiseChainLock_1.PromiseChainLock(); let countLocks = 0; const messages = []; async function doSomething(msg) { const unlocker = await promiseChainLock.acquireLock(); (0, utils_1.ensure)(countLocks === 0); countLocks += 1; messages.push(msg); await sleep(100); (0, utils_1.ensure)(countLocks === 1); countLocks -= 1; unlocker(null); } doSomething("first"); doSomething("second"); doSomething("third"); messages.push("zeroth"); await promiseChainLock.acquireLock(); (0, utils_1.ensure)(messages.length === 4); (0, utils_1.ensure)(messages[0] === "zeroth"); (0, utils_1.ensure)(messages[1] === "first"); (0, utils_1.ensure)(messages[2] === "second"); (0, utils_1.ensure)(messages[3] === "third"); }); //# sourceMappingURL=PromiseChainLock.test.js.map