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.
fix(secondaries): fixes connection with secondary readPreference (#258)
Ensures that when readPreference is `secondary`, the `connect` event is not triggered until connection has been established with a secondary. Fixes NODE-1089
- Loading branch information
1 parent
ec2b7a6
commit 0060ad7
Showing
2 changed files
with
68 additions
and
4 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,51 @@ | ||
'use strict'; | ||
|
||
const expect = require('chai').expect; | ||
const ReplSet = require('../../../../lib/topologies/replset'); | ||
const ReadPreference = require('../../../../lib/topologies/read_preference'); | ||
const mock = require('../../../mock'); | ||
const ReplSetFixture = require('../common').ReplSetFixture; | ||
|
||
describe('Secondaries (ReplSet)', function() { | ||
let test; | ||
before(() => (test = new ReplSetFixture())); | ||
afterEach(() => mock.cleanup()); | ||
beforeEach(() => test.setup()); | ||
|
||
it('Should not be "connected" with ReadPreference secondary unless secondary is connected', { | ||
metadata: { | ||
requires: { | ||
topology: 'single' | ||
} | ||
}, | ||
|
||
test: function(done) { | ||
const replSet = new ReplSet( | ||
[test.primaryServer.address(), test.firstSecondaryServer.address()], | ||
{ | ||
setName: 'rs', | ||
connectionTimeout: 3000, | ||
socketTimeout: 0, | ||
haInterval: 100, | ||
size: 1 | ||
} | ||
); | ||
|
||
replSet.on('error', done); | ||
|
||
replSet.on('connect', server => { | ||
let err; | ||
try { | ||
expect(server.s.replicaSetState.hasSecondary()).to.equal(true); | ||
} catch (e) { | ||
err = e; | ||
} | ||
|
||
replSet.destroy(); | ||
done(err); | ||
}); | ||
|
||
replSet.connect({ readPreference: new ReadPreference('secondary') }); | ||
} | ||
}); | ||
}); |