Skip to content

Instantly share code, notes, and snippets.

@j05u3
Created February 25, 2023 23:14
Show Gist options
  • Save j05u3/6d9d3d6266c95b0b4350e29a2ab29ca5 to your computer and use it in GitHub Desktop.
Save j05u3/6d9d3d6266c95b0b4350e29a2ab29ca5 to your computer and use it in GitHub Desktop.
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