Skip to content

Commit 92c379a

Browse files
committed
fix unit
1 parent 94a37d0 commit 92c379a

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

test/unit/assorted/server_discovery_and_monitoring.spec.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,14 @@ describe('Server Discovery and Monitoring (spec)', function () {
215215
.callsFake(async function (_selector, _options) {
216216
topologySelectServers.restore();
217217

218-
const fakeServer = { s: { state: 'connected' }, removeListener: () => true };
218+
const fakeServer = {
219+
s: { state: 'connected' },
220+
removeListener: () => true,
221+
pool: {
222+
checkOut: async () => ({}),
223+
checkIn: () => undefined
224+
}
225+
};
219226
return fakeServer;
220227
});
221228
});

test/unit/assorted/server_discovery_and_monitoring.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ describe('Server Discovery and Monitoring', function () {
3131
.callsFake(async function (_selector, _options) {
3232
topologySelectServer.restore();
3333

34-
const fakeServer = { s: { state: 'connected' }, removeListener: () => true };
34+
const fakeServer = {
35+
s: { state: 'connected' },
36+
removeListener: () => true,
37+
pool: {
38+
checkOut: async () => ({}),
39+
checkIn: () => undefined
40+
}
41+
};
3542
return fakeServer;
3643
});
3744

test/unit/assorted/server_selection_spec_helper.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,14 @@ export async function executeServerSelectionTest(testDefinition) {
106106
.callsFake(async function () {
107107
topologySelectServers.restore();
108108

109-
const fakeServer = { s: { state: 'connected' }, removeListener: () => {} };
109+
const fakeServer = {
110+
s: { state: 'connected' },
111+
removeListener: () => true,
112+
pool: {
113+
checkOut: async () => ({}),
114+
checkIn: () => undefined
115+
}
116+
};
110117
return fakeServer;
111118
});
112119

test/unit/sdam/topology.test.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { Topology } from '../../../src/sdam/topology';
2525
import { TopologyDescription } from '../../../src/sdam/topology_description';
2626
import { TimeoutContext } from '../../../src/timeout';
2727
import { isHello, ns } from '../../../src/utils';
28+
import { ConnectionPool } from '../../mongodb';
2829
import * as mock from '../../tools/mongodb-mock/index';
2930
import { topologyWithPlaceholderClient } from '../../tools/utils';
3031

@@ -444,31 +445,23 @@ describe('Topology (unit)', function () {
444445

445446
describe('selectServer()', function () {
446447
it('should schedule monitoring if no suitable server is found', async function () {
447-
const topology = topologyWithPlaceholderClient('someserver:27019', {});
448+
const topology = topologyWithPlaceholderClient(
449+
'someserver:27019',
450+
{},
451+
{ serverSelectionTimeoutMS: 10 }
452+
);
448453
const requestCheck = sinon.stub(Server.prototype, 'requestCheck');
449454

450-
// satisfy the initial connect, then restore the original method
451-
const selectServer = sinon
452-
.stub(Topology.prototype, 'selectServer')
453-
.callsFake(async function () {
454-
const server = Array.from(this.s.servers.values())[0];
455-
selectServer.restore();
456-
return server;
457-
});
458-
459455
sinon.stub(Server.prototype, 'connect').callsFake(function () {
460456
this.s.state = 'connected';
461457
this.emit('connect');
462458
return;
463459
});
464460

465-
await topology.connect();
466-
const err = await topology
467-
.selectServer(ReadPreference.secondary, { serverSelectionTimeoutMS: 1000 })
468-
.then(
469-
() => null,
470-
e => e
471-
);
461+
const err = await topology.connect().then(
462+
() => null,
463+
e => e
464+
);
472465
expect(err).to.match(/Server selection timed out/);
473466
expect(err).to.have.property('reason');
474467
// When server is created `connect` is called on the monitor. When server selection
@@ -516,12 +509,20 @@ describe('Topology (unit)', function () {
516509
this.emit('connect');
517510
});
518511

512+
sinon.stub(ConnectionPool.prototype, 'checkOut').callsFake(async function () {
513+
return {};
514+
});
515+
516+
sinon.stub(ConnectionPool.prototype, 'checkIn').callsFake(function (_) {
517+
return;
518+
});
519+
519520
const toSelect = 10;
520521
let completed = 0;
521522
// methodology:
522523
// - perform 9 server selections, a few with a selector that throws an error
523524
// - ensure each selection immediately returns an empty result (gated by a boolean)
524-
// guaranteeing tha the queue will be full before the last selection
525+
// guaranteeing that the queue will be full before the last selection
525526
// - make one last selection, but ensure that all selections are no longer blocked from
526527
// returning their value
527528
// - verify that 10 callbacks were called

0 commit comments

Comments
 (0)