Skip to content

Commit fd47c96

Browse files
committed
[test] Move code block closer to where it is used
1 parent c95e695 commit fd47c96

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

test/websocket.test.js

+23-22
Original file line numberDiff line numberDiff line change
@@ -3011,27 +3011,6 @@ describe('WebSocket', () => {
30113011
});
30123012

30133013
it('consumes all received data when connection is closed (2/2)', (done) => {
3014-
const payload1 = Buffer.alloc(15 * 1024);
3015-
const payload2 = Buffer.alloc(1);
3016-
3017-
const opts = {
3018-
fin: true,
3019-
opcode: 0x02,
3020-
mask: false,
3021-
readOnly: false
3022-
};
3023-
3024-
const list = [
3025-
...Sender.frame(payload1, { rsv1: false, ...opts }),
3026-
...Sender.frame(payload2, { rsv1: true, ...opts })
3027-
];
3028-
3029-
for (let i = 0; i < 399; i++) {
3030-
list.push(list[list.length - 2], list[list.length - 1]);
3031-
}
3032-
3033-
const data = Buffer.concat(list);
3034-
30353014
const wss = new WebSocket.Server(
30363015
{
30373016
perMessageDeflate: true,
@@ -3049,15 +3028,37 @@ describe('WebSocket', () => {
30493028

30503029
const push = ws._socket.push;
30513030

3031+
// Override `ws._socket.push()` to know exactly when data is
3032+
// received and call `ws.terminate()` immediately after that without
3033+
// relying on a timer.
30523034
ws._socket.push = (data) => {
30533035
ws._socket.push = push;
30543036
ws._socket.push(data);
30553037
ws.terminate();
30563038
};
30573039

3040+
const payload1 = Buffer.alloc(15 * 1024);
3041+
const payload2 = Buffer.alloc(1);
3042+
3043+
const opts = {
3044+
fin: true,
3045+
opcode: 0x02,
3046+
mask: false,
3047+
readOnly: false
3048+
};
3049+
3050+
const list = [
3051+
...Sender.frame(payload1, { rsv1: false, ...opts }),
3052+
...Sender.frame(payload2, { rsv1: true, ...opts })
3053+
];
3054+
3055+
for (let i = 0; i < 399; i++) {
3056+
list.push(list[list.length - 2], list[list.length - 1]);
3057+
}
3058+
30583059
// This hack is used because there is no guarantee that more than
30593060
// 16 KiB will be sent as a single TCP packet.
3060-
push.call(ws._socket, data);
3061+
push.call(ws._socket, Buffer.concat(list));
30613062

30623063
wss.clients
30633064
.values()

0 commit comments

Comments
 (0)