Avoid internal variables in recv queue tests
Makes for more robust and realistic tests.
This commit is contained in:
parent
b298bf9e90
commit
b146de6d69
|
@ -7,73 +7,63 @@ describe('Websock', function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
describe('Receive queue methods', function () {
|
describe('Receive queue methods', function () {
|
||||||
let sock;
|
let sock, websock;
|
||||||
const RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
sock = new Websock();
|
sock = new Websock();
|
||||||
// skip init
|
websock = new FakeWebSocket();
|
||||||
sock._allocateBuffers();
|
websock._open();
|
||||||
sock._rQ.set(RQ_TEMPLATE);
|
sock.attach(websock);
|
||||||
sock._rQlen = RQ_TEMPLATE.length;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('rQpeek8', function () {
|
describe('rQpeek8', function () {
|
||||||
it('should peek at the next byte without poping it off the queue', function () {
|
it('should peek at the next byte without poping it off the queue', function () {
|
||||||
const befLen = sock._rQlen - sock._rQi;
|
websock._receiveData(new Uint8Array([0xab, 0xcd]));
|
||||||
const peek = sock.rQpeek8();
|
expect(sock.rQpeek8()).to.equal(0xab);
|
||||||
expect(sock.rQpeek8()).to.equal(peek);
|
expect(sock.rQpeek8()).to.equal(0xab);
|
||||||
expect(sock._rQlen - sock._rQi).to.equal(befLen);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('rQshift8()', function () {
|
describe('rQshift8()', function () {
|
||||||
it('should pop a single byte from the receive queue', function () {
|
it('should pop a single byte from the receive queue', function () {
|
||||||
const peek = sock.rQpeek8();
|
websock._receiveData(new Uint8Array([0xab, 0xcd]));
|
||||||
const befLen = sock._rQlen - sock._rQi;
|
expect(sock.rQshift8()).to.equal(0xab);
|
||||||
expect(sock.rQshift8()).to.equal(peek);
|
expect(sock.rQshift8()).to.equal(0xcd);
|
||||||
expect(sock._rQlen - sock._rQi).to.equal(befLen - 1);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('rQshift16()', function () {
|
describe('rQshift16()', function () {
|
||||||
it('should pop two bytes from the receive queue and return a single number', function () {
|
it('should pop two bytes from the receive queue and return a single number', function () {
|
||||||
const befLen = sock._rQlen - sock._rQi;
|
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34]));
|
||||||
const expected = (RQ_TEMPLATE[0] << 8) + RQ_TEMPLATE[1];
|
expect(sock.rQshift16()).to.equal(0xabcd);
|
||||||
expect(sock.rQshift16()).to.equal(expected);
|
expect(sock.rQshift16()).to.equal(0x1234);
|
||||||
expect(sock._rQlen - sock._rQi).to.equal(befLen - 2);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('rQshift32()', function () {
|
describe('rQshift32()', function () {
|
||||||
it('should pop four bytes from the receive queue and return a single number', function () {
|
it('should pop four bytes from the receive queue and return a single number', function () {
|
||||||
const befLen = sock._rQlen - sock._rQi;
|
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,
|
||||||
const expected = (RQ_TEMPLATE[0] << 24) +
|
0x88, 0xee, 0x11, 0x33]));
|
||||||
(RQ_TEMPLATE[1] << 16) +
|
expect(sock.rQshift32()).to.equal(0xabcd1234);
|
||||||
(RQ_TEMPLATE[2] << 8) +
|
expect(sock.rQshift32()).to.equal(0x88ee1133);
|
||||||
RQ_TEMPLATE[3];
|
|
||||||
expect(sock.rQshift32()).to.equal(expected);
|
|
||||||
expect(sock._rQlen - sock._rQi).to.equal(befLen - 4);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('rQshiftStr', function () {
|
describe('rQshiftStr', function () {
|
||||||
it('should shift the given number of bytes off of the receive queue and return a string', function () {
|
it('should shift the given number of bytes off of the receive queue and return a string', function () {
|
||||||
const befLen = sock._rQlen;
|
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,
|
||||||
const befRQi = sock._rQi;
|
0x88, 0xee, 0x11, 0x33]));
|
||||||
const shifted = sock.rQshiftStr(3);
|
expect(sock.rQshiftStr(4)).to.equal('\xab\xcd\x12\x34');
|
||||||
expect(shifted).to.be.a('string');
|
expect(sock.rQshiftStr(4)).to.equal('\x88\xee\x11\x33');
|
||||||
expect(shifted).to.equal(String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3))));
|
|
||||||
expect(sock._rQlen - sock._rQi).to.equal(befLen - 3);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to handle very large strings', function () {
|
it('should be able to handle very large strings', function () {
|
||||||
const BIG_LEN = 500000;
|
const BIG_LEN = 500000;
|
||||||
const RQ_BIG = new Uint8Array(BIG_LEN);
|
const incoming = new Uint8Array(BIG_LEN);
|
||||||
let expected = "";
|
let expected = "";
|
||||||
let letterCode = 'a'.charCodeAt(0);
|
let letterCode = 'a'.charCodeAt(0);
|
||||||
for (let i = 0; i < BIG_LEN; i++) {
|
for (let i = 0; i < BIG_LEN; i++) {
|
||||||
RQ_BIG[i] = letterCode;
|
incoming[i] = letterCode;
|
||||||
expected += String.fromCharCode(letterCode);
|
expected += String.fromCharCode(letterCode);
|
||||||
|
|
||||||
if (letterCode < 'z'.charCodeAt(0)) {
|
if (letterCode < 'z'.charCodeAt(0)) {
|
||||||
|
@ -82,90 +72,77 @@ describe('Websock', function () {
|
||||||
letterCode = 'a'.charCodeAt(0);
|
letterCode = 'a'.charCodeAt(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sock._rQ.set(RQ_BIG);
|
websock._receiveData(incoming);
|
||||||
sock._rQlen = RQ_BIG.length;
|
|
||||||
|
|
||||||
const shifted = sock.rQshiftStr(BIG_LEN);
|
const shifted = sock.rQshiftStr(BIG_LEN);
|
||||||
|
|
||||||
expect(shifted).to.be.equal(expected);
|
expect(shifted).to.be.equal(expected);
|
||||||
expect(sock._rQlen - sock._rQi).to.equal(0);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('rQshiftBytes', function () {
|
describe('rQshiftBytes', function () {
|
||||||
it('should shift the given number of bytes of the receive queue and return an array', function () {
|
it('should shift the given number of bytes of the receive queue and return an array', function () {
|
||||||
const befLen = sock._rQlen;
|
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,
|
||||||
const befRQi = sock._rQi;
|
0x88, 0xee, 0x11, 0x33]));
|
||||||
const shifted = sock.rQshiftBytes(3);
|
expect(sock.rQshiftBytes(4)).to.array.equal(new Uint8Array([0xab, 0xcd, 0x12, 0x34]));
|
||||||
expect(shifted).to.be.an.instanceof(Uint8Array);
|
expect(sock.rQshiftBytes(4)).to.array.equal(new Uint8Array([0x88, 0xee, 0x11, 0x33]));
|
||||||
expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3));
|
|
||||||
expect(sock._rQlen - sock._rQi).to.equal(befLen - 3);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a shared array if requested', function () {
|
it('should return a shared array if requested', function () {
|
||||||
const befRQi = sock._rQi;
|
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,
|
||||||
const shifted = sock.rQshiftBytes(3, false);
|
0x88, 0xee, 0x11, 0x33]));
|
||||||
expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3));
|
const bytes = sock.rQshiftBytes(4, false);
|
||||||
expect(shifted.buffer.byteLength).to.not.equal(shifted.length);
|
expect(bytes).to.array.equal(new Uint8Array([0xab, 0xcd, 0x12, 0x34]));
|
||||||
|
expect(bytes.buffer.byteLength).to.not.equal(bytes.length);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('rQpeekBytes', function () {
|
describe('rQpeekBytes', function () {
|
||||||
beforeEach(function () {
|
|
||||||
sock._rQi = 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not modify the receive queue', function () {
|
it('should not modify the receive queue', function () {
|
||||||
const befLen = sock._rQlen - sock._rQi;
|
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,
|
||||||
sock.rQpeekBytes(2);
|
0x88, 0xee, 0x11, 0x33]));
|
||||||
expect(sock._rQlen - sock._rQi).to.equal(befLen);
|
expect(sock.rQpeekBytes(4)).to.array.equal(new Uint8Array([0xab, 0xcd, 0x12, 0x34]));
|
||||||
});
|
expect(sock.rQpeekBytes(4)).to.array.equal(new Uint8Array([0xab, 0xcd, 0x12, 0x34]));
|
||||||
|
|
||||||
it('should return an array containing the requested bytes of the receive queue', function () {
|
|
||||||
const sl = sock.rQpeekBytes(2);
|
|
||||||
expect(sl).to.be.an.instanceof(Uint8Array);
|
|
||||||
expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should take the current rQi in to account', function () {
|
|
||||||
sock._rQi = 1;
|
|
||||||
expect(sock.rQpeekBytes(2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a shared array if requested', function () {
|
it('should return a shared array if requested', function () {
|
||||||
const sl = sock.rQpeekBytes(2, false);
|
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,
|
||||||
expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
|
0x88, 0xee, 0x11, 0x33]));
|
||||||
expect(sl.buffer.byteLength).to.not.equal(sl.length);
|
const bytes = sock.rQpeekBytes(4, false);
|
||||||
|
expect(bytes).to.array.equal(new Uint8Array([0xab, 0xcd, 0x12, 0x34]));
|
||||||
|
expect(bytes.buffer.byteLength).to.not.equal(bytes.length);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('rQwait', function () {
|
describe('rQwait', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
sock._rQi = 0;
|
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,
|
||||||
|
0x88, 0xee, 0x11, 0x33]));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return true if there are not enough bytes in the receive queue', function () {
|
it('should return true if there are not enough bytes in the receive queue', function () {
|
||||||
expect(sock.rQwait('hi', RQ_TEMPLATE.length + 1)).to.be.true;
|
expect(sock.rQwait('hi', 9)).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if there are enough bytes in the receive queue', function () {
|
it('should return false if there are enough bytes in the receive queue', function () {
|
||||||
expect(sock.rQwait('hi', RQ_TEMPLATE.length)).to.be.false;
|
expect(sock.rQwait('hi', 8)).to.be.false;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return true and reduce rQi by "goback" if there are not enough bytes', function () {
|
it('should return true and reduce rQi by "goback" if there are not enough bytes', function () {
|
||||||
sock._rQi = 5;
|
expect(sock.rQshift32()).to.equal(0xabcd1234);
|
||||||
expect(sock.rQwait('hi', RQ_TEMPLATE.length, 4)).to.be.true;
|
expect(sock.rQwait('hi', 8, 2)).to.be.true;
|
||||||
expect(sock._rQi).to.equal(1);
|
expect(sock.rQshift32()).to.equal(0x123488ee);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should raise an error if we try to go back more than possible', function () {
|
it('should raise an error if we try to go back more than possible', function () {
|
||||||
sock._rQi = 5;
|
expect(sock.rQshift32()).to.equal(0xabcd1234);
|
||||||
expect(() => sock.rQwait('hi', RQ_TEMPLATE.length, 6)).to.throw(Error);
|
expect(() => sock.rQwait('hi', 8, 6)).to.throw(Error);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not reduce rQi if there are enough bytes', function () {
|
it('should not reduce rQi if there are enough bytes', function () {
|
||||||
sock._rQi = 5;
|
expect(sock.rQshift32()).to.equal(0xabcd1234);
|
||||||
sock.rQwait('hi', 1, 6);
|
expect(sock.rQwait('hi', 4, 2)).to.be.false;
|
||||||
expect(sock._rQi).to.equal(5);
|
expect(sock.rQshift32()).to.equal(0x88ee1133);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue