Skip to content

Commit 2ca8ce6

Browse files
committed
Add tests for other lock types
1 parent cca20bc commit 2ca8ce6

File tree

5 files changed

+86
-5
lines changed

5 files changed

+86
-5
lines changed

test/src/RedisMultiSemaphore.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('MultiSemaphore', () => {
8787
const semaphore2 = new MultiSemaphore(client, 'key', 3, 1, timeoutOptions)
8888
await semaphore1.acquire()
8989
await semaphore2.acquire()
90-
await delay(100)
90+
await delay(400)
9191
expect(await client.zrange('semaphore:key', 0, -1)).to.have.members([
9292
semaphore1.identifier + '_0',
9393
semaphore1.identifier + '_1',
@@ -100,6 +100,20 @@ describe('MultiSemaphore', () => {
100100
await semaphore2.release()
101101
expect(await client.zcard('semaphore:key')).to.be.eql(0)
102102
})
103+
it('should stop refreshing lock if stopped', async () => {
104+
const semaphore1 = new MultiSemaphore(client, 'key', 3, 2, timeoutOptions)
105+
const semaphore2 = new MultiSemaphore(client, 'key', 3, 1, timeoutOptions)
106+
await semaphore1.acquire()
107+
await semaphore2.acquire()
108+
await semaphore1.stopRefresh()
109+
await delay(400)
110+
expect(await client.zrange('semaphore:key', 0, -1)).to.be.eql([
111+
semaphore2.identifier + '_0'
112+
])
113+
await semaphore2.stopRefresh()
114+
await delay(400)
115+
expect(await client.zcard('semaphore:key')).to.be.eql(0)
116+
})
103117
it('should acquire maximum LIMIT semaphores', async () => {
104118
const s = () =>
105119
new MultiSemaphore(client, 'key', 3, 1, {

test/src/RedisSemaphore.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('Semaphore', () => {
7777
const semaphore2 = new Semaphore(client, 'key', 2, timeoutOptions)
7878
await semaphore1.acquire()
7979
await semaphore2.acquire()
80-
await delay(100)
80+
await delay(400)
8181
expect(await client.zrange('semaphore:key', 0, -1)).to.have.members([
8282
semaphore1.identifier,
8383
semaphore2.identifier
@@ -89,6 +89,20 @@ describe('Semaphore', () => {
8989
await semaphore2.release()
9090
expect(await client.zcard('semaphore:key')).to.be.eql(0)
9191
})
92+
it('should stop refreshing lock if stopped', async () => {
93+
const semaphore1 = new Semaphore(client, 'key', 2, timeoutOptions)
94+
const semaphore2 = new Semaphore(client, 'key', 2, timeoutOptions)
95+
await semaphore1.acquire()
96+
await semaphore2.acquire()
97+
await semaphore1.stopRefresh()
98+
await delay(400)
99+
expect(await client.zrange('semaphore:key', 0, -1)).to.be.eql([
100+
semaphore2.identifier
101+
])
102+
await semaphore2.stopRefresh()
103+
await delay(400)
104+
expect(await client.zcard('semaphore:key')).to.be.eql(0)
105+
})
92106
it('should acquire maximum LIMIT semaphores', async () => {
93107
const s = () =>
94108
new Semaphore(client, 'key', 3, {

test/src/RedlockMultiSemaphore.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ describe('RedlockMultiSemaphore', () => {
151151
)
152152
await semaphore1.acquire()
153153
await semaphore2.acquire()
154-
await delay(100)
154+
await delay(400)
155155
await expectZRangeAllHaveMembers('semaphore:key', [
156156
semaphore1.identifier + '_0',
157157
semaphore1.identifier + '_1',
@@ -162,6 +162,30 @@ describe('RedlockMultiSemaphore', () => {
162162
await semaphore2.release()
163163
await expectZCardAllEql('semaphore:key', 0)
164164
})
165+
it('should stop refreshing lock if stopped', async () => {
166+
const semaphore1 = new RedlockMultiSemaphore(
167+
allClients,
168+
'key',
169+
3,
170+
2,
171+
timeoutOptions
172+
)
173+
const semaphore2 = new RedlockMultiSemaphore(
174+
allClients,
175+
'key',
176+
3,
177+
1,
178+
timeoutOptions
179+
)
180+
await semaphore1.acquire()
181+
await semaphore2.acquire()
182+
semaphore1.stopRefresh()
183+
await delay(400)
184+
await expectZRangeAllEql('semaphore:key', [semaphore2.identifier + '_0'])
185+
semaphore2.stopRefresh()
186+
await delay(400)
187+
await expectZCardAllEql('semaphore:key', 0)
188+
})
165189
it('should acquire maximum LIMIT semaphores', async () => {
166190
const s = () =>
167191
new RedlockMultiSemaphore(allClients, 'key', 3, 1, {

test/src/RedlockMutex.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,18 @@ describe('RedlockMutex', () => {
6363
it('should refresh lock every refreshInterval ms until release', async () => {
6464
const mutex = new RedlockMutex(allClients, 'key', timeoutOptions)
6565
await mutex.acquire()
66-
await delay(100)
66+
await delay(400)
6767
await expectGetAll('mutex:key', mutex.identifier)
6868
await mutex.release()
6969
await expectGetAll('mutex:key', null)
7070
})
71+
it('should stop refreshing if stopped', async () => {
72+
const mutex = new RedlockMutex(allClients, 'key', timeoutOptions)
73+
await mutex.acquire()
74+
mutex.stopRefresh()
75+
await delay(400)
76+
await expectGetAll('mutex:key', null)
77+
})
7178
describe('lost lock case', () => {
7279
beforeEach(() => {
7380
catchUnhandledRejection()

test/src/RedlockSemaphore.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('RedlockSemaphore', () => {
118118
)
119119
await semaphore1.acquire()
120120
await semaphore2.acquire()
121-
await delay(100)
121+
await delay(400)
122122
await expectZRangeAllHaveMembers('semaphore:key', [
123123
semaphore1.identifier,
124124
semaphore2.identifier
@@ -128,6 +128,28 @@ describe('RedlockSemaphore', () => {
128128
await semaphore2.release()
129129
await expectZCardAllEql('semaphore:key', 0)
130130
})
131+
it('should stop refreshing lock if stopped', async () => {
132+
const semaphore1 = new RedlockSemaphore(
133+
allClients,
134+
'key',
135+
2,
136+
timeoutOptions
137+
)
138+
const semaphore2 = new RedlockSemaphore(
139+
allClients,
140+
'key',
141+
2,
142+
timeoutOptions
143+
)
144+
await semaphore1.acquire()
145+
await semaphore2.acquire()
146+
semaphore1.stopRefresh()
147+
await delay(400)
148+
await expectZRangeAllEql('semaphore:key', [semaphore2.identifier])
149+
semaphore2.stopRefresh()
150+
await delay(400)
151+
await expectZCardAllEql('semaphore:key', 0)
152+
})
131153
it('should acquire maximum LIMIT semaphores', async () => {
132154
const s = () =>
133155
new RedlockSemaphore(allClients, 'key', 3, {

0 commit comments

Comments
 (0)