Skip to content

Commit b5dfd00

Browse files
committed
quic: Fix test util function and use custom uint8array comparison
1 parent 1e684ca commit b5dfd00

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

test/common/quic/test-helpers.mjs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import assert from "node:assert";
12
// start demo data
23
// taken from @fails-components/webtransport tests
34
// by the original author
45
function createBytesChunk(length) {
6+
assert(length % 4 === 0);
57
const workArray = new Array(length / 2);
68
for (let i = 0; i < length / 4; i++) {
79
workArray[2 * i + 1] = length % 0xffff;
@@ -30,16 +32,24 @@ export const KNOWN_BYTES_LONG = [
3032
// end demo data
3133

3234
export function uint8concat(arrays) {
33-
const length = arrays.reduce((acc, curr) => acc + curr.length, 0);
35+
const length = arrays.reduce((acc, curr) => acc + curr.byteLength, 0);
3436
const result = new Uint8Array(length);
3537
let pos = 0;
3638
let array = 0;
3739
while (pos < length) {
3840
const curArr = arrays[array];
3941
const curLen = curArr.byteLength;
4042
const dest = new Uint8Array(result.buffer, result.byteOffset + pos, curLen);
41-
dest.set(curArr);
43+
dest.set(new Uint8Array(curArr));
4244
array++;
4345
pos += curArr.byteLength;
4446
}
47+
return result;
48+
}
49+
50+
export function equalUint8Arrays(arr1, arr2) {
51+
assert.ok(arr1.byteLength === arr2.byteLength, "Array content differs in length " + arr1.byteLength + " vs. " + arr2.byteLength);
52+
for (let i = 0; i < arr1.byteLength; i++) {
53+
assert.ok(arr1[i] === arr2[i], "Array content differs at " + i + " arr1: " + arr1[i] + " arr2: " + arr2[i]);
54+
}
4555
}

test/parallel/test-quic-client-to-server-bidirectional-echo.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Flags: --experimental-quic --no-warnings
22

33
import { hasQuic, skip, mustCall } from '../common/index.mjs';
4-
import { ok, strictEqual, deepStrictEqual } from 'node:assert';
4+
import { ok, strictEqual } from 'node:assert';
55
import { readKey } from '../common/fixtures.mjs';
6-
import { KNOWN_BYTES_LONG, uint8concat } from '../common/quic/test-helpers.mjs';
6+
import { KNOWN_BYTES_LONG, uint8concat, equalUint8Arrays } from '../common/quic/test-helpers.mjs';
77
import { TransformStream } from 'node:stream/web';
88

99
if (!hasQuic) {
@@ -73,7 +73,7 @@ const readFromStream = async () => {
7373
if (done) break;
7474
}
7575
// Now compare what we got
76-
deepStrictEqual(uint8concat(KNOWN_BYTES_LONG), uint8concat(readChunks));
76+
equalUint8Arrays(uint8concat(KNOWN_BYTES_LONG), uint8concat(readChunks));
7777
};
7878

7979
await Promise.all([writeToStream(), readFromStream()]);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Flags: --experimental-quic --no-warnings
22

33
import { hasQuic, skip, mustCall } from '../common/index.mjs';
4-
import { ok, strictEqual, deepStrictEqual } from 'node:assert';
4+
import { ok, strictEqual } from 'node:assert';
55
import { readKey } from '../common/fixtures.mjs';
6-
import { KNOWN_BYTES_LONG, uint8concat } from '../common/quic/test-helpers.mjs';
6+
import { KNOWN_BYTES_LONG, uint8concat, equalUint8Arrays } from '../common/quic/test-helpers.mjs';
77
import { TransformStream } from 'node:stream/web';
88

99
if (!hasQuic) {
@@ -39,7 +39,7 @@ const serverEndpoint = await listen(async (serverSession) => {
3939
// ignore
4040
});
4141
// Now compare what we got
42-
deepStrictEqual(uint8concat(KNOWN_BYTES_LONG), uint8concat(readChunks));
42+
equalUint8Arrays(uint8concat(KNOWN_BYTES_LONG), uint8concat(readChunks));
4343
serverFinished.resolve();
4444
}, 1);
4545

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Flags: --experimental-quic --no-warnings
22

33
import { hasQuic, skip, mustCall } from '../common/index.mjs';
4-
import { ok, strictEqual, deepStrictEqual } from 'node:assert';
4+
import { ok, strictEqual } from 'node:assert';
55
import { readKey } from '../common/fixtures.mjs';
6-
import { KNOWN_BYTES_LONG, uint8concat } from '../common/quic/test-helpers.mjs';
6+
import { KNOWN_BYTES_LONG, uint8concat, equalUint8Arrays } from '../common/quic/test-helpers.mjs';
77
import { TransformStream } from 'node:stream/web';
88

99
if (!hasQuic) {
@@ -64,7 +64,7 @@ clientSession.onstream = mustCall(async (stream) => {
6464
// ignore
6565
});
6666
// Now compare what we got
67-
deepStrictEqual(uint8concat(KNOWN_BYTES_LONG), uint8concat(readChunks));
67+
equalUint8Arrays(uint8concat(KNOWN_BYTES_LONG), uint8concat(readChunks));
6868
clientFinished.resolve();
6969
}, 1);
7070

0 commit comments

Comments
 (0)