Skip to content

Commit 5127791

Browse files
committed
test: reorg
1 parent f6bd749 commit 5127791

File tree

2 files changed

+98
-91
lines changed

2 files changed

+98
-91
lines changed

test/unit/cmap/connect.test.ts

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { expect } from 'chai';
2-
import * as sinon from 'sinon';
32
import { setTimeout } from 'timers';
43
import { promisify } from 'util';
54

65
import {
7-
BinMsg,
86
CancellationToken,
97
ClientMetadata,
108
connect,
@@ -15,12 +13,10 @@ import {
1513
LEGACY_HELLO_COMMAND,
1614
MongoCredentials,
1715
MongoNetworkError,
18-
ns,
1916
prepareHandshakeDocument as prepareHandshakeDocumentCb
2017
} from '../../mongodb';
2118
import { genClusterTime } from '../../tools/common';
2219
import * as mock from '../../tools/mongodb-mock/index';
23-
import { generateOpMsgBuffer } from '../../tools/utils';
2420

2521
const CONNECT_DEFAULTS = {
2622
id: 1,
@@ -158,93 +154,6 @@ describe('Connect Tests', function () {
158154
});
159155
});
160156

161-
context('when sending commands on a connection', () => {
162-
let server;
163-
let connectOptions;
164-
let connection: Connection;
165-
let streamSetTimeoutSpy;
166-
167-
beforeEach(async () => {
168-
server = await mock.createServer();
169-
server.setMessageHandler(request => {
170-
if (isHello(request.document)) {
171-
request.reply(mock.HELLO);
172-
}
173-
});
174-
connectOptions = {
175-
...CONNECT_DEFAULTS,
176-
hostAddress: server.hostAddress() as HostAddress,
177-
socketTimeoutMS: 15000
178-
};
179-
180-
connection = await promisify<Connection>(callback =>
181-
//@ts-expect-error: Callbacks do not have mutual exclusion for error/result existence
182-
connect(connectOptions, callback)
183-
)();
184-
185-
streamSetTimeoutSpy = sinon.spy(connection.stream, 'setTimeout');
186-
});
187-
188-
afterEach(async () => {
189-
connection.destroy({ force: true });
190-
sinon.restore();
191-
await mock.cleanup();
192-
});
193-
194-
it('sets timeout specified on class before writing to the socket', async () => {
195-
await promisify(callback =>
196-
connection.command(ns('admin.$cmd'), { hello: 1 }, {}, callback)
197-
)();
198-
expect(streamSetTimeoutSpy).to.have.been.calledWith(15000);
199-
});
200-
201-
it('sets timeout specified on options before writing to the socket', async () => {
202-
await promisify(callback =>
203-
connection.command(ns('admin.$cmd'), { hello: 1 }, { socketTimeoutMS: 2000 }, callback)
204-
)();
205-
expect(streamSetTimeoutSpy).to.have.been.calledWith(2000);
206-
});
207-
208-
it('clears timeout after getting a message if moreToCome=false', async () => {
209-
connection.stream.setTimeout(1);
210-
const msg = generateOpMsgBuffer({ hello: 1 });
211-
const msgHeader = {
212-
length: msg.readInt32LE(0),
213-
requestId: 1,
214-
responseTo: 0,
215-
opCode: msg.readInt32LE(12)
216-
};
217-
const msgBody = msg.subarray(16);
218-
try {
219-
connection.onMessage(new BinMsg(msg, msgHeader, msgBody));
220-
} catch {
221-
// regardless of outcome
222-
}
223-
// timeout is still reset
224-
expect(connection.stream).to.have.property('timeout', 0);
225-
});
226-
227-
it('does not clear timeout after getting a message if moreToCome=true', async () => {
228-
connection.stream.setTimeout(1);
229-
const msg = generateOpMsgBuffer({ hello: 1 });
230-
const msgHeader = {
231-
length: msg.readInt32LE(0),
232-
requestId: 1,
233-
responseTo: 0,
234-
opCode: msg.readInt32LE(12)
235-
};
236-
const msgBody = msg.subarray(16);
237-
msgBody.writeInt32LE(2); // OPTS_MORE_TO_COME
238-
try {
239-
connection.onMessage(new BinMsg(msg, msgHeader, msgBody));
240-
} catch {
241-
// regardless of outcome
242-
}
243-
// timeout is still set
244-
expect(connection.stream).to.have.property('timeout', 1);
245-
});
246-
});
247-
248157
it('should emit `MongoNetworkError` for network errors', function (done) {
249158
connect({ hostAddress: new HostAddress('non-existent:27018') }, err => {
250159
expect(err).to.be.instanceOf(MongoNetworkError);

test/unit/cmap/connection.test.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import { Socket } from 'net';
44
import * as sinon from 'sinon';
55
import { Readable } from 'stream';
66
import { setTimeout } from 'timers';
7+
import { promisify } from 'util';
78

89
import {
910
BinMsg,
11+
ClientMetadata,
1012
connect,
1113
Connection,
1214
hasSessionSupport,
15+
HostAddress,
1316
isHello,
1417
MessageStream,
1518
MongoNetworkError,
@@ -413,6 +416,101 @@ describe('new Connection()', function () {
413416
});
414417
});
415418
});
419+
420+
context('when sending commands on a connection', () => {
421+
const CONNECT_DEFAULTS = {
422+
id: 1,
423+
tls: false,
424+
generation: 1,
425+
monitorCommands: false,
426+
metadata: {} as ClientMetadata,
427+
loadBalanced: false
428+
};
429+
let server;
430+
let connectOptions;
431+
let connection: Connection;
432+
let streamSetTimeoutSpy;
433+
434+
beforeEach(async () => {
435+
server = await mock.createServer();
436+
server.setMessageHandler(request => {
437+
if (isHello(request.document)) {
438+
request.reply(mock.HELLO);
439+
}
440+
});
441+
connectOptions = {
442+
...CONNECT_DEFAULTS,
443+
hostAddress: server.hostAddress() as HostAddress,
444+
socketTimeoutMS: 15000
445+
};
446+
447+
connection = await promisify<Connection>(callback =>
448+
//@ts-expect-error: Callbacks do not have mutual exclusion for error/result existence
449+
connect(connectOptions, callback)
450+
)();
451+
452+
streamSetTimeoutSpy = sinon.spy(connection.stream, 'setTimeout');
453+
});
454+
455+
afterEach(async () => {
456+
connection.destroy({ force: true });
457+
sinon.restore();
458+
await mock.cleanup();
459+
});
460+
461+
it('sets timeout specified on class before writing to the socket', async () => {
462+
await promisify(callback =>
463+
connection.command(ns('admin.$cmd'), { hello: 1 }, {}, callback)
464+
)();
465+
expect(streamSetTimeoutSpy).to.have.been.calledWith(15000);
466+
});
467+
468+
it('sets timeout specified on options before writing to the socket', async () => {
469+
await promisify(callback =>
470+
connection.command(ns('admin.$cmd'), { hello: 1 }, { socketTimeoutMS: 2000 }, callback)
471+
)();
472+
expect(streamSetTimeoutSpy).to.have.been.calledWith(2000);
473+
});
474+
475+
it('clears timeout after getting a message if moreToCome=false', async () => {
476+
connection.stream.setTimeout(1);
477+
const msg = generateOpMsgBuffer({ hello: 1 });
478+
const msgHeader = {
479+
length: msg.readInt32LE(0),
480+
requestId: 1,
481+
responseTo: 0,
482+
opCode: msg.readInt32LE(12)
483+
};
484+
const msgBody = msg.subarray(16);
485+
try {
486+
connection.onMessage(new BinMsg(msg, msgHeader, msgBody));
487+
} catch {
488+
// regardless of outcome
489+
}
490+
// timeout is still reset
491+
expect(connection.stream).to.have.property('timeout', 0);
492+
});
493+
494+
it('does not clear timeout after getting a message if moreToCome=true', async () => {
495+
connection.stream.setTimeout(1);
496+
const msg = generateOpMsgBuffer({ hello: 1 });
497+
const msgHeader = {
498+
length: msg.readInt32LE(0),
499+
requestId: 1,
500+
responseTo: 0,
501+
opCode: msg.readInt32LE(12)
502+
};
503+
const msgBody = msg.subarray(16);
504+
msgBody.writeInt32LE(2); // OPTS_MORE_TO_COME
505+
try {
506+
connection.onMessage(new BinMsg(msg, msgHeader, msgBody));
507+
} catch {
508+
// regardless of outcome
509+
}
510+
// timeout is still set
511+
expect(connection.stream).to.have.property('timeout', 1);
512+
});
513+
});
416514
});
417515

418516
describe('when the socket times out', () => {

0 commit comments

Comments
 (0)