Skip to content

Commit e46b986

Browse files
initial POC
1 parent 76c1c58 commit e46b986

File tree

8 files changed

+33
-99
lines changed

8 files changed

+33
-99
lines changed

src/change_stream.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,8 @@ export class ChangeStream<
10731073
try {
10741074
await topology.selectServer(this.cursor.readPreference, {
10751075
operationName: 'reconnect topology in change stream',
1076-
timeoutContext: this.timeoutContext
1076+
timeoutContext: this.timeoutContext,
1077+
deprioritizedServers: []
10771078
});
10781079
this.cursor = this._createChangeStreamCursor(this.cursor.resumeOptions);
10791080
} catch {

test/tools/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,12 @@ export function topologyWithPlaceholderClient(
285285
options: Partial<TopologyOptions>
286286
): Topology {
287287
return new Topology(
288-
new MongoClient('mongodb://iLoveJavaScript'),
288+
new MongoClient(
289+
'mongodb://iLoveJavaScript',
290+
options.serverSelectionTimeoutMS
291+
? { serverSelectionTimeoutMS: options.serverSelectionTimeoutMS }
292+
: {}
293+
),
289294
seeds,
290295
options as TopologyOptions
291296
);

test/unit/assorted/server_selection.spec.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
runServerSelectionLogicTest
1212
} from './server_selection_logic_spec_utils';
1313

14-
describe.only('Server Selection Logic (spec)', function () {
14+
describe('Server Selection Logic (spec)', function () {
1515
beforeEach(function () {
1616
if (this.currentTest.title.match(/Possible/)) {
1717
this.currentTest.skipReason = 'Nodejs driver does not support PossiblePrimary';

test/unit/assorted/server_selection_latency_window_utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ export async function runServerSelectionLatencyWindowTest(test: ServerSelectionL
134134
const selectedServers: Server[] = [];
135135

136136
for (let i = 0; i < test.iterations; ++i) {
137-
const server: Server = await topology.selectServer(ReadPreference.NEAREST, {});
137+
const server: Server = await topology.selectServer(ReadPreference.NEAREST, {
138+
deprioritizedServers: [],
139+
operationName: 'test operation'
140+
});
138141
selectedServers.push(server);
139142
}
140143

test/unit/assorted/server_selection_spec_helper.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ export async function executeServerSelectionTest(testDefinition) {
137137

138138
// default to serverSelectionTimeoutMS of `100` for unit tests
139139
try {
140-
const server = await topology.selectServer(selector, { serverSelectionTimeoutMS: 50 });
140+
const server = await topology.selectServer(selector, {
141+
serverSelectionTimeoutMS: 50,
142+
deprioritizedServers: [],
143+
operationName: 'test operation'
144+
});
141145

142146
if (testDefinition.error) throw new Error('Expected an error, but found none!');
143147
if (expectedServers.length === 0 && server !== null) {

test/unit/error.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,13 @@ describe('MongoErrors', () => {
383383
);
384384
return replSet
385385
.connect()
386-
.then(topology => topology.selectServer('primary', { timeoutContext }))
386+
.then(topology =>
387+
topology.selectServer('primary', {
388+
timeoutContext,
389+
deprioritizedServers: [],
390+
operationName: 'test operation'
391+
})
392+
)
387393
.then(server => server.command(op, timeoutContext))
388394
.then(
389395
() => expect.fail('expected command to fail'),
@@ -426,7 +432,11 @@ describe('MongoErrors', () => {
426432
Object.assign({}, RAW_USER_WRITE_CONCERN_CMD),
427433
{}
428434
);
429-
const server = await topology.selectServer('primary', { timeoutContext });
435+
const server = await topology.selectServer('primary', {
436+
timeoutContext,
437+
deprioritizedServers: [],
438+
operationName: 'test operation'
439+
});
430440
try {
431441
await server.command(op, timeoutContext);
432442
} catch (err) {

test/unit/sdam/server_selection.test.ts

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -50,93 +50,6 @@ describe('server selection', function () {
5050
ok: 0
5151
});
5252

53-
describe('#readPreferenceServerSelector', function () {
54-
let selector;
55-
let servers;
56-
57-
context('when the topology is sharded', function () {
58-
const topologyDescription = new TopologyDescription(
59-
TopologyType.Sharded,
60-
new Map(),
61-
'test',
62-
MIN_SECONDARY_WRITE_WIRE_VERSION,
63-
new ObjectId(),
64-
MIN_SECONDARY_WRITE_WIRE_VERSION
65-
);
66-
67-
beforeEach(function () {
68-
selector = readPreferenceServerSelector(ReadPreference.secondaryPreferred);
69-
});
70-
71-
context('when there are deprioritized servers', function () {
72-
context('when there are other servers', function () {
73-
beforeEach(function () {
74-
servers = selector(topologyDescription, [mongos], [mongosTwo]);
75-
});
76-
77-
it('returns a server from the other servers', function () {
78-
expect(servers).to.deep.equal([mongos]);
79-
});
80-
});
81-
82-
context('when there are no other servers', function () {
83-
beforeEach(function () {
84-
servers = selector(topologyDescription, [], [mongosTwo]);
85-
});
86-
87-
it('returns a server from the deprioritized servers', function () {
88-
expect(servers).to.deep.equal([mongosTwo]);
89-
});
90-
});
91-
});
92-
93-
context('when there are no deprioritised servers', function () {
94-
beforeEach(function () {
95-
servers = selector(topologyDescription, [mongos]);
96-
});
97-
98-
it('returns a server from the other servers', function () {
99-
expect(servers).to.deep.equal([mongos]);
100-
});
101-
});
102-
});
103-
104-
context('when the topology is not sharded', function () {
105-
const topologyDescription = new TopologyDescription(
106-
TopologyType.ReplicaSetWithPrimary,
107-
new Map(),
108-
'test',
109-
MIN_SECONDARY_WRITE_WIRE_VERSION,
110-
new ObjectId(),
111-
MIN_SECONDARY_WRITE_WIRE_VERSION
112-
);
113-
114-
beforeEach(function () {
115-
selector = readPreferenceServerSelector(ReadPreference.secondary);
116-
});
117-
118-
context('when there are deprioritized servers', function () {
119-
beforeEach(function () {
120-
servers = selector(topologyDescription, [secondaryTwo], [secondary]);
121-
});
122-
123-
it('selects from all server lists', function () {
124-
expect(servers).to.contain.oneOf([secondary, secondaryTwo]);
125-
});
126-
});
127-
128-
context('when there are no deprioritised servers', function () {
129-
beforeEach(function () {
130-
servers = selector(topologyDescription, [secondary], []);
131-
});
132-
133-
it('selects from all non-deprioritised servers', function () {
134-
expect(servers).to.deep.equal([secondary]);
135-
});
136-
});
137-
});
138-
});
139-
14053
describe('#sameServerSelector', function () {
14154
const topologyDescription = sinon.stub();
14255
const serverDescriptions = new Map();

test/unit/sdam/topology.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,9 @@ describe('Topology (unit)', function () {
446446

447447
describe('selectServer()', function () {
448448
it('should schedule monitoring if no suitable server is found', async function () {
449-
const topology = topologyWithPlaceholderClient(
450-
'someserver:27019',
451-
{},
452-
{ serverSelectionTimeoutMS: 10 }
453-
);
449+
const topology = topologyWithPlaceholderClient('someserver:27019', {
450+
serverSelectionTimeoutMS: 10
451+
});
454452
const requestCheck = sinon.stub(Server.prototype, 'requestCheck');
455453

456454
sinon.stub(Server.prototype, 'connect').callsFake(function () {

0 commit comments

Comments
 (0)