|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const chai = require('chai'); |
| 4 | +const expect = chai.expect; |
| 5 | +const serUtils = require('../utils/serialization_utils.js'); |
| 6 | +const DeserializeStream = serUtils.DeserializeStream; |
| 7 | + |
| 8 | +describe('DeserializeStream', () => { |
| 9 | + const deserializeStream = new DeserializeStream(); |
| 10 | + |
| 11 | + it('basic', (done) => { |
| 12 | + const len = 20; |
| 13 | + deserializeStream.on('message', (message) => { |
| 14 | + expect(message.length).to.equal(len); |
| 15 | + |
| 16 | + done(); |
| 17 | + }); |
| 18 | + |
| 19 | + const buf = new Buffer(len).fill(0); |
| 20 | + const bufWithLen = serUtils.Serialize(buf); |
| 21 | + |
| 22 | + deserializeStream.write(bufWithLen); |
| 23 | + }); |
| 24 | + |
| 25 | + it('2 chunks', (done) => { |
| 26 | + const len = 20; |
| 27 | + let iter = 0; |
| 28 | + deserializeStream.on('message', (message) => { |
| 29 | + ++iter; |
| 30 | + expect(message.length).to.equal(len); |
| 31 | + |
| 32 | + if (iter === 2) { |
| 33 | + done(); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + const buf = new Buffer(len).fill(0); |
| 38 | + const bufWithLen = serUtils.Serialize(buf); |
| 39 | + |
| 40 | + deserializeStream.write(bufWithLen); |
| 41 | + deserializeStream.write(bufWithLen); |
| 42 | + }); |
| 43 | + |
| 44 | + it('split length', (done) => { |
| 45 | + const len = 20; |
| 46 | + let iter = 0; |
| 47 | + deserializeStream.on('message', (message) => { |
| 48 | + ++iter; |
| 49 | + expect(message.length).to.equal(len); |
| 50 | + |
| 51 | + if (iter === 2) { |
| 52 | + done(); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + const buf = new Buffer(len).fill(0); |
| 57 | + const bufWithLen = serUtils.Serialize(buf); |
| 58 | + |
| 59 | + const doubleBuf = Buffer.concat([bufWithLen, bufWithLen]); |
| 60 | + const bufA = doubleBuf.slice(0, len+5); |
| 61 | + const bufB = doubleBuf.slice(len+5); |
| 62 | + |
| 63 | + deserializeStream.write(bufA); |
| 64 | + deserializeStream.write(bufB); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('service response flag', () => { |
| 68 | + |
| 69 | + beforeEach(() => { |
| 70 | + deserializeStream.setServiceRespDeserialize(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('normal', (done) => { |
| 74 | + const len = 20; |
| 75 | + let iter = 0; |
| 76 | + deserializeStream.on('message', (message) => { |
| 77 | + ++iter; |
| 78 | + expect(message.length).to.equal(len); |
| 79 | + |
| 80 | + if (iter === 2) { |
| 81 | + done(); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + const buf = new Buffer(len).fill(0); |
| 86 | + const bufWithLen = serUtils.Serialize(buf); |
| 87 | + const okBuf = new Buffer(1).fill(1); |
| 88 | + |
| 89 | + const doubleBuf = Buffer.concat([okBuf, bufWithLen, okBuf, bufWithLen]); |
| 90 | + const bufA = doubleBuf.slice(0, len+5); |
| 91 | + const bufB = doubleBuf.slice(len+5); |
| 92 | + |
| 93 | + deserializeStream.write(bufA); |
| 94 | + deserializeStream.write(bufB); |
| 95 | + }); |
| 96 | + |
| 97 | + it('improper', (done) => { |
| 98 | + const len = 19; |
| 99 | + let iter = 0; |
| 100 | + deserializeStream.on('message', (message) => { |
| 101 | + ++iter; |
| 102 | + expect(message.length).to.equal(len); |
| 103 | + |
| 104 | + if (iter === 2) { |
| 105 | + done(); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + const buf = new Buffer(len + 5).fill(0); |
| 110 | + buf[0] = 1; |
| 111 | + buf.writeUInt32LE(19, 1); |
| 112 | + |
| 113 | + const doubleBuf = Buffer.concat([buf, buf]); |
| 114 | + const bufA = doubleBuf.slice(0, len+6); |
| 115 | + const bufB = doubleBuf.slice(len+6); |
| 116 | + |
| 117 | + deserializeStream.write(bufA); |
| 118 | + deserializeStream.write(bufB); |
| 119 | + }); |
| 120 | + |
| 121 | + afterEach(() => { |
| 122 | + deserializeStream._deserializeServiceResp = false; |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('various splits', () => { |
| 127 | + const len = 20; |
| 128 | + const bufData = new Buffer(len).fill(0); |
| 129 | + const serBufData = serUtils.Serialize(bufData); |
| 130 | + |
| 131 | + const peelEmOff = (buff, sliceLens) => { |
| 132 | + let i = 0; |
| 133 | + while (buff.length > 0) { |
| 134 | + const sliceLen = sliceLens[i++]; |
| 135 | + const slice = buff.slice(0, sliceLen); |
| 136 | + deserializeStream.write(slice); |
| 137 | + buff = buff.slice(sliceLen); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + it('1', (done) => { |
| 142 | + const sliceLens = [24, 23, 25]; |
| 143 | + const numBufs = sliceLens.length; |
| 144 | + const bufArr = new Array(numBufs).fill(serBufData); |
| 145 | + let bigBuff = Buffer.concat(bufArr); |
| 146 | + |
| 147 | + let iter = 0; |
| 148 | + deserializeStream.on('message', (message) => { |
| 149 | + ++iter; |
| 150 | + expect(message.length).to.equal(len); |
| 151 | + |
| 152 | + if (iter === numBufs) { |
| 153 | + done(); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + peelEmOff(bigBuff, sliceLens); |
| 158 | + }); |
| 159 | + |
| 160 | + it('2', (done) => { |
| 161 | + const sliceLens = [18, 26, 28]; |
| 162 | + const numBufs = sliceLens.length; |
| 163 | + const bufArr = new Array(numBufs).fill(serBufData); |
| 164 | + let bigBuff = Buffer.concat(bufArr); |
| 165 | + |
| 166 | + let iter = 0; |
| 167 | + deserializeStream.on('message', (message) => { |
| 168 | + ++iter; |
| 169 | + expect(message.length).to.equal(len); |
| 170 | + |
| 171 | + if (iter === numBufs) { |
| 172 | + done(); |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + peelEmOff(bigBuff, sliceLens); |
| 177 | + }); |
| 178 | + |
| 179 | + it('3', (done) => { |
| 180 | + const sliceLens = [18, 28, 26, 25, 26, 24, 21, 23, 25]; |
| 181 | + const numBufs = sliceLens.length; |
| 182 | + const bufArr = new Array(numBufs).fill(serBufData); |
| 183 | + let bigBuff = Buffer.concat(bufArr); |
| 184 | + |
| 185 | + let iter = 0; |
| 186 | + deserializeStream.on('message', (message) => { |
| 187 | + ++iter; |
| 188 | + expect(message.length).to.equal(len); |
| 189 | + |
| 190 | + if (iter === numBufs) { |
| 191 | + done(); |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + peelEmOff(bigBuff, sliceLens); |
| 196 | + }); |
| 197 | + |
| 198 | + it('4', (done) => { |
| 199 | + const sliceLens = [28, 26, 25, 26, 24, 21, 23, 26, 17]; |
| 200 | + const numBufs = sliceLens.length; |
| 201 | + const bufArr = new Array(numBufs).fill(serBufData); |
| 202 | + let bigBuff = Buffer.concat(bufArr); |
| 203 | + |
| 204 | + let iter = 0; |
| 205 | + deserializeStream.on('message', (message) => { |
| 206 | + ++iter; |
| 207 | + expect(message.length).to.equal(len); |
| 208 | + |
| 209 | + if (iter === numBufs) { |
| 210 | + done(); |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + peelEmOff(bigBuff, sliceLens); |
| 215 | + }); |
| 216 | + |
| 217 | + it('services', (done) => { |
| 218 | + const sliceLens = [28, 26, 25, 26, 24, 21, 23, 26, 17]; |
| 219 | + const numBufs = sliceLens.length; |
| 220 | + const len = 19; |
| 221 | + let iter = 0; |
| 222 | + deserializeStream.on('message', (message) => { |
| 223 | + ++iter; |
| 224 | + expect(message.length).to.equal(len); |
| 225 | + |
| 226 | + if (iter === numBufs) { |
| 227 | + deserializeStream._deserializeServiceResp = false; |
| 228 | + done(); |
| 229 | + } |
| 230 | + }); |
| 231 | + |
| 232 | + const buf = new Buffer(len + 5).fill(0); |
| 233 | + buf[0] = 1; |
| 234 | + buf.writeUInt32LE(19, 1); |
| 235 | + |
| 236 | + const bufArr = new Array(numBufs).fill(buf); |
| 237 | + let bigBuff = Buffer.concat(bufArr); |
| 238 | + |
| 239 | + deserializeStream.setServiceRespDeserialize(); |
| 240 | + peelEmOff(bigBuff, sliceLens); |
| 241 | + }); |
| 242 | + }); |
| 243 | + |
| 244 | + // TODO: spend more time writing unit tests and get rid of randomness |
| 245 | + |
| 246 | + it('random split stream', (done) => { |
| 247 | + const len = 20; |
| 248 | + const bufData = new Buffer(len).fill(0); |
| 249 | + const serBufData = serUtils.Serialize(bufData); |
| 250 | + |
| 251 | + const numBufs = 10000; |
| 252 | + const bufArr = new Array(numBufs).fill(serBufData); |
| 253 | + let bigBuff = Buffer.concat(bufArr); |
| 254 | + |
| 255 | + let iter = 0; |
| 256 | + deserializeStream.on('message', (message) => { |
| 257 | + ++iter; |
| 258 | + expect(message.length).to.equal(len); |
| 259 | + |
| 260 | + if (iter === numBufs) { |
| 261 | + done(); |
| 262 | + } |
| 263 | + }); |
| 264 | + |
| 265 | + const rand = () => { return Math.round(Math.random() * 2 * (len + 4)); } |
| 266 | + |
| 267 | + while (bigBuff.length > 0) { |
| 268 | + const pos = Math.min(rand(), bigBuff.length); |
| 269 | + const slice = bigBuff.slice(0, pos); |
| 270 | + deserializeStream.write(slice); |
| 271 | + bigBuff = bigBuff.slice(pos); |
| 272 | + } |
| 273 | + }); |
| 274 | + |
| 275 | + it('random split stream 2', (done) => { |
| 276 | + const len = 20; |
| 277 | + const bufData = new Buffer(len).fill(0); |
| 278 | + const serBufData = serUtils.Serialize(bufData); |
| 279 | + |
| 280 | + const numBufs = 10000; |
| 281 | + const bufArr = new Array(numBufs).fill(serBufData); |
| 282 | + let bigBuff = Buffer.concat(bufArr); |
| 283 | + |
| 284 | + let iter = 0; |
| 285 | + deserializeStream.on('message', (message) => { |
| 286 | + ++iter; |
| 287 | + expect(message.length).to.equal(len); |
| 288 | + |
| 289 | + if (iter === numBufs) { |
| 290 | + done(); |
| 291 | + } |
| 292 | + }); |
| 293 | + |
| 294 | + // return a random integer between 20 and 28 |
| 295 | + const rand = () => { return Math.round(Math.random() * 8) + len; } |
| 296 | + |
| 297 | + while (bigBuff.length > 0) { |
| 298 | + const pos = Math.min(rand(), bigBuff.length); |
| 299 | + const slice = bigBuff.slice(0, pos); |
| 300 | + deserializeStream.write(slice); |
| 301 | + bigBuff = bigBuff.slice(pos); |
| 302 | + } |
| 303 | + }); |
| 304 | + |
| 305 | + it('random split services', (done) => { |
| 306 | + const sliceLens = [28, 26, 25, 26, 24, 21, 23, 26, 17]; |
| 307 | + const numBufs = 10000; |
| 308 | + |
| 309 | + const len = 19; |
| 310 | + let iter = 0; |
| 311 | + deserializeStream.on('message', (message) => { |
| 312 | + ++iter; |
| 313 | + expect(message.length).to.equal(len); |
| 314 | + |
| 315 | + if (iter === numBufs) { |
| 316 | + deserializeStream._deserializeServiceResp = false; |
| 317 | + done(); |
| 318 | + } |
| 319 | + }); |
| 320 | + |
| 321 | + const buf = new Buffer(len + 5).fill(0); |
| 322 | + buf[0] = 1; |
| 323 | + buf.writeUInt32LE(19, 1); |
| 324 | + |
| 325 | + const bufArr = new Array(numBufs).fill(buf); |
| 326 | + let bigBuff = Buffer.concat(bufArr); |
| 327 | + |
| 328 | + deserializeStream.setServiceRespDeserialize(); |
| 329 | + |
| 330 | + const rand = () => { return Math.round(Math.random() * 8) + len; } |
| 331 | + |
| 332 | + while (bigBuff.length > 0) { |
| 333 | + const pos = Math.min(rand(), bigBuff.length); |
| 334 | + const slice = bigBuff.slice(0, pos); |
| 335 | + deserializeStream.write(slice); |
| 336 | + bigBuff = bigBuff.slice(pos); |
| 337 | + } |
| 338 | + }); |
| 339 | + |
| 340 | + afterEach(() => { |
| 341 | + deserializeStream.removeAllListeners('message'); |
| 342 | + }); |
| 343 | +}); |
0 commit comments