-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fc9c02
commit 2a3d9b8
Showing
5 changed files
with
141 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { expect, test } from "bun:test"; | ||
import { Redis } from "@upstash/redis"; | ||
import { LockManager } from "./lock-manager"; | ||
|
||
function getUniqueLockId() { | ||
return `lock-test-${Math.random().toString(36).substr(2, 9)}`; | ||
} | ||
|
||
test("lock created, extended, and released with defaults", async () => { | ||
const lm = new LockManager({ | ||
redis: Redis.fromEnv(), | ||
}); | ||
const id = getUniqueLockId(); | ||
const lock = await lm.acquire({ id }); | ||
|
||
expect(lock.id).toBe(id); | ||
expect(lock.status).toBe("ACQUIRED"); | ||
const extended = await lock.extend(10000); | ||
expect(extended).toBe(true); | ||
const released = await lock.release(); | ||
expect(released).toBe(true); | ||
expect(lock.status).toBe("RELEASED"); | ||
}); | ||
|
||
test("lock created, extended, and released with values", async () => { | ||
const lm = new LockManager({ | ||
redis: Redis.fromEnv(), | ||
}); | ||
|
||
const id = getUniqueLockId(); | ||
const lock = await lm.acquire({ | ||
id, | ||
lease: 5000, | ||
retry: { | ||
attempts: 1, | ||
delay: 100, | ||
}, | ||
}); | ||
|
||
expect(lock.id).toBe(id); | ||
expect(lock.status).toBe("ACQUIRED"); | ||
const extended = await lock.extend(10000); | ||
expect(extended).toBe(true); | ||
const released = await lock.release(); | ||
expect(released).toBe(true); | ||
expect(lock.status).toBe("RELEASED"); | ||
}); | ||
|
||
test("lock acquisition fails", async () => { | ||
const lm = new LockManager({ | ||
redis: Redis.fromEnv(), | ||
}); | ||
|
||
const id = getUniqueLockId(); | ||
const lockSuccess = await lm.acquire({ | ||
id, | ||
lease: 5000, | ||
retry: { | ||
attempts: 1, | ||
delay: 100, | ||
}, | ||
}); | ||
|
||
expect(lockSuccess.status).toBe("ACQUIRED"); | ||
|
||
// Since the lock was already acquired, this should fail | ||
const lockFail = await lm.acquire({ | ||
id, | ||
retry: { | ||
attempts: 1, | ||
delay: 100, | ||
}, | ||
}); | ||
|
||
expect(lockFail.status).toBe("FAILED"); | ||
}); | ||
|
||
test("lock lease times out", async () => { | ||
const lm = new LockManager({ | ||
redis: Redis.fromEnv(), | ||
}); | ||
|
||
const lock = await lm.acquire({ | ||
id: "lock-test-3", | ||
lease: 100, | ||
retry: { | ||
attempts: 1, | ||
delay: 100, | ||
}, | ||
}); | ||
|
||
// Wait for the lock to expire | ||
setTimeout(async () => { | ||
expect(lock.status).toBe("RELEASED"); | ||
}, 200); | ||
}); |
This file contains 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
This file contains 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