Skip to content

Commit 8efe1ef

Browse files
committed
test cleanup
1 parent 3926158 commit 8efe1ef

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

test/unit/assorted/server_discovery_and_monitoring.test.ts

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,31 @@ import { type TopologyDescription } from 'mongodb-legacy';
33
import * as sinon from 'sinon';
44

55
import {
6-
type MongoClient,
6+
MongoClient,
77
ObjectId,
88
Server,
99
ServerDescription,
1010
Topology,
11-
TOPOLOGY_DESCRIPTION_CHANGED,
1211
type TopologyDescriptionChangedEvent
1312
} from '../../mongodb';
1413

15-
const SDAM_EVENTS = [
16-
// Topology events
17-
TOPOLOGY_DESCRIPTION_CHANGED
18-
];
19-
2014
describe('Server Discovery and Monitoring', function () {
2115
let serverConnect: sinon.SinonStub;
2216
let topologySelectServer: sinon.SinonStub;
2317
let client: MongoClient;
24-
let events: TopologyDescriptionChangedEvent[] = [];
18+
let events: TopologyDescriptionChangedEvent[];
2519

2620
function getNewDescription() {
27-
const [topologyDescriptionChanged] = events.filter(
28-
x => x.name === 'topologyDescriptionChanged'
29-
);
30-
events = [];
21+
const topologyDescriptionChanged = events[events.length - 1];
3122
return topologyDescriptionChanged.newDescription;
3223
}
3324

34-
before(async function () {
25+
beforeEach(async function () {
3526
serverConnect = sinon.stub(Server.prototype, 'connect').callsFake(function () {
3627
this.s.state = 'connected';
3728
this.emit('connect');
3829
});
30+
3931
topologySelectServer = sinon
4032
.stub(Topology.prototype, 'selectServer')
4133
.callsFake(async function (_selector, _options) {
@@ -44,57 +36,65 @@ describe('Server Discovery and Monitoring', function () {
4436
const fakeServer = { s: { state: 'connected' }, removeListener: () => true };
4537
return fakeServer;
4638
});
47-
const events = [];
39+
40+
events = [];
41+
client = new MongoClient('mongodb://a/?replicaSet=rs');
4842
client.on('topologyDescriptionChanged', event => events.push(event));
4943
await client.connect();
44+
45+
// Start with a as primary
46+
client.topology.serverUpdateHandler(
47+
new ServerDescription('a:27017', {
48+
ok: 1,
49+
helloOk: true,
50+
isWritablePrimary: true,
51+
hosts: ['a:27017', 'b:27017'],
52+
setName: 'rs',
53+
setVersion: 1,
54+
electionId: ObjectId.createFromHexString('000000000000000000000001'),
55+
minWireVersion: 0,
56+
maxWireVersion: 21
57+
})
58+
);
59+
60+
// b is elected as primary, a gets marked stale
61+
client.topology.serverUpdateHandler(
62+
new ServerDescription('b:27017', {
63+
ok: 1,
64+
helloOk: true,
65+
isWritablePrimary: true,
66+
hosts: ['a:27017', 'b:27017'],
67+
setName: 'rs',
68+
setVersion: 2,
69+
electionId: ObjectId.createFromHexString('000000000000000000000001'),
70+
minWireVersion: 0,
71+
maxWireVersion: 21
72+
})
73+
);
5074
});
5175

52-
after(function () {
76+
afterEach(async function () {
5377
serverConnect.restore();
78+
await client.close().catch(() => null);
5479
});
5580

56-
describe('when a newer primary is detected', function () {
57-
it('steps down original primary to unknown server description with appropriate error message', async function () {
58-
let newDescription: TopologyDescription;
59-
// Start with a as primary
60-
client.topology.serverUpdateHandler(
61-
new ServerDescription('a:27017', {
62-
ok: 1,
63-
helloOk: true,
64-
isWritablePrimary: true,
65-
hosts: ['a:27017', 'b:27017'],
66-
setName: 'rs',
67-
setVersion: 1,
68-
electionId: ObjectId.createFromHexString('000000000000000000000001'),
69-
minWireVersion: 0,
70-
maxWireVersion: 21
71-
})
72-
);
73-
74-
newDescription = getNewDescription();
75-
76-
expect(newDescription.type).to.equal('ReplicaSetWithPrimary');
77-
78-
// b is elected as primary, a gets marked stale
79-
client.topology.serverUpdateHandler(
80-
new ServerDescription('b:27017', {
81-
ok: 1,
82-
helloOk: true,
83-
isWritablePrimary: true,
84-
hosts: ['a:27017', 'b:27017'],
85-
setName: 'rs',
86-
setVersion: 2,
87-
electionId: ObjectId.createFromHexString('000000000000000000000001'),
88-
minWireVersion: 0,
89-
maxWireVersion: 21
90-
})
91-
);
81+
let newDescription: TopologyDescription;
9282

83+
describe('when a newer primary is detected', function () {
84+
it('steps down original primary to unknown server description with appropriate error message', function () {
9385
newDescription = getNewDescription();
9486

95-
let aOutcome = newDescription.servers.get('a:27017');
87+
const aOutcome = newDescription.servers.get('a:27017');
88+
const bOutcome = newDescription.servers.get('b:27017');
89+
expect(aOutcome.type).to.equal('Unknown');
9690
expect(aOutcome.error).to.match(/primary marked stale due to discovery of newer primary/);
9791

92+
expect(bOutcome.type).to.equal('RSPrimary');
93+
});
94+
});
95+
96+
describe('when a stale primary still reports itself as primary', function () {
97+
it('gets marked as unknown with an error message with the new and old replicaSetVersion and electionId', function () {
9898
// a still incorrectly reports as primary
9999
client.topology.serverUpdateHandler(
100100
new ServerDescription('a:27017', {
@@ -112,7 +112,7 @@ describe('Server Discovery and Monitoring', function () {
112112

113113
newDescription = getNewDescription();
114114

115-
aOutcome = newDescription.servers.get('a:27017');
115+
const aOutcome = newDescription.servers.get('a:27017');
116116

117117
expect(aOutcome.type).to.equal('Unknown');
118118
expect(aOutcome.error).to.match(

0 commit comments

Comments
 (0)