Skip to content

Commit c94a5ca

Browse files
committed
test: fix flakky test
1 parent 5f0ae58 commit c94a5ca

2 files changed

Lines changed: 197 additions & 4 deletions

File tree

packages/verrou/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<p align="center">
2+
<br/>
3+
<a href="https://verrou.dev/">Verrou 🔐</a> is a locking library for Node.js applications
4+
<br/>
5+
</p>
6+
7+
## Features
8+
9+
- 🔒 Easy usage
10+
- 🔄 Multiple drivers (Redis, Postgres, MySQL, Sqlite, In-Memory and others)
11+
- 🔑 Customizable named locks
12+
- 🌐 Consistent API across all drivers
13+
- 🧪 Easy testing by switching to an in-memory driver
14+
- 🔨 Easily extensible with your own drivers
15+
16+
See documentation at [verrou.dev](https://verrou.dev/docs/introduction)
17+
18+
## Why Verrou ?
19+
20+
Main advantage of Verrou is that it provides a consistent API across all drivers. This means that you can switch from one driver to another without having to change your code. It also means you can switch to an in-memory in your test environment, making tests faster and easier to setup (no infrastructure or anything fancy to setup).
21+
22+
Having a consistent API also means that you don't have to learn a new API when switching from one driver to another. Today, in the node ecosystem, we have different npm packages to manage locks, but they all have differents APIs and behaviors.
23+
24+
But having a consistent API doesn't mean having a less powerful API. Verrou provides every features you would expect from a locking library, and even more.
25+
26+
## Basic usage
27+
28+
```ts
29+
import { Verrou } from '@verrou/core'
30+
import { redisStore } from '@verrou/core/drivers/redis'
31+
import { memoryStore } from '@verrou/core/drivers/memory'
32+
33+
const verrou = new Verrou({
34+
default: 'redis',
35+
drivers: {
36+
redis: { driver: redisStore() },
37+
memory: { driver: memoryStore() }
38+
}
39+
})
40+
41+
// Lock a resource and run a function
42+
await verrou.createLock('my-resource').run(async () => {
43+
await doSomething()
44+
}) // Lock is automatically released
45+
```
46+
47+
## Sponsor
48+
49+
If you like this project, [please consider supporting it by sponsoring it](https://github.com/sponsors/Julien-R44/). It will help a lot to maintain and improve it. Thanks a lot !

packages/verrou/src/test_suite.ts

Lines changed: 148 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="@japa/assert" />
22

3-
import type { Group } from '@japa/runner/core'
3+
import { type Group } from '@japa/runner/core'
44
import type { test as JapaTest } from '@japa/runner'
55
import { setTimeout as sleep } from 'node:timers/promises'
66

@@ -136,6 +136,7 @@ export function registerStoreTestSuite<T extends { new (options: any): LockStore
136136
lock.run(async () => {
137137
await sleep(500)
138138
flag = true
139+
console.log('runned')
139140
})
140141

141142
assert.isFalse(flag)
@@ -154,17 +155,160 @@ export function registerStoreTestSuite<T extends { new (options: any): LockStore
154155
const lock = provider.createLock('foo')
155156
let flag = false
156157

157-
lock
158+
await lock
159+
.run(async () => {
160+
flag = true
161+
throw new Error('hello world')
162+
})
163+
.catch(() => undefined)
164+
165+
assert.isTrue(flag)
166+
await lock.run(async () => undefined)
167+
}).pin()
168+
169+
test('exceptions during run do not leave mutex in locked state', async ({ assert }) => {
170+
const provider = new LockFactory(new store(config))
171+
const lock = provider.createLock('foo')
172+
let flag = false
173+
174+
await lock
158175
.run(async () => {
159176
flag = true
160177
throw new Error('hello world')
161178
})
162179
.catch(() => undefined)
163180

164-
assert.isFalse(flag)
165181
await lock.run(async () => undefined)
166182
assert.isTrue(flag)
167-
})
183+
}).pin()
184+
185+
test('exceptions during run do not leave mutex in locked state', async ({ assert }) => {
186+
const provider = new LockFactory(new store(config))
187+
const lock = provider.createLock('foo')
188+
let flag = false
189+
190+
await lock
191+
.run(async () => {
192+
flag = true
193+
throw new Error('hello world')
194+
})
195+
.catch(() => undefined)
196+
197+
await lock.run(async () => undefined)
198+
assert.isTrue(flag)
199+
}).pin()
200+
201+
test('exceptions during run do not leave mutex in locked state', async ({ assert }) => {
202+
const provider = new LockFactory(new store(config))
203+
const lock = provider.createLock('foo')
204+
let flag = false
205+
206+
await lock
207+
.run(async () => {
208+
flag = true
209+
throw new Error('hello world')
210+
})
211+
.catch(() => undefined)
212+
213+
await lock.run(async () => undefined)
214+
assert.isTrue(flag)
215+
}).pin()
216+
217+
test('exceptions during run do not leave mutex in locked state', async ({ assert }) => {
218+
const provider = new LockFactory(new store(config))
219+
const lock = provider.createLock('foo')
220+
let flag = false
221+
222+
await lock
223+
.run(async () => {
224+
flag = true
225+
throw new Error('hello world')
226+
})
227+
.catch(() => undefined)
228+
229+
await lock.run(async () => undefined)
230+
assert.isTrue(flag)
231+
}).pin()
232+
233+
test('exceptions during run do not leave mutex in locked state', async ({ assert }) => {
234+
const provider = new LockFactory(new store(config))
235+
const lock = provider.createLock('foo')
236+
let flag = false
237+
238+
await lock
239+
.run(async () => {
240+
flag = true
241+
throw new Error('hello world')
242+
})
243+
.catch(() => undefined)
244+
245+
await lock.run(async () => undefined)
246+
assert.isTrue(flag)
247+
}).pin()
248+
249+
test('exceptions during run do not leave mutex in locked state', async ({ assert }) => {
250+
const provider = new LockFactory(new store(config))
251+
const lock = provider.createLock('foo')
252+
let flag = false
253+
254+
await lock
255+
.run(async () => {
256+
flag = true
257+
throw new Error('hello world')
258+
})
259+
.catch(() => undefined)
260+
261+
await lock.run(async () => undefined)
262+
assert.isTrue(flag)
263+
}).pin()
264+
265+
test('exceptions during run do not leave mutex in locked state', async ({ assert }) => {
266+
const provider = new LockFactory(new store(config))
267+
const lock = provider.createLock('foo')
268+
let flag = false
269+
270+
await lock
271+
.run(async () => {
272+
flag = true
273+
throw new Error('hello world')
274+
})
275+
.catch(() => undefined)
276+
277+
await lock.run(async () => undefined)
278+
assert.isTrue(flag)
279+
}).pin()
280+
281+
test('exceptions during run do not leave mutex in locked state', async ({ assert }) => {
282+
const provider = new LockFactory(new store(config))
283+
const lock = provider.createLock('foo')
284+
let flag = false
285+
286+
await lock
287+
.run(async () => {
288+
flag = true
289+
throw new Error('hello world')
290+
})
291+
.catch(() => undefined)
292+
293+
await lock.run(async () => undefined)
294+
assert.isTrue(flag)
295+
}).pin()
296+
297+
test('exceptions during run do not leave mutex in locked state', async ({ assert }) => {
298+
const provider = new LockFactory(new store(config))
299+
const lock = provider.createLock('foo')
300+
let flag = false
301+
302+
await lock
303+
.run(async () => {
304+
flag = true
305+
throw new Error('hello world')
306+
})
307+
.catch(() => undefined)
308+
309+
await lock.run(async () => undefined)
310+
assert.isTrue(flag)
311+
}).pin()
168312

169313
test('multiple calls to release behave as expected', async ({ assert }) => {
170314
let v = 0

0 commit comments

Comments
 (0)