Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
chore: We can now upgrade to mocha@5 🌮
Browse files Browse the repository at this point in the history
## Symptoms

Upgrading to `mocha@5` now causes my tests to hang without any context. [See this Travis build as an example](https://travis-ci.org/mongodb-js/index-model/builds/451375910)

## Resolution

We were *not* explicitly closing driver connections after tests that needed it finished which kept the driver's underlying sockets to remain open. These `after(client.close)` blocks have been added in this PR.

## Root Cause

`mocha@4` introduced a reenforcement for better testing and quality with the removal of mocha killing its process when it *thinks* the tests complete. [See release notes](https://boneskull.com/mocha-v4-nears-release/#mochawontforceexit)

For us, this is a huge win. `mocha` has always included its own process management which allowed us to play fast and loose releasing fds/sockets/timeouts/etc properly.

## Diagnosis

Googling lead me to [this mocha issue](mochajs/mocha#3044) where the maintainer provides several solutions to debug this and make your tests even *more* correct. Here's what worked for me:

```bash
npm i -g wtfnode;
wtfnode ./node_modules/.bin/_mocha test/data-service.test.js;
# Wait for hang to appear
# Hit ctrl+c which produces the output below
```

```
  34 passing (1s)

^C[WTF Node?] open handles:
- File descriptors: (note: stdio always exists)
  - fd 1 (tty) (stdio)
  - fd 2 (tty) (stdio)
- Sockets:
  - 127.0.0.1:57887 -> 127.0.0.1:27018
  - 127.0.0.1:57888 -> 127.0.0.1:27018
  - 127.0.0.1:57890 -> 127.0.0.1:27018
  - 127.0.0.1:57891 -> 127.0.0.1:27018
- Timers:
  - (5000 ~ 5 s) (anonymous) @ /Users/lucas/data-service/node_modules/mongodb-core/lib/topologies/server.js:373
```
  • Loading branch information
imlucas committed Jan 16, 2019
1 parent 41de0d4 commit 6a5a486
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 69 deletions.
103 changes: 38 additions & 65 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"mongodb-js"
],
"scripts": {
"pretest": "mongodb-runner start --port=27018",
"test": "mocha",
"posttest": "mongodb-runner stop --port 27018",
"pretest": "wtfnode ./node_modules/.bin/mongodb-runner start --port=27018",
"test": "wtfnode ./node_modules/.bin/_mocha",
"posttest": "wtfnode ./node_modules/.bin/mongodb-runner stop --port 27018",
"check": "mongodb-js-precommit"
},
"precommit": [
Expand Down Expand Up @@ -52,7 +52,7 @@
"devDependencies": {
"chai": "^3.4.1",
"eslint-config-mongodb-js": "^3.0.1",
"mocha": "^3.1.2",
"mocha": "^5.2.0",
"mock-require": "^2.0.1",
"mongodb-js-precommit": "^1.1.0",
"mongodb-runner": "^4.6.0",
Expand Down
3 changes: 3 additions & 0 deletions test/data-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ describe('DataService', function() {
before(function(done) {
service.connect(done);
});
after(function(done) {
service.disconnect(done);
});

describe('#deleteOne', function() {
it('deletes the document from the collection', function(done) {
Expand Down
3 changes: 3 additions & 0 deletions test/instance-detail-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ describe('mongodb-data-service#instance', function() {
describe('local', function() {
let client;
let db;
after(function(done) {
client.close(done);
});
it('should connect to `localhost:27018`', function(done) {
const model = Connection.from('mongodb://localhost:27018/data-service');
connect(
Expand Down
4 changes: 4 additions & 0 deletions test/native-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe('NativeClient', function() {
client.connect(callback);
});

after(function(done) {
client.disconnect(done);
});

describe('#connect', function() {
context('when mocking connection-model', function() {
/*
Expand Down

0 comments on commit 6a5a486

Please sign in to comment.