Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply PayloadSender fix from #1886 and add unit test #1887

Merged
merged 1 commit into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line

let leftOver = packet.header.payloadLength;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to remainingBytes


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line

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