Skip to content

Commit

Permalink
chore: Set up unit testing (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
meyfa authored Jul 7, 2024
1 parent 3ba6f97 commit 2a6bd6f
Show file tree
Hide file tree
Showing 6 changed files with 703 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ jobs:
- run: npm ci
- run: npm run build
- run: npm run lint

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run build
- run: npm run test
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"scripts": {
"build": "node -e \"fs.rmSync('./dist',{force:true,recursive:true})\" && tsc",
"lint": "tsc --noEmit -p tsconfig.lint.json && eslint --ignore-path .gitignore ."
"lint": "tsc --noEmit -p tsconfig.lint.json && eslint --ignore-path .gitignore .",
"test": "mocha --require tsx --recursive \"test/**/*.ts\""
},
"dependencies": {
"pino-pretty": "11.2.1",
Expand Down
40 changes: 40 additions & 0 deletions backend/test/util/mutex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Mutex } from '../../src/util/mutex.js'
import assert from 'node:assert'

describe('util/mutex.ts', () => {
describe('Mutex', () => {
it('serializes access to a critical section', async () => {
let locked = false
const mutex = new Mutex()
const unlock = await mutex.lock()
locked = true
setTimeout(() => {
unlock()
locked = false
}, 100)
const unlock2 = await mutex.lock()
assert.strictEqual(locked, false)
unlock2()
})

it('resolves in the order of lock calls', async () => {
const mutex = new Mutex()
let order = ''
const promises: Array<Promise<any>> = []
promises.push(mutex.lock().then((unlock) => {
order += '1'
setTimeout(unlock, 50)
}))
promises.push(mutex.lock().then((unlock) => {
order += '2'
setTimeout(unlock, 50)
}))
promises.push(mutex.lock().then((unlock) => {
order += '3'
setTimeout(unlock, 50)
}))
await Promise.all(promises)
assert.strictEqual(order, '123')
})
})
})
Loading

0 comments on commit 2a6bd6f

Please sign in to comment.