From c07f20606d90998288a5b89fccdcab569df270a5 Mon Sep 17 00:00:00 2001 From: Steven Gum <14935595+stevengum@users.noreply.github.com> Date: Tue, 10 Mar 2020 14:38:41 -0700 Subject: [PATCH] apply PayloadSender fix from #1886 and add unit test (#1887) --- .../src/payloadTransport/payloadSender.ts | 24 +++++++--- .../tests/PayloadSender.test.js | 48 +++++++++++++++++-- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/libraries/botframework-streaming/src/payloadTransport/payloadSender.ts b/libraries/botframework-streaming/src/payloadTransport/payloadSender.ts index 554192406e..e6f339eee1 100644 --- a/libraries/botframework-streaming/src/payloadTransport/payloadSender.ts +++ b/libraries/botframework-streaming/src/payloadTransport/payloadSender.ts @@ -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) { diff --git a/libraries/botframework-streaming/tests/PayloadSender.test.js b/libraries/botframework-streaming/tests/PayloadSender.test.js index 4a5cf13c4f..919567b29d 100644 --- a/libraries/botframework-streaming/tests/PayloadSender.test.js +++ b/libraries/botframework-streaming/tests/PayloadSender.test.js @@ -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{ @@ -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); @@ -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) => {