Skip to content

Commit 2cdc099

Browse files
authoredMay 5, 2017
Merge pull request #34 from signalfx/wip/fix-tests
Fix unit tests for decoding compressed socket messages
2 parents 4d7a24f + fa8eb23 commit 2cdc099

File tree

2 files changed

+69
-42
lines changed

2 files changed

+69
-42
lines changed
 

‎lib/client/signalflow/websocket_message_parser.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ function parseBinaryMessage(data, knownComputations) {
269269

270270
if (json) {
271271
var decoded = textDecoder.decode(data);
272-
return JSON.parse(decoded);
272+
var body = JSON.parse(decoded);
273+
Object.keys(body).forEach(function (k) {
274+
msg[k] = body[k];
275+
});
276+
return msg;
273277
}
274278

275279
switch (msg['type']) {

‎test/signalflow/websocket_message_parser.js

+64-41
Original file line numberDiff line numberDiff line change
@@ -264,54 +264,77 @@ describe('it should properly unpack binary compressed json messages', function (
264264
}
265265
});
266266

267+
it('should unpack the 1-byte version correctly', function () {
268+
expect(outputMsg.version).to.equal(1);
269+
});
270+
267271
it('should unpack the 1-byte message type correctly', function () {
268272
expect(outputMsg.type).to.equal('control-message');
269273
});
270274

275+
it('should unpack the 1-byte flags correctly', function () {
276+
expect(outputMsg.flags).to.equal(3);
277+
});
278+
279+
it('should unpack the channel name correctly', function () {
280+
expect(outputMsg.channel).to.equal('R0');
281+
});
282+
271283
it('should unpack the json message body correctly', function () {
272284
expect(outputMsg.event).to.equal('JOB_START');
273285
expect(outputMsg.handle).to.equal('AAAAAAAAACo');
274286
});
275287
});
276288

277-
// TODO : this test is not working and causes the suite to not complete. fixme later
278-
//describe('it should properly unpack binary compressed data messages', function () {
279-
// var originalMsg = [2, 0, 5, 1, 82, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
280-
// 120, 156, 99, 96, 96, 96, 98, 102, 128, 0, 45, 8, 197, 114, 137, 9,
281-
// 42, 160, 237, 192, 41, 183, 35, 240, 117, 171, 60, 0, 33, 132, 4, 50];
282-
// var arrBuff = new ArrayBuffer(originalMsg.length);
283-
// var typedArr = new Uint8Array(arrBuff);
284-
// originalMsg.forEach(function (val, idx) {
285-
// typedArr[idx] = val;
286-
// });
287-
//
288-
// var outputMsg = wsmh.parseWebSocketMessage({data: arrBuff}, {
289-
// R0: {
290-
// params: {
291-
// }
292-
// }
293-
// });
294-
//
295-
// it('should unpack the 2-byte version correctly', function () {
296-
// expect(outputMsg.version).to.equal(512);
297-
// });
298-
//
299-
// it('should unpack the 1-byte message type correctly', function () {
300-
// expect(outputMsg.type).to.equal('data');
301-
// });
302-
//
303-
// it('should unpack the 1-byte flags correctly', function () {
304-
// expect(outputMsg.flags).to.equal(1);
305-
// });
306-
//
307-
// it('should detect the correct number of datapoints in binary data batch', function () {
308-
// expect(outputMsg.count).to.equal(2);
309-
// });
310-
//
311-
// it('should decode the datapoints correctly from the binary data batch', function () {
312-
// expect(outputMsg.data[0].tsId).to.equal('AAAAAAAAACo');
313-
// expect(outputMsg.data[0].value).to.equal(1234);
314-
// expect(outputMsg.data[1].tsId).to.equal('AAAAAAAAACs');
315-
// expect(outputMsg.data[1].value).to.equal(3.14);
316-
// });
317-
//});
289+
describe('it should properly unpack binary compressed data messages', function () {
290+
var originalMsg = [2, 0, 5, 1, 82, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
291+
120, 156, 99, 96, 0, 1, 150, 75, 96, 138, 65, 32, 3, 72, 48, 49,
292+
67, 56, 12, 92, 80, 90, 139, 9, 202, 224, 118, 224, 148, 219, 17,
293+
248, 186, 85, 30, 0, 74, 190, 4, 148];
294+
var arrBuff = new ArrayBuffer(originalMsg.length);
295+
var typedArr = new Uint8Array(arrBuff);
296+
originalMsg.forEach(function (val, idx) {
297+
typedArr[idx] = val;
298+
});
299+
300+
var outputMsg = wsmh.parseWebSocketMessage({data: arrBuff}, {
301+
R0: {
302+
params: {
303+
}
304+
}
305+
});
306+
307+
it('should unpack the 2-byte version correctly', function () {
308+
expect(outputMsg.version).to.equal(512);
309+
});
310+
311+
it('should unpack the 1-byte message type correctly', function () {
312+
expect(outputMsg.type).to.equal('data');
313+
});
314+
315+
it('should unpack the 1-byte flags correctly', function () {
316+
expect(outputMsg.flags).to.equal(1);
317+
});
318+
319+
it('should unpack the correct data batch timestamp', function () {
320+
expect(outputMsg.logicalTimestampMs).to.equal(1234);
321+
});
322+
323+
it('should unpack the correct data batch max delay', function () {
324+
expect(outputMsg.maxDelayMs).to.equal(4200);
325+
});
326+
327+
it('should detect the correct number of datapoints in binary data batch', function () {
328+
expect(outputMsg.data.length).to.equal(2);
329+
});
330+
331+
it('should decode the first datapoint correctly from the binary data batch', function () {
332+
expect(outputMsg.data[0].tsId).to.equal('AAAAAAAAAAo');
333+
expect(outputMsg.data[0].value).to.equal(42);
334+
});
335+
336+
it('should decode the second datapoint correctly from the binary data batch', function () {
337+
expect(outputMsg.data[1].tsId).to.equal('AAAAAAAAAAs');
338+
expect(outputMsg.data[1].value).to.equal(3.14);
339+
});
340+
});

0 commit comments

Comments
 (0)
Please sign in to comment.