forked from binded/advisory-lock
-
Notifications
You must be signed in to change notification settings - Fork 2
/
issue-1.test.js
35 lines (33 loc) · 1.1 KB
/
issue-1.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import test from 'tape'
import pg from 'pg'
import advisoryLock from '../src'
import { conString, timeout } from './common'
// Returns the number of active connections to the database
const getActiveConnections = () => new Promise((resolve, reject) => {
const sql = 'SELECT count(*) FROM pg_stat_activity'
const client = new pg.Client(conString)
client.connect((connectError) => {
if (connectError) return reject(connectError)
client.query(sql, (queryError, result) => {
if (queryError) return reject(queryError)
resolve(Number(result.rows[0].count))
client.end()
})
})
})
test('withLock releases connection after unlocking', (t) => {
getActiveConnections().then((startConnectionCount) => {
for (let i = 0; i < 25; i++) {
const createMutex = advisoryLock(conString)
createMutex('test-withlock-release').withLock().catch(t.fail).then(() => {
createMutex.client.end()
})
}
timeout(500).then(() => {
getActiveConnections().then((connectionCount) => {
t.equal(connectionCount, startConnectionCount)
t.end()
})
})
})
})