Skip to content

Commit e9cd32c

Browse files
committed
test(NODE-4834): Split up tests more cleanly
1 parent 23755ca commit e9cd32c

File tree

1 file changed

+78
-53
lines changed

1 file changed

+78
-53
lines changed

test/unit/cmap/connection.test.ts

Lines changed: 78 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ class InputStream extends Readable {
6666
}
6767
}
6868

69-
describe('new Connection()', function () {
69+
describe('new Connection()', function() {
7070
let server;
7171
after(() => mock.cleanup());
7272
before(() => mock.createServer().then(s => (server = s)));
7373

74-
it('should support fire-and-forget messages', function (done) {
74+
it('should support fire-and-forget messages', function(done) {
7575
server.setMessageHandler(request => {
7676
const doc = request.document;
7777
if (isHello(doc)) {
@@ -100,7 +100,7 @@ describe('new Connection()', function () {
100100
});
101101
});
102102

103-
it('should destroy streams which time out', function (done) {
103+
it('should destroy streams which time out', function(done) {
104104
server.setMessageHandler(request => {
105105
const doc = request.document;
106106
if (isHello(doc)) {
@@ -131,7 +131,7 @@ describe('new Connection()', function () {
131131
});
132132
});
133133

134-
it('should throw a network error with kBeforeHandshake set to false on timeout after handshake', function (done) {
134+
it('should throw a network error with kBeforeHandshake set to false on timeout after handshake', function(done) {
135135
server.setMessageHandler(request => {
136136
const doc = request.document;
137137
if (isHello(doc)) {
@@ -160,7 +160,7 @@ describe('new Connection()', function () {
160160
});
161161
});
162162

163-
it('should throw a network error with kBeforeHandshake set to true on timeout before handshake', function (done) {
163+
it('should throw a network error with kBeforeHandshake set to true on timeout before handshake', function(done) {
164164
server.setMessageHandler(() => {
165165
// respond to no requests to trigger timeout event
166166
});
@@ -181,23 +181,23 @@ describe('new Connection()', function () {
181181
});
182182
});
183183

184-
describe('#onMessage', function () {
185-
context('when the connection is a monitoring connection', function () {
184+
describe('#onMessage', function() {
185+
context('when the connection is a monitoring connection', function() {
186186
let queue: Map<number, OperationDescription>;
187187
let driverSocket: FakeSocket;
188188
let connection: Connection;
189189

190-
beforeEach(function () {
190+
beforeEach(function() {
191191
driverSocket = sinon.spy(new FakeSocket());
192192
});
193193

194-
context('when multiple hellos exist on the stream', function () {
194+
context('when multiple hellos exist on the stream', function() {
195195
let callbackSpy;
196196
const inputStream = new InputStream();
197197
const document = { ok: 1 };
198198
const last = { isWritablePrimary: true };
199199

200-
beforeEach(function () {
200+
beforeEach(function() {
201201
callbackSpy = sinon.spy();
202202
const firstHello = generateOpMsgBuffer(document);
203203
const secondHello = generateOpMsgBuffer(document);
@@ -223,18 +223,18 @@ describe('new Connection()', function () {
223223
inputStream.push(null);
224224
});
225225

226-
it('calls the callback with the last hello document', async function () {
226+
it('calls the callback with the last hello document', async function() {
227227
const messages = await once(connection, 'message');
228228
expect(messages[0].responseTo).to.equal(0);
229229
expect(callbackSpy).to.be.calledOnceWith(undefined, last);
230230
});
231231
});
232232

233-
context('when requestId/responseTo do not match', function () {
233+
context('when requestId/responseTo do not match', function() {
234234
let callbackSpy;
235235
const document = { ok: 1 };
236236

237-
beforeEach(function () {
237+
beforeEach(function() {
238238
callbackSpy = sinon.spy();
239239

240240
// @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay
@@ -265,16 +265,16 @@ describe('new Connection()', function () {
265265
connection.onMessage(message);
266266
});
267267

268-
it('calls the operation description callback with the document', function () {
268+
it('calls the operation description callback with the document', function() {
269269
expect(callbackSpy).to.be.calledOnceWith(undefined, document);
270270
});
271271
});
272272

273-
context('when requestId/reponseTo match', function () {
273+
context('when requestId/reponseTo match', function() {
274274
let callbackSpy;
275275
const document = { ok: 1 };
276276

277-
beforeEach(function () {
277+
beforeEach(function() {
278278
callbackSpy = sinon.spy();
279279

280280
// @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay
@@ -305,23 +305,23 @@ describe('new Connection()', function () {
305305
connection.onMessage(message);
306306
});
307307

308-
it('calls the operation description callback with the document', function () {
308+
it('calls the operation description callback with the document', function() {
309309
expect(callbackSpy).to.be.calledOnceWith(undefined, document);
310310
});
311311
});
312312

313-
context('when no operation description is in the queue', function () {
313+
context('when no operation description is in the queue', function() {
314314
const document = { ok: 1 };
315315

316-
beforeEach(function () {
316+
beforeEach(function() {
317317
// @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay
318318
connection = sinon.spy(new Connection(driverSocket, connectionOptionsDefaults));
319319
connection.isMonitoringConnection = true;
320320
const queueSymbol = getSymbolFrom(connection, 'queue');
321321
queue = connection[queueSymbol];
322322
});
323323

324-
it('does not error', function () {
324+
it('does not error', function() {
325325
const msg = generateOpMsgBuffer(document);
326326
const msgHeader: MessageHeader = {
327327
length: msg.readInt32LE(0),
@@ -338,12 +338,12 @@ describe('new Connection()', function () {
338338
});
339339
});
340340

341-
context('when more than one operation description is in the queue', function () {
341+
context('when more than one operation description is in the queue', function() {
342342
let spyOne;
343343
let spyTwo;
344344
const document = { ok: 1 };
345345

346-
beforeEach(function () {
346+
beforeEach(function() {
347347
spyOne = sinon.spy();
348348
spyTwo = sinon.spy();
349349

@@ -380,7 +380,7 @@ describe('new Connection()', function () {
380380
connection.onMessage(message);
381381
});
382382

383-
it('calls all operation description callbacks with an error', function () {
383+
it('calls all operation description callbacks with an error', function() {
384384
expect(spyOne).to.be.calledOnce;
385385
expect(spyTwo).to.be.calledOnce;
386386
const errorOne = spyOne.firstCall.args[0];
@@ -542,12 +542,12 @@ describe('new Connection()', function () {
542542
});
543543
});
544544

545-
describe('.hasSessionSupport', function () {
545+
describe('.hasSessionSupport', function() {
546546
let connection;
547547
const stream = new Socket();
548548

549-
context('when logicalSessionTimeoutMinutes is present', function () {
550-
beforeEach(function () {
549+
context('when logicalSessionTimeoutMinutes is present', function() {
550+
beforeEach(function() {
551551
const options = {
552552
...connectionOptionsDefaults,
553553
hostAddress: server.hostAddress(),
@@ -556,14 +556,14 @@ describe('new Connection()', function () {
556556
connection = new Connection(stream, options);
557557
});
558558

559-
it('returns true', function () {
559+
it('returns true', function() {
560560
expect(hasSessionSupport(connection)).to.be.true;
561561
});
562562
});
563563

564-
context('when logicalSessionTimeoutMinutes is not present', function () {
565-
context('when in load balancing mode', function () {
566-
beforeEach(function () {
564+
context('when logicalSessionTimeoutMinutes is not present', function() {
565+
context('when in load balancing mode', function() {
566+
beforeEach(function() {
567567
const options = {
568568
...connectionOptionsDefaults,
569569
hostAddress: server.hostAddress(),
@@ -572,13 +572,13 @@ describe('new Connection()', function () {
572572
connection = new Connection(stream, options);
573573
});
574574

575-
it('returns true', function () {
575+
it('returns true', function() {
576576
expect(hasSessionSupport(connection)).to.be.true;
577577
});
578578
});
579579

580-
context('when not in load balancing mode', function () {
581-
beforeEach(function () {
580+
context('when not in load balancing mode', function() {
581+
beforeEach(function() {
582582
const options = {
583583
...connectionOptionsDefaults,
584584
hostAddress: server.hostAddress(),
@@ -587,7 +587,7 @@ describe('new Connection()', function () {
587587
connection = new Connection(stream, options);
588588
});
589589

590-
it('returns false', function () {
590+
it('returns false', function() {
591591
expect(hasSessionSupport(connection)).to.be.false;
592592
});
593593
});
@@ -616,24 +616,7 @@ describe('new Connection()', function () {
616616
clock.restore();
617617
});
618618

619-
it('ends the tcp socket and destroys the messageStream', () => {
620-
connection.destroy({ force: false });
621-
clock.tick(1);
622-
expect(messageStream.destroy).to.have.been.calledOnce;
623-
expect(driverSocket.end).to.have.been.calledOnce;
624-
});
625-
626-
it('calls stream.end exactly once when destroy is called multiple times', () => {
627-
connection.destroy({ force: false });
628-
connection.destroy({ force: false });
629-
connection.destroy({ force: false });
630-
connection.destroy({ force: false });
631-
clock.tick(1);
632-
expect(driverSocket.destroy).to.not.have.been.called;
633-
expect(driverSocket.end).to.have.been.calledOnce;
634-
});
635-
636-
context('when options.force == true', function () {
619+
context('when options.force == true', function() {
637620
it('calls steam.destroy', () => {
638621
connection.destroy({ force: true });
639622
clock.tick(1);
@@ -645,9 +628,30 @@ describe('new Connection()', function () {
645628
clock.tick(1);
646629
expect(driverSocket.end).to.not.have.been.called;
647630
});
631+
632+
it('destroys the tcp socket', () => {
633+
connection.destroy({ force: true });
634+
clock.tick(1);
635+
expect(driverSocket.destroy).to.have.been.calledOnce;
636+
});
637+
638+
it('destroys the messageStream', () => {
639+
connection.destroy({ force: true });
640+
clock.tick(1);
641+
expect(messageStream.destroy).to.have.been.calledOnce;
642+
});
643+
644+
it('calls stream.destroy whenever destroy is called ', () => {
645+
connection.destroy({ force: true });
646+
connection.destroy({ force: true });
647+
connection.destroy({ force: true });
648+
clock.tick(1);
649+
expect(driverSocket.destroy).to.have.been.calledThrice;
650+
expect(driverSocket.end).to.not.have.been.called;
651+
});
648652
});
649653

650-
context('when options.force == false', function () {
654+
context('when options.force == false', function() {
651655
it('calls stream.end', () => {
652656
connection.destroy({ force: false });
653657
clock.tick(1);
@@ -659,6 +663,27 @@ describe('new Connection()', function () {
659663
clock.tick(1);
660664
expect(driverSocket.destroy).to.not.have.been.called;
661665
});
666+
667+
it('ends the tcp socket', () => {
668+
connection.destroy({ force: false });
669+
clock.tick(1);
670+
expect(driverSocket.end).to.have.been.calledOnce;
671+
});
672+
673+
it('destroys the messageStream', () => {
674+
connection.destroy({ force: false });
675+
clock.tick(1);
676+
expect(messageStream.destroy).to.have.been.calledOnce;
677+
});
678+
679+
it('calls stream.end exactly once when destroy is called multiple times', () => {
680+
connection.destroy({ force: false });
681+
connection.destroy({ force: false });
682+
connection.destroy({ force: false });
683+
connection.destroy({ force: false });
684+
clock.tick(1);
685+
expect(driverSocket.end).to.have.been.calledOnce;
686+
});
662687
});
663688
});
664689
});

0 commit comments

Comments
 (0)