Skip to content

Commit 6b6cf9f

Browse files
committed
increase coverage
1 parent cb6c190 commit 6b6cf9f

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.vscode
3-
dist
3+
dist
4+
coverage

src/index.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,24 @@ export async function tryLock(
141141
const release: ReleaseFunc = async function () {
142142
if (released) return;
143143

144-
if (!isRedisClient(redis)) {
145-
await redis.eval(releaseScript, {
146-
keys: [lockKey, REDIS_RELEASES_CHANNEL],
147-
arguments: [lockValue],
148-
});
144+
const evalParams = {
145+
keys: [lockKey, REDIS_RELEASES_CHANNEL],
146+
arguments: [lockValue],
147+
};
149148

149+
if (!isRedisClient(redis)) {
150+
await redis.eval(releaseScript, evalParams);
150151
released = true;
151152
return;
152153
}
153154

154155
// If it's redis client, cache the script and use its SHA
155156
if (scriptHash == null) scriptHash = await redis.scriptLoad(releaseScript);
156157

157-
await redis
158-
.evalSha(scriptHash, {
159-
keys: [lockKey, REDIS_RELEASES_CHANNEL],
160-
arguments: [lockValue],
161-
})
162-
.catch((err: Error) => {
163-
if (err.message.includes('NOSCRIPT')) scriptHash = null; // Signal script flushed
164-
else throw err;
165-
});
158+
await redis.evalSha(scriptHash, evalParams).catch((err: Error) => {
159+
if (err.message.includes('NOSCRIPT')) scriptHash = null; // Signal script flushed
160+
else throw err;
161+
});
166162

167163
if (scriptHash == null) await release(); // If script flushed, try again
168164
released = true;

unit.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ describe('Lock tests', () => {
3939
expect(release.fencingToken).toEqual(-1);
4040
});
4141

42+
test('release works without script cache', async () => {
43+
const scriptLoad = redis.scriptLoad;
44+
// @ts-ignore
45+
redis.scriptLoad = null;
46+
47+
let [hasLock, release] = await tryLock(redis, lockName);
48+
expect(hasLock).toEqual(true);
49+
50+
await release();
51+
52+
[hasLock, release] = await tryLock(redis, lockName);
53+
expect(hasLock).toEqual(true);
54+
55+
redis.scriptLoad = scriptLoad;
56+
});
57+
4258
test('it issues monotonic fencing tokens', async () => {
4359
let lastToken: number | null = null;
4460

0 commit comments

Comments
 (0)