-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: allow connect again after close #2355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0136cd1
835f0aa
3be1b95
865d8b5
854c72f
5043ca3
5763728
c4eb0fa
32c1ce8
dafab24
caf581e
414e091
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,28 +241,35 @@ MongoClient.prototype.close = function(force, callback) { | |
force = false; | ||
} | ||
|
||
const client = this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were using arrow functions everywhere here, so I just updated everything to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. iirc the reason |
||
const topology = this.topology; | ||
|
||
return maybePromise(this, callback, cb => { | ||
const completeClose = err => { | ||
client.emit('close', client); | ||
this.emit('close', this); | ||
|
||
if (!(client.topology instanceof NativeTopology)) { | ||
for (const item of client.s.dbCache) { | ||
item[1].emit('close', client); | ||
if (!(topology instanceof NativeTopology)) { | ||
for (const item of this.s.dbCache) { | ||
item[1].emit('close', this); | ||
} | ||
} | ||
|
||
client.removeAllListeners('close'); | ||
this.removeAllListeners('close'); | ||
|
||
// clear out references to old topology | ||
this.topology = undefined; | ||
this.s.dbCache = new Map(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the old |
||
this.s.sessions = new Set(); | ||
|
||
cb(err); | ||
}; | ||
|
||
if (client.topology == null) { | ||
if (topology == null) { | ||
completeClose(); | ||
return; | ||
} | ||
|
||
client.topology.close(force, err => { | ||
const autoEncrypter = client.topology.s.options.autoEncrypter; | ||
topology.close(force, err => { | ||
const autoEncrypter = topology.s.options.autoEncrypter; | ||
if (!autoEncrypter) { | ||
completeClose(err); | ||
return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,7 @@ | |
"test": "npm run lint && mocha --recursive test/functional test/unit test/core", | ||
"test-nolint": "mocha --recursive test/functional test/unit test/core", | ||
"coverage": "istanbul cover mongodb-test-runner -- -t 60000 test/core test/unit test/functional", | ||
"lint": "eslint lib test", | ||
"lint": "eslint -v && eslint lib test", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR. I just sometimes get weird discrepancies between running |
||
"format": "prettier --print-width 100 --tab-width 2 --single-quote --write 'test/**/*.js' 'lib/**/*.js'", | ||
"bench": "node test/benchmarks/driverBench/", | ||
"generate-evergreen": "node .evergreen/generate_evergreen_tasks.js", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
'use strict'; | ||
const test = require('./shared').assert, | ||
setupDatabase = require('./shared').setupDatabase, | ||
expect = require('chai').expect; | ||
const test = require('./shared').assert; | ||
const setupDatabase = require('./shared').setupDatabase; | ||
const withClient = require('./shared').withClient; | ||
const expect = require('chai').expect; | ||
|
||
describe('Connection', function() { | ||
before(function() { | ||
|
@@ -514,4 +515,27 @@ describe('Connection', function() { | |
}); | ||
} | ||
}); | ||
|
||
it('should be able to connect again after close', function() { | ||
return withClient.call(this, (client, done) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be equivalent to just calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without it I get |
||
expect(client.isConnected()).to.be.true; | ||
const collection = () => client.db('testReconnect').collection('test'); | ||
collection().insertOne({ a: 1 }, (err, result) => { | ||
expect(err).to.not.exist; | ||
expect(result).to.exist; | ||
client.close(err => { | ||
expect(err).to.not.exist; | ||
client.connect(err => { | ||
expect(err).to.not.exist; | ||
collection().insertOne({ b: 2 }, (err, result) => { | ||
expect(err).to.not.exist; | ||
expect(result).to.exist; | ||
expect(client.topology.isDestroyed()).to.be.false; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I upgraded
nvm
so we can use the newish--no-progress
option.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh very nice 👏