Skip to content

Commit 99671a2

Browse files
committed
test(NODE-4783): add half integration test
1 parent 67216ef commit 99671a2

File tree

3 files changed

+69
-21
lines changed

3 files changed

+69
-21
lines changed

test/tools/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,18 @@ export class TestBuilder {
354354
}
355355
}
356356

357+
export function bufferToStream(buffer) {
358+
const stream = new Readable();
359+
if (Array.isArray(buffer)) {
360+
buffer.forEach(b => stream.push(b));
361+
} else {
362+
stream.push(buffer);
363+
}
364+
365+
stream.push(null);
366+
return stream;
367+
}
368+
357369
export function generateOpMsgBuffer(document: Document): Buffer {
358370
const header = Buffer.alloc(4 * 4 + 4);
359371

test/unit/cmap/connection.test.ts

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { expect } from 'chai';
2-
import { EventEmitter } from 'events';
2+
import { EventEmitter, on } from 'events';
33
import { Socket } from 'net';
44
import * as sinon from 'sinon';
5+
import { Readable } from 'stream';
56
import { setTimeout } from 'timers';
67

78
import { BinMsg } from '../../../src/cmap/commands';
@@ -165,11 +166,44 @@ describe('new Connection()', function () {
165166

166167
beforeEach(function () {
167168
driverSocket = sinon.spy(new FakeSocket());
168-
// @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay
169-
connection = sinon.spy(new Connection(driverSocket, connectionOptionsDefaults));
170-
connection.isMonitoringConnection = true;
171-
const queueSymbol = getSymbolFrom(connection, 'queue');
172-
queue = connection[queueSymbol];
169+
});
170+
171+
context('when multiple hellos exist on the stream', function () {
172+
let callbackSpy;
173+
const inputStream = new Readable();
174+
const document = { ok: 1 };
175+
176+
beforeEach(function () {
177+
callbackSpy = sinon.spy();
178+
const firstHello = generateOpMsgBuffer(document);
179+
const secondHello = generateOpMsgBuffer(document);
180+
const thirdHello = generateOpMsgBuffer(document);
181+
const buffer = Buffer.concat([firstHello, secondHello, thirdHello]);
182+
183+
// @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay
184+
connection = sinon.spy(new Connection(inputStream, connectionOptionsDefaults));
185+
connection.isMonitoringConnection = true;
186+
const queueSymbol = getSymbolFrom(connection, 'queue');
187+
queue = connection[queueSymbol];
188+
189+
// Create the operation description.
190+
const operationDescription: OperationDescription = {
191+
requestId: 1,
192+
cb: callbackSpy
193+
};
194+
195+
// Stick an operation description in the queue.
196+
queue.set(1, operationDescription);
197+
198+
// Push the buffer of 3 hellos to the input stream
199+
inputStream.push(buffer);
200+
inputStream.push(null);
201+
});
202+
203+
it('calls the operation description callback with the document', async function () {
204+
await on(inputStream, 'message');
205+
expect(callbackSpy).to.be.calledOnceWith(undefined, document);
206+
});
173207
});
174208

175209
context('when requestId/responseTo do not match', function () {
@@ -178,6 +212,13 @@ describe('new Connection()', function () {
178212

179213
beforeEach(function () {
180214
callbackSpy = sinon.spy();
215+
216+
// @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay
217+
connection = sinon.spy(new Connection(driverSocket, connectionOptionsDefaults));
218+
connection.isMonitoringConnection = true;
219+
const queueSymbol = getSymbolFrom(connection, 'queue');
220+
queue = connection[queueSymbol];
221+
181222
// Create the operation description.
182223
const operationDescription: OperationDescription = {
183224
requestId: 1,
@@ -201,7 +242,7 @@ describe('new Connection()', function () {
201242
});
202243

203244
it('calls the operation description callback with the document', function () {
204-
expect(callbackSpy).to.be.calledExactlyOnceWith(undefined, document);
245+
expect(callbackSpy).to.be.calledOnceWith(undefined, document);
205246
});
206247
});
207248

@@ -211,6 +252,13 @@ describe('new Connection()', function () {
211252

212253
beforeEach(function () {
213254
callbackSpy = sinon.spy();
255+
256+
// @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay
257+
connection = sinon.spy(new Connection(driverSocket, connectionOptionsDefaults));
258+
connection.isMonitoringConnection = true;
259+
const queueSymbol = getSymbolFrom(connection, 'queue');
260+
queue = connection[queueSymbol];
261+
214262
// Create the operation description.
215263
const operationDescription: OperationDescription = {
216264
requestId: 1,
@@ -234,7 +282,7 @@ describe('new Connection()', function () {
234282
});
235283

236284
it('calls the operation description callback with the document', function () {
237-
expect(callbackSpy).to.be.calledExactlyOnceWith(undefined, document);
285+
expect(callbackSpy).to.be.calledOnceWith(undefined, document);
238286
});
239287
});
240288
});

test/unit/cmap/message_stream.test.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,7 @@ const { MessageStream } = require('../../../src/cmap/message_stream');
66
const { Msg } = require('../../../src/cmap/commands');
77
const expect = require('chai').expect;
88
const { LEGACY_HELLO_COMMAND } = require('../../../src/constants');
9-
const { generateOpMsgBuffer } = require('../../tools/utils');
10-
11-
function bufferToStream(buffer) {
12-
const stream = new Readable();
13-
if (Array.isArray(buffer)) {
14-
buffer.forEach(b => stream.push(b));
15-
} else {
16-
stream.push(buffer);
17-
}
18-
19-
stream.push(null);
20-
return stream;
21-
}
9+
const { bufferToStream, generateOpMsgBuffer } = require('../../tools/utils');
2210

2311
describe('MessageStream', function () {
2412
context('when the stream is for a monitoring connection', function () {

0 commit comments

Comments
 (0)