Skip to content

Commit

Permalink
[test] Use stream.getDefaultHighWaterMark() when available
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Dec 26, 2023
1 parent 01ba54e commit 391ddf3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
18 changes: 14 additions & 4 deletions test/create-websocket-stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
const assert = require('assert');
const EventEmitter = require('events');
const { createServer } = require('http');
const { Duplex } = require('stream');
const { Duplex, getDefaultHighWaterMark } = require('stream');
const { randomBytes } = require('crypto');

const createWebSocketStream = require('../lib/stream');
const Sender = require('../lib/sender');
const WebSocket = require('..');
const { EMPTY_BUFFER } = require('../lib/constants');

const highWaterMark = getDefaultHighWaterMark
? getDefaultHighWaterMark(false)
: 16 * 1024;

describe('createWebSocketStream', () => {
it('is exposed as a property of the `WebSocket` class', () => {
assert.strictEqual(WebSocket.createWebSocketStream, createWebSocketStream);
Expand Down Expand Up @@ -445,12 +449,15 @@ describe('createWebSocketStream', () => {
};

const list = [
...Sender.frame(randomBytes(16 * 1024), { rsv1: false, ...opts }),
...Sender.frame(randomBytes(highWaterMark), {
rsv1: false,
...opts
}),
...Sender.frame(Buffer.alloc(1), { rsv1: true, ...opts })
];

// This hack is used because there is no guarantee that more than
// 16 KiB will be sent as a single TCP packet.
// `highWaterMark` bytes will be sent as a single TCP packet.
ws._socket.push(Buffer.concat(list));
});

Expand Down Expand Up @@ -494,7 +501,10 @@ describe('createWebSocketStream', () => {
};

const list = [
...Sender.frame(randomBytes(16 * 1024), { rsv1: false, ...opts }),
...Sender.frame(randomBytes(highWaterMark), {
rsv1: false,
...opts
}),
...Sender.frame(Buffer.alloc(1), { rsv1: true, ...opts })
];

Expand Down
21 changes: 15 additions & 6 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const net = require('net');
const tls = require('tls');
const os = require('os');
const fs = require('fs');
const { getDefaultHighWaterMark } = require('stream');
const { URL } = require('url');

const Sender = require('../lib/sender');
Expand All @@ -23,6 +24,10 @@ const {
} = require('../lib/event-target');
const { EMPTY_BUFFER, GUID, kListener, NOOP } = require('../lib/constants');

const highWaterMark = getDefaultHighWaterMark
? getDefaultHighWaterMark(false)
: 16 * 1024;

class CustomAgent extends http.Agent {
addRequest() {}
}
Expand Down Expand Up @@ -4092,7 +4097,7 @@ describe('WebSocket', () => {
ws.terminate();
};

const payload1 = Buffer.alloc(15 * 1024);
const payload1 = Buffer.alloc(highWaterMark - 1024);
const payload2 = Buffer.alloc(1);

const opts = {
Expand All @@ -4107,13 +4112,17 @@ describe('WebSocket', () => {
...Sender.frame(payload2, { rsv1: true, ...opts })
];

for (let i = 0; i < 399; i++) {
for (let i = 0; i < 340; i++) {
list.push(list[list.length - 2], list[list.length - 1]);
}

const data = Buffer.concat(list);

assert.ok(data.length > highWaterMark);

// This hack is used because there is no guarantee that more than
// 16 KiB will be sent as a single TCP packet.
push.call(ws._socket, Buffer.concat(list));
// `highWaterMark` bytes will be sent as a single TCP packet.
push.call(ws._socket, data);

wss.clients
.values()
Expand All @@ -4128,8 +4137,8 @@ describe('WebSocket', () => {

ws.on('close', (code) => {
assert.strictEqual(code, 1006);
assert.strictEqual(messageLengths.length, 402);
assert.strictEqual(messageLengths[0], 15360);
assert.strictEqual(messageLengths.length, 343);
assert.strictEqual(messageLengths[0], highWaterMark - 1024);
assert.strictEqual(messageLengths[messageLengths.length - 1], 1);
wss.close(done);
});
Expand Down

0 comments on commit 391ddf3

Please sign in to comment.