This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connection): attempt both ipv6 and ipv4 when no family entered
If the user does not specify an IP family, we will attempt to connect first via IPv6, and fall back to IPv4 if that fails Fixes NODE-1222
- Loading branch information
1 parent
243e942
commit 5cc188b
Showing
2 changed files
with
228 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
'use strict'; | ||
|
||
var bson = require('bson'); | ||
const expect = require('chai').expect; | ||
const mock = require('../../mock'); | ||
const Connection = require('../../../lib/connection/connection'); | ||
|
||
describe('Connection', function() { | ||
const noop = () => {}; | ||
let server; | ||
afterEach(() => mock.cleanup()); | ||
|
||
function testCase(name, options) { | ||
const config = options.config; | ||
|
||
it(name, { | ||
metadata: { requires: { topology: ['single'] } }, | ||
test: function(done) { | ||
const connection = new Connection( | ||
noop, | ||
Object.assign( | ||
{ | ||
bson, | ||
port: server.port | ||
}, | ||
config | ||
) | ||
); | ||
|
||
const cleanup = err => { | ||
connection.destroy(); | ||
done(err); | ||
}; | ||
|
||
const errorHandler = options.error | ||
? err => { | ||
try { | ||
options.error(err); | ||
cleanup(); | ||
} catch (e) { | ||
cleanup(e); | ||
} | ||
} | ||
: cleanup; | ||
|
||
const connectHandler = options.connect | ||
? () => { | ||
try { | ||
options.connect(connection); | ||
cleanup(); | ||
} catch (e) { | ||
cleanup(e); | ||
} | ||
} | ||
: () => { | ||
cleanup(new Error('Expected test to not connect, but it connected successfully')); | ||
}; | ||
|
||
connection.on('error', errorHandler); | ||
connection.on('connect', connectHandler); | ||
connection.connect(); | ||
} | ||
}); | ||
} | ||
|
||
describe('IPv4', function() { | ||
beforeEach(() => mock.createServer(0, '127.0.0.1').then(_server => (server = _server))); | ||
|
||
testCase('should connect with no family', { | ||
config: { host: 'localhost' }, | ||
connect: connection => { | ||
console.log(server); | ||
expect(connection.connection.remoteAddress).to.equal('127.0.0.1'); | ||
expect(connection.connection.remotePort).to.equal(server.port); | ||
expect(connection.connection.remoteFamily).to.equal('IPv4'); | ||
} | ||
}); | ||
|
||
testCase('should connect with family=4', { | ||
config: { host: 'localhost', family: 4 }, | ||
connect: connection => { | ||
expect(connection.connection.remoteAddress).to.equal('127.0.0.1'); | ||
expect(connection.connection.remotePort).to.equal(server.port); | ||
expect(connection.connection.remoteFamily).to.equal('IPv4'); | ||
} | ||
}); | ||
|
||
testCase('should error with family=6', { | ||
config: { host: 'localhost', family: 6 }, | ||
error: err => expect(err).to.be.an.instanceOf(Error) | ||
}); | ||
}); | ||
|
||
describe('IPv6', function() { | ||
beforeEach(() => mock.createServer(0, '::1').then(_server => (server = _server))); | ||
|
||
testCase('should connect with no family', { | ||
config: { host: 'localhost' }, | ||
connect: connection => { | ||
expect(connection.connection.remoteAddress).to.equal('::1'); | ||
expect(connection.connection.remotePort).to.equal(server.port); | ||
expect(connection.connection.remoteFamily).to.equal('IPv6'); | ||
} | ||
}); | ||
|
||
testCase('should error with family=4', { | ||
config: { host: 'localhost', family: 4 }, | ||
error: err => expect(err).to.be.an.instanceOf(Error) | ||
}); | ||
|
||
testCase('should connect with family=6', { | ||
config: { host: 'localhost', family: 6 }, | ||
connect: connection => { | ||
expect(connection.connection.remoteAddress).to.equal('::1'); | ||
expect(connection.connection.remotePort).to.equal(server.port); | ||
expect(connection.connection.remoteFamily).to.equal('IPv6'); | ||
} | ||
}); | ||
}); | ||
}); |