Skip to content

Commit

Permalink
apply PayloadSender fix from #1886 and add unit test (#1887)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengum authored Mar 10, 2020
1 parent 42e366d commit c07f206
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,26 @@ export class PayloadSender {

private writePacket(packet: ISendPacket): void {
try {
let sendHeaderBuffer: Buffer = Buffer.alloc(PayloadConstants.MaxHeaderLength);
HeaderSerializer.serialize(packet.header, sendHeaderBuffer);
this.sender.send(sendHeaderBuffer);

if (packet.header.payloadLength > 0 && packet.payload) {
let count = packet.header.payloadLength;
while (count > 0) {

let leftOver = packet.header.payloadLength;

while (leftOver > 0) {
let count = leftOver <= PayloadConstants.MaxPayloadLength ? leftOver : PayloadConstants.MaxPayloadLength;
let chunk = packet.payload.read(count);

var header = packet.header;
header.payloadLength = count;
header.end = leftOver <= PayloadConstants.MaxPayloadLength;

let sendHeaderBuffer: Buffer = Buffer.alloc(PayloadConstants.MaxHeaderLength);

HeaderSerializer.serialize(header, sendHeaderBuffer);

this.sender.send(sendHeaderBuffer);

this.sender.send(chunk);
count -= chunk.length;
leftOver-= chunk.length;
}

if (packet.sentCallback) {
Expand Down
48 changes: 44 additions & 4 deletions libraries/botframework-streaming/tests/PayloadSender.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const PayloadReceiver = require('../lib/PayloadTransport/payloadReceiver');
const PayloadTypes = require('../lib/payloads/payloadTypes');
const PayloadAssemblerManager = require('../lib/payloads/payloadAssemblerManager');
const chai = require('chai');
var sinon = require('sinon');
var expect = chai.expect;

class FauxSock{
Expand Down Expand Up @@ -38,7 +39,6 @@ class FauxSock{
}
describe('PayloadTransport', () => {
describe('PayloadSender', () => {

it('starts out disconnected.', () => {
let ps = new PayloadSender.PayloadSender();
expect(ps.isConnected).to.equal(false);
Expand All @@ -57,10 +57,50 @@ describe('PayloadTransport', () => {

let stream = new SubscribableStream.SubscribableStream();
stream.write('This is a test stream.');
let header = {payloadType: PayloadTypes.PayloadTypes.request, payloadLength: 22, id: '100', end: true};
let packet = {header: header, payload: stream, sendCallBack: undefined};

let header = {
payloadType: PayloadTypes.PayloadTypes.request,
payloadLength: stream.length,
id: '100',
end: true
};

const psSenderSpy = sinon.spy(ps.sender, 'send');
expect(ps.sendPayload(header, stream, ()=>done()));
expect(psSenderSpy.calledTwice).to.be.true;
});

it('writes a large stream to its sender.', (done) => {
const ps = new PayloadSender.PayloadSender();
ps.connect(new FauxSock);
expect(ps.isConnected).to.equal(true);

const stream = new SubscribableStream.SubscribableStream();
let testString;
let count = 250;
while (count >= 0) {
testString += 'This is a LARGE test stream.';
count--;
}

stream.write(testString);
// Max PayloadLength is 4096
const header = {
payloadType: PayloadTypes.PayloadTypes.request,
payloadLength: stream.length,
id: '100',
end: true
};
const psSenderSpy = sinon.spy(ps.sender, 'send');

expect(ps.sendPayload(header, stream, () => {
// This try-catch is required as chai failures need to be caught and bubbled up via done().
try {
expect(psSenderSpy.callCount).to.equal(4);
done();
} catch (e) {
done(e);
}
}));
});

it('calls the packet sent callback.', (done) => {
Expand Down

0 comments on commit c07f206

Please sign in to comment.