mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
test(microservices): Fix maxBufferSize tests
- Use valid JSON strings in test data - Account for packet header length in buffer size calculations - Fix chunked data test to properly accumulate buffer
This commit is contained in:
@@ -16,7 +16,12 @@ describe('JsonSocket maxBufferSize', () => {
|
|||||||
|
|
||||||
it('should accept data up to default maxBufferSize', () => {
|
it('should accept data up to default maxBufferSize', () => {
|
||||||
const socket = new JsonSocket(new Socket());
|
const socket = new JsonSocket(new Socket());
|
||||||
const largeData = 'x'.repeat(DEFAULT_MAX_BUFFER_SIZE - 1);
|
// Account for header length (number + '#')
|
||||||
|
// Use a smaller size to ensure total buffer (header + data) doesn't exceed limit
|
||||||
|
// Create valid JSON string data
|
||||||
|
const headerOverhead = 20; // Approximate header size for large numbers
|
||||||
|
const dataSize = DEFAULT_MAX_BUFFER_SIZE - headerOverhead;
|
||||||
|
const largeData = '"' + 'x'.repeat(dataSize - 2) + '"'; // Valid JSON string
|
||||||
const packet = `${largeData.length}#${largeData}`;
|
const packet = `${largeData.length}#${largeData}`;
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
@@ -45,7 +50,12 @@ describe('JsonSocket maxBufferSize', () => {
|
|||||||
it('should accept data up to custom maxBufferSize', () => {
|
it('should accept data up to custom maxBufferSize', () => {
|
||||||
const customSize = 1000;
|
const customSize = 1000;
|
||||||
const socket = new JsonSocket(new Socket(), customSize);
|
const socket = new JsonSocket(new Socket(), customSize);
|
||||||
const data = 'x'.repeat(customSize - 1);
|
// Account for header length (number + '#')
|
||||||
|
// For 1000, header is "1000#" = 5 characters
|
||||||
|
const headerOverhead = 5;
|
||||||
|
const dataSize = customSize - headerOverhead;
|
||||||
|
// Create valid JSON string data
|
||||||
|
const data = '"' + 'x'.repeat(dataSize - 2) + '"'; // Valid JSON string
|
||||||
const packet = `${data.length}#${data}`;
|
const packet = `${data.length}#${data}`;
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
@@ -69,13 +79,15 @@ describe('JsonSocket maxBufferSize', () => {
|
|||||||
const socket = new JsonSocket(new Socket(), customSize);
|
const socket = new JsonSocket(new Socket(), customSize);
|
||||||
const largeData = 'x'.repeat(customSize + 100);
|
const largeData = 'x'.repeat(customSize + 100);
|
||||||
const packet = `${largeData.length}#${largeData}`;
|
const packet = `${largeData.length}#${largeData}`;
|
||||||
|
// Total buffer size will be: header length (5) + data length (1100) = 1105
|
||||||
|
const expectedBufferSize = packet.length;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socket['handleData'](packet);
|
socket['handleData'](packet);
|
||||||
expect.fail('Should have thrown MaxPacketLengthExceededException');
|
expect.fail('Should have thrown MaxPacketLengthExceededException');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(err).to.be.instanceof(MaxPacketLengthExceededException);
|
expect(err).to.be.instanceof(MaxPacketLengthExceededException);
|
||||||
expect(err.message).to.include(String(customSize + 100));
|
expect(err.message).to.include(String(expectedBufferSize));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -85,16 +97,16 @@ describe('JsonSocket maxBufferSize', () => {
|
|||||||
const customSize = 100;
|
const customSize = 100;
|
||||||
const socket = new JsonSocket(new Socket(), customSize);
|
const socket = new JsonSocket(new Socket(), customSize);
|
||||||
|
|
||||||
// Send first chunk that doesn't exceed limit
|
// Send data in chunks without a valid header delimiter
|
||||||
socket['handleData']('50#');
|
// This will accumulate in the buffer without being processed
|
||||||
socket['handleData']('x'.repeat(50));
|
// First chunk: partial header
|
||||||
|
socket['handleData']('50');
|
||||||
|
|
||||||
// Send second chunk that causes total to exceed limit
|
// Second chunk: more data that accumulates beyond limit
|
||||||
|
// Buffer now has "50" (2 chars), send enough to exceed customSize
|
||||||
const exceedingData = 'x'.repeat(customSize);
|
const exceedingData = 'x'.repeat(customSize);
|
||||||
socket['handleData'](exceedingData);
|
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
socket['handleData']('more data');
|
socket['handleData'](exceedingData);
|
||||||
}).to.throw(MaxPacketLengthExceededException);
|
}).to.throw(MaxPacketLengthExceededException);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -164,7 +176,12 @@ describe('JsonSocket maxBufferSize', () => {
|
|||||||
const socket = new JsonSocket(new Socket(), veryLargeSize);
|
const socket = new JsonSocket(new Socket(), veryLargeSize);
|
||||||
expect(socket['maxBufferSize']).to.equal(veryLargeSize);
|
expect(socket['maxBufferSize']).to.equal(veryLargeSize);
|
||||||
|
|
||||||
const data = 'x'.repeat(veryLargeSize - 1);
|
// Account for header length (number + '#')
|
||||||
|
// For 10MB, header is approximately "10485760#" = 10 characters
|
||||||
|
const headerOverhead = 20; // Safe overhead for large numbers
|
||||||
|
const dataSize = veryLargeSize - headerOverhead;
|
||||||
|
// Create valid JSON string data
|
||||||
|
const data = '"' + 'x'.repeat(dataSize - 2) + '"'; // Valid JSON string
|
||||||
const packet = `${data.length}#${data}`;
|
const packet = `${data.length}#${data}`;
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
@@ -175,7 +192,12 @@ describe('JsonSocket maxBufferSize', () => {
|
|||||||
it('should handle maxBufferSize exactly at the limit', () => {
|
it('should handle maxBufferSize exactly at the limit', () => {
|
||||||
const customSize = 100;
|
const customSize = 100;
|
||||||
const socket = new JsonSocket(new Socket(), customSize);
|
const socket = new JsonSocket(new Socket(), customSize);
|
||||||
const data = 'x'.repeat(customSize);
|
// Account for header: "100#" = 4 characters
|
||||||
|
// So data can be 100 - 4 = 96 characters to stay at limit
|
||||||
|
const headerOverhead = 4;
|
||||||
|
const dataSize = customSize - headerOverhead;
|
||||||
|
// Create valid JSON string data
|
||||||
|
const data = '"' + 'x'.repeat(dataSize - 2) + '"'; // Valid JSON string
|
||||||
const packet = `${data.length}#${data}`;
|
const packet = `${data.length}#${data}`;
|
||||||
|
|
||||||
// Should not throw when exactly at limit
|
// Should not throw when exactly at limit
|
||||||
|
|||||||
Reference in New Issue
Block a user