Skip to content

Commit 8d9fde3

Browse files
W-A-Jamesnbbeeken
authored andcommitted
Updated tests
1 parent 484680d commit 8d9fde3

File tree

5 files changed

+18
-118
lines changed

5 files changed

+18
-118
lines changed

test/integration/crud/abstract_operation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ describe('abstract operation', async function () {
353353
subclassType.name === 'ProfilingLevelOperation' ? { ok: 1, was: 1 } : { ok: 1 };
354354
const cmdCallerStub = sinon
355355
.stub(Server.prototype, 'command')
356-
.yieldsRight(undefined, yieldDoc);
356+
.returns(Promise.resolve(yieldDoc));
357357
if (sameServerOnlyOperationSubclasses.includes(subclassType.name.toString())) {
358358
await subclassInstance.execute(constructorServer, client.session);
359359
} else {

test/integration/retryable-writes/non-server-retryable_writes.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ describe('Non Server Retryable Writes', function () {
3333
{ requires: { topology: 'replicaset', mongodb: '>=4.2.9' } },
3434
async () => {
3535
const serverCommandStub = sinon.stub(Server.prototype, 'command');
36-
serverCommandStub.onCall(0).yieldsRight(new PoolClearedError('error'));
36+
serverCommandStub.onCall(0).returns(Promise.reject(new PoolClearedError('error')));
3737
serverCommandStub
3838
.onCall(1)
39-
.yieldsRight(
40-
new MongoWriteConcernError({ errorLabels: ['NoWritesPerformed'], errorCode: 10107 }, {})
39+
.returns(
40+
Promise.reject(
41+
new MongoWriteConcernError({ errorLabels: ['NoWritesPerformed'], errorCode: 10107 }, {})
42+
)
4143
);
4244

4345
const insertResult = await collection.insertOne({ _id: 1 }).catch(error => error);

test/integration/server-selection/operation_count.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ describe('Server Operation Count Tests', function () {
170170
const server = Array.from(client.topology.s.servers.values())[0];
171171
expect(server.s.operationCount).to.equal(0);
172172

173-
sinon.stub(ConnectionPool.prototype, 'checkOut').callsFake(function (cb) {
174-
cb(new Error('unable to checkout connection'), undefined);
175-
});
173+
sinon
174+
.stub(ConnectionPool.prototype, 'checkOut')
175+
.returns(Promise.reject(new Error('unable to checkout connection')));
176176
const commandSpy = sinon.spy(server, 'command');
177177

178178
const error = await collection.insertOne({ count: 1 }).catch(e => e);

test/tools/cmap_spec_runner.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ const compareInputToSpec = (input, expected, message) => {
185185

186186
const getTestOpDefinitions = (threadContext: ThreadContext) => ({
187187
checkOut: async function (op) {
188-
const connection: Connection = await promisify(ConnectionPool.prototype.checkOut).call(
189-
threadContext.pool
190-
);
188+
const connection: Connection = await ConnectionPool.prototype.checkOut.call(threadContext.pool);
191189
if (op.label != null) {
192190
threadContext.connections.set(op.label, connection);
193191
} else {

test/unit/cmap/connection_pool.test.js

Lines changed: 8 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { ConnectionPool } = require('../../mongodb');
3+
const { ConnectionPool, MongoError } = require('../../mongodb');
44
const { WaitQueueTimeoutError } = require('../../mongodb');
55
const mock = require('../../tools/mongodb-mock/index');
66
const sinon = require('sinon');
@@ -69,7 +69,7 @@ describe('Connection Pool', function () {
6969
expect(closeEvent).have.property('reason').equal('error');
7070
});
7171

72-
it('should propagate socket timeouts to connections', function (done) {
72+
it('should propagate socket timeouts to connections', async function () {
7373
mockMongod.setMessageHandler(request => {
7474
const doc = request.document;
7575
if (isHello(doc)) {
@@ -87,18 +87,12 @@ describe('Connection Pool', function () {
8787

8888
pool.ready();
8989

90-
pool.withConnection(
91-
(err, conn, cb) => {
92-
expect(err).to.not.exist;
93-
conn.command(ns('admin.$cmd'), { ping: 1 }, undefined, (err, result) => {
94-
expect(err).to.exist;
95-
expect(result).to.not.exist;
96-
expect(err).to.match(/timed out/);
97-
cb();
98-
});
99-
},
100-
() => pool.close(done)
101-
);
90+
const conn = await pool.checkOut();
91+
const maybeError = await conn.command(ns('admin.$cmd'), { ping: 1 }, undefined).catch(e => e);
92+
expect(maybeError).to.be.instanceOf(MongoError);
93+
expect(maybeError).to.match(/timed out/);
94+
95+
pool.checkIn(conn);
10296
});
10397

10498
it('should clear timed out wait queue members if no connections are available', function (done) {
@@ -222,98 +216,4 @@ describe('Connection Pool', function () {
222216
expect(createConnStub).to.have.been.calledTwice;
223217
});
224218
});
225-
226-
describe('withConnection', function () {
227-
it('should manage a connection for a successful operation', function (done) {
228-
mockMongod.setMessageHandler(request => {
229-
const doc = request.document;
230-
if (isHello(doc)) {
231-
request.reply(mock.HELLO);
232-
}
233-
});
234-
235-
const pool = new ConnectionPool(stubServer, { hostAddress: mockMongod.hostAddress() });
236-
pool.ready();
237-
238-
const callback = (err, result) => {
239-
expect(err).to.not.exist;
240-
expect(result).to.exist;
241-
pool.close(done);
242-
};
243-
244-
pool.withConnection((err, conn, cb) => {
245-
expect(err).to.not.exist;
246-
247-
conn.command(
248-
ns('$admin.cmd'),
249-
{ [LEGACY_HELLO_COMMAND]: 1 },
250-
undefined,
251-
(cmdErr, hello) => {
252-
expect(cmdErr).to.not.exist;
253-
cb(undefined, hello);
254-
}
255-
);
256-
}, callback);
257-
});
258-
259-
it('should allow user interaction with an error', function (done) {
260-
mockMongod.setMessageHandler(request => {
261-
const doc = request.document;
262-
if (isHello(doc)) {
263-
request.connection.destroy();
264-
}
265-
});
266-
267-
const pool = new ConnectionPool(stubServer, {
268-
waitQueueTimeoutMS: 200,
269-
hostAddress: mockMongod.hostAddress()
270-
});
271-
272-
pool.ready();
273-
274-
const callback = err => {
275-
expect(err).to.exist;
276-
expect(err).to.match(/closed/);
277-
pool.close(done);
278-
};
279-
280-
pool.withConnection(
281-
undefined,
282-
(err, conn, cb) => {
283-
expect(err).to.exist;
284-
expect(err).to.match(/closed/);
285-
cb(err);
286-
},
287-
callback
288-
);
289-
});
290-
291-
it('should return an error to the original callback', function (done) {
292-
mockMongod.setMessageHandler(request => {
293-
const doc = request.document;
294-
if (isHello(doc)) {
295-
request.reply(mock.HELLO);
296-
}
297-
});
298-
299-
const pool = new ConnectionPool(stubServer, { hostAddress: mockMongod.hostAddress() });
300-
pool.ready();
301-
302-
const callback = (err, result) => {
303-
expect(err).to.exist;
304-
expect(result).to.not.exist;
305-
expect(err).to.match(/my great error/);
306-
pool.close(done);
307-
};
308-
309-
pool.withConnection(
310-
undefined,
311-
(err, conn, cb) => {
312-
expect(err).to.not.exist;
313-
cb(new Error('my great error'));
314-
},
315-
callback
316-
);
317-
});
318-
});
319219
});

0 commit comments

Comments
 (0)