Skip to content

Commit 289273c

Browse files
committed
fix: handle idle timeout test data race
1 parent 243ffee commit 289273c

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

packages/pg-pool/test/idle-timeout.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@ describe('idle timeout', () => {
2525
it(
2626
'times out and removes clients when others are also removed',
2727
co.wrap(function* () {
28-
let currentClient = 1
2928
const pool = new Pool({ idleTimeoutMillis: 10 })
3029
const clientA = yield pool.connect()
3130
const clientB = yield pool.connect()
32-
clientA.release()
33-
clientB.release(new Error())
31+
clientA.release() // this will put clientA in the idle pool
32+
clientB.release(new Error()) // an error will cause clientB to be removed immediately
3433

3534
const removal = new Promise((resolve) => {
36-
pool.on('remove', () => {
35+
pool.on('remove', (client) => {
36+
// clientB's stream may take a while to close, so we may get a remove
37+
// event for it
38+
// we only want to handle the remove event for clientA when it times out
39+
// due to being idle
40+
if (client !== clientA) {
41+
return
42+
}
43+
3744
expect(pool.idleCount).to.equal(0)
3845
expect(pool.totalCount).to.equal(0)
39-
40-
if (currentClient >= 2) {
41-
resolve()
42-
} else {
43-
currentClient++
44-
}
46+
resolve()
4547
})
4648
})
4749

@@ -50,7 +52,7 @@ describe('idle timeout', () => {
5052
try {
5153
yield Promise.race([removal, timeout])
5254
} finally {
53-
yield pool.end()
55+
pool.end()
5456
}
5557
})
5658
)

0 commit comments

Comments
 (0)