Skip to content

Commit 497b7e1

Browse files
authored
fix: flaky tests (#84)
* fix: give mongodb more time during testing * pin mongodb at 8.0.9 * increase sleep * remove sleep * close client after clean * add 1 second delay again to test * remove the second again * serialize tests * add 1 second sleep to verify * remove the 1 second sleep again
1 parent 67f01f3 commit 497b7e1

3 files changed

Lines changed: 19 additions & 26 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ jobs:
2222
node-version: ${{ matrix.node-version }}
2323
- name: start mongodb
2424
run: |
25-
docker pull mongo:8
2625
npm run mongodb
2726
- name: Install
2827
run: |

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
"scripts": {
77
"lint": "eslint",
88
"lint:fix": "eslint --fix",
9-
"unit": "node --test --test-timeout=180000 test/*.js",
9+
"unit": "node --test --test-timeout=180000 --test-concurrency=1 test/*.js",
1010
"test": "npm run lint && npm run unit",
1111
"coverage": "c8 --reporter=lcov npm run unit",
1212
"check-coverage": "c8 check-coverage --lines 90 --functions 98 --branches 85",
1313
"test:ci": "npm run lint && npm run coverage",
1414
"license-checker": "license-checker --production --onlyAllow='MIT;ISC;BSD-3-Clause;BSD-2-Clause;0BSD;Apache-2.0;Apache*'",
1515
"release": "read -p 'GITHUB_TOKEN: ' GITHUB_TOKEN && export GITHUB_TOKEN=$GITHUB_TOKEN && release-it --disable-metrics",
16-
"mongodb": "docker run -d --rm --name mongodb -p 27017:27017 mongo:8"
16+
"mongodb": "docker run -d --rm --name mongodb -p 27017:27017 mongo:8.0.9"
1717
},
1818
"release-it": {
1919
"github": {

test/abs.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,21 @@ const mqemitterMongo = require('mqemitter-mongodb')
88
const dbname = 'aedes-test'
99
const mongourl = `mongodb://127.0.0.1/${dbname}`
1010

11-
async function cleanDB (collections) {
11+
async function cleanDB () {
12+
const mongoClient = new MongoClient(mongourl, { w: 1 })
13+
const db = mongoClient.db(dbname)
14+
await db.admin().ping()
15+
16+
const collections = [
17+
db.collection('subscriptions'),
18+
db.collection('retained'),
19+
db.collection('will'),
20+
db.collection('outgoing'),
21+
db.collection('incoming')
22+
]
23+
1224
await Promise.all(collections.map((c) => c.deleteMany({})))
25+
await mongoClient.close()
1326
}
1427

1528
function makeBuildEmitter (dbopts) {
@@ -19,9 +32,9 @@ function makeBuildEmitter (dbopts) {
1932
}
2033
}
2134

22-
function makePersistence (dbopts, collections) {
35+
function makePersistence (dbopts) {
2336
return async function build () {
24-
await cleanDB(collections)
37+
await cleanDB()
2538
const instance = persistence(dbopts)
2639
// make intance.destroy close the broker as well
2740
const oldDestroy = instance.destroy.bind(instance)
@@ -35,34 +48,15 @@ function makePersistence (dbopts, collections) {
3548
}
3649
// Testing starts here.
3750
async function doTest () {
38-
const mongoClient = new MongoClient(mongourl, { w: 1 })
39-
const db = mongoClient.db(dbname)
40-
test('Can connect to MongoDB', async (t) => {
41-
await db.admin().ping()
42-
t.assert.ok(true, 'Can connect to MongoDB')
43-
})
44-
45-
const collections = [
46-
db.collection('subscriptions'),
47-
db.collection('retained'),
48-
db.collection('will'),
49-
db.collection('outgoing'),
50-
db.collection('incoming')
51-
]
52-
5351
const defaultDBopts = {
5452
url: mongourl
5553
}
5654
// run all tests from aedes-abstract-persistence
5755
abs({
5856
test,
5957
buildEmitter: makeBuildEmitter(defaultDBopts),
60-
persistence: makePersistence(defaultDBopts, collections),
58+
persistence: makePersistence(defaultDBopts),
6159
waitForReady: true,
6260
})
63-
64-
test.after(() => {
65-
mongoClient.close()
66-
})
6761
}
6862
doTest()

0 commit comments

Comments
 (0)