Skip to content

Commit 556923c

Browse files
committed
quic: Move helper functions to separate files
1 parent 9cddce0 commit 556923c

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

test/common/quic/test-helpers.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// start demo data
2+
// taken from @fails-components/webtransport tests
3+
// by the original author
4+
function createBytesChunk(length) {
5+
const workArray = new Array(length / 2);
6+
for (let i = 0; i < length / 4; i++) {
7+
workArray[2 * i + 1] = length % 0xffff;
8+
workArray[2 * i] = i;
9+
}
10+
const helper = new Uint16Array(workArray);
11+
const toreturn = new Uint8Array(
12+
helper.buffer,
13+
helper.byteOffset,
14+
helper.byteLength
15+
);
16+
return toreturn;
17+
}
18+
19+
// The number in the comments, help you identify the chunk, as it is the length first two bytes
20+
// this is helpful, when debugging buffer passing
21+
export const KNOWN_BYTES_LONG = [
22+
createBytesChunk(60000), // 96, 234
23+
createBytesChunk(12), // 0, 12
24+
createBytesChunk(50000), // 195, 80
25+
createBytesChunk(1600).buffer, // 6, 64 we use buffer here to increae test coverage
26+
createBytesChunk(20000), // 78, 32
27+
createBytesChunk(30000), // 117, 48
28+
];
29+
30+
// end demo data
31+
32+
export function uint8concat(arrays) {
33+
const length = arrays.reduce((acc, curr) => acc + curr.length, 0);
34+
const result = new Uint8Array(length);
35+
let pos = 0;
36+
let array = 0;
37+
while (pos < length) {
38+
const curArr = arrays[array];
39+
const curLen = curArr.byteLength;
40+
const dest = new Uint8Array(result.buffer, result.byteOffset + pos, curLen);
41+
dest.set(curArr);
42+
array++;
43+
pos += curArr.byteLength;
44+
}
45+
}

test/parallel/test-quic-server-to-client-unidirectional.mjs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { hasQuic, skip, mustCall } from '../common/index.mjs';
44
import { ok, strictEqual, deepStrictEqual } from 'node:assert';
55
import { readKey } from '../common/fixtures.mjs';
6+
import { KNOWN_BYTES_LONG, uint8concat } from '../common/quic/test-helpers.mjs';
67
import { TransformStream } from 'node:stream/web';
78

89
if (!hasQuic) {
@@ -19,53 +20,6 @@ const certs = readKey('agent1-cert.pem');
1920
// The opened promise should resolve when the client finished reading
2021
const clientFinished = Promise.withResolvers();
2122

22-
// start demo data
23-
// FIX ME: move the following to a central place
24-
// if used in several tests
25-
// taken from @fails-components/webtransport tests
26-
// by the original author
27-
function createBytesChunk(length) {
28-
const workArray = new Array(length / 2);
29-
for (let i = 0; i < length / 4; i++) {
30-
workArray[2 * i + 1] = length % 0xffff;
31-
workArray[2 * i] = i;
32-
}
33-
const helper = new Uint16Array(workArray);
34-
const toreturn = new Uint8Array(
35-
helper.buffer,
36-
helper.byteOffset,
37-
helper.byteLength
38-
);
39-
return toreturn;
40-
}
41-
42-
// The number in the comments, help you identify the chunk, as it is the length first two bytes
43-
// this is helpful, when debugging buffer passing
44-
const KNOWN_BYTES_LONG = [
45-
createBytesChunk(60000), // 96, 234
46-
createBytesChunk(12), // 0, 12
47-
createBytesChunk(50000), // 195, 80
48-
createBytesChunk(1600).buffer, // 6, 64 we use buffer here to increae test coverage
49-
createBytesChunk(20000), // 78, 32
50-
createBytesChunk(30000), // 117, 48
51-
];
52-
53-
// end demo data
54-
55-
function uint8concat(arrays) {
56-
const length = arrays.reduce((acc, curr) => acc + curr.length, 0);
57-
const result = new Uint8Array(length);
58-
let pos = 0;
59-
let array = 0;
60-
while (pos < length) {
61-
const curArr = arrays[array];
62-
const curLen = curArr.byteLength;
63-
const dest = new Uint8Array(result.buffer, result.byteOffset + pos, curLen);
64-
dest.set(curArr);
65-
array++;
66-
pos += curArr.byteLength;
67-
}
68-
}
6923

7024
const serverEndpoint = await listen(async (serverSession) => {
7125
await serverSession.opened;

0 commit comments

Comments
 (0)