Skip to content

test: add quic datagrams test #57119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/quic/quic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ class QuicSession {
const length = datagram.byteLength;
const offset = datagram.byteOffset;
datagram = new Uint8Array(ArrayBufferPrototypeTransfer(datagram.buffer),
length, offset);
offset, length);
}

debug(`sending datagram with ${datagram.byteLength} bytes`);
Expand Down
128 changes: 128 additions & 0 deletions test/parallel/test-quic-datagrams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Flags: --experimental-quic --no-warnings
'use strict';

const { hasQuic } = require('../common');
const {
deepStrictEqual
} = require('node:assert');
const {
describe,
it,
beforeEach,
afterEach
} = require('node:test');

const skip = !hasQuic;

const STRING_DATAGRAMS = ['hello', 'world']

const encoder = new TextEncoder()
const BUF_DATAGRAMS = STRING_DATAGRAMS.map(str => encoder.encode(str))

async function delay (ms) {
await new Promise((resolve) => {
setTimeout(() => {
resolve()
}, ms)
})
}

async function sendDatagrams(sender, receiver, datagrams) {
const allDatagramsReceived = Promise.withResolvers();
const datagramsReceived = [];

// TODO: currently only the first datagram is received, and it's received many
// times so this test will not pass
receiver.ondatagram = (datagram) => {
datagramsReceived.push(datagram);

if (datagramsReceived.length === datagrams.length) {
allDatagramsReceived.resolve();
}
};

for (const datagram of datagrams) {
await sender.sendDatagram(datagram);
}

await allDatagramsReceived.promise;

for (let i = 0; i < datagrams.length; i++) {
deepStrictEqual(datagramsReceived[i], datagrams[i]);
}
}

describe('quic datagrams', { skip }, async () => {
const { createPrivateKey } = require('node:crypto');
const fixtures = require('../common/fixtures');
const keys = createPrivateKey(fixtures.readKey('agent1-key.pem'));
const certs = fixtures.readKey('agent1-cert.pem');
let serverEndpoint
let clientSession
let serverSession

const {
listen,
connect,
} = require('node:quic');

beforeEach(async () => {
const clientConnected = Promise.withResolvers();

serverEndpoint = await listen((serverSession) => {
serverSession.opened.then(() => {
clientConnected.resolve(serverSession);
});
}, { keys, certs });

clientSession = await connect(serverEndpoint.address);
await clientSession.opened;

serverSession = await clientConnected.promise;
})

afterEach(async () => {
// TODO: calling `.close` on `serverEndpoint` without first closing both
// sessions causes a segfault - we should be able to stop the listener
// without manually closing all sessions first
await clientSession?.close();
await serverSession?.close();
await serverEndpoint?.close();
})

it('a quic client can send ArrayBufferView datagrams to a server', async () => {
// TODO: if this delay is removed, `can_send_packets` in `session.cc`
// returns false and the datagram is not sent - after the `.opened` promise
// resolves we should be able to send datagrams without additional delays
await delay(500);

await sendDatagrams(clientSession, serverSession, BUF_DATAGRAMS);
});

it('a quic server can send ArrayBufferView datagrams to a client', async () => {
// TODO: if this delay is removed, `can_send_packets` in `session.cc`
// returns false and the datagram is not sent - after the `.opened` promise
// resolves we should be able to send datagrams without additional delays
await delay(500);

await sendDatagrams(serverSession, clientSession, BUF_DATAGRAMS);
});

it('a quic client can send String datagrams to a server', async () => {
// TODO: if this delay is removed, `can_send_packets` in `session.cc`
// returns false and the datagram is not sent - after the `.opened` promise
// resolves we should be able to send datagrams without additional delays
await delay(500);

await sendDatagrams(clientSession, serverSession, STRING_DATAGRAMS);
});

it('a quic server can send String datagrams to a client', async () => {
// TODO: if this delay is removed, `can_send_packets` in `session.cc`
// returns false and the datagram is not sent - after the `.opened` promise
// resolves we should be able to send datagrams without additional delays
await delay(500);

await sendDatagrams(serverSession, clientSession, STRING_DATAGRAMS);
});
});