Created
February 25, 2023 23:14
-
-
Save j05u3/6d9d3d6266c95b0b4350e29a2ab29ca5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect } from "chai"; | |
import { ONE_MINUTE_IN_MILLIS } from "../util/time-constants"; | |
import { ExternalResourceLock } from "./external-resource-lock"; | |
describe("External Resource Lock", () => { | |
it("should work", async () => { | |
const g = new ExternalResourceLock("externalDatabases", "database01"); | |
await g.deleteDoc(); | |
await g.acquireLock(); | |
await g.releaseLock(); | |
// try to get the lock in parallel (should fail, only one can get the lock) | |
const pr = []; | |
for (let i = 0; i < 10; i++) { | |
pr.push(g.acquireLock()); | |
} | |
const r = await Promise.all(pr); | |
const locks = r.filter(v => v !== null); | |
expect(locks.length).to.equal(1); | |
expect(locks[0]?.isLocked).to.equal(true); | |
const nv = await g.isLocked(); | |
expect(nv).to.equal(true); | |
// release the lock | |
await g.releaseLock(); | |
const currentStatus = await g.isLocked(); | |
expect(currentStatus).to.equal(false); | |
}).timeout(4 * ONE_MINUTE_IN_MILLIS); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment