node/test/parallel/test-quic-quicstream-destroy.js
James M Snell 086c916997 quic: extensive refactoring of QuicStream lifecycle
This one was a bit of a rabbit hole... but, with this set of
changes, `QuicStream` should now work with autoDestroy, supports
a promisified `close()`, and fixes a number of other internal
bugs that were spotted trying to get it to work.

PR-URL: https://github.com/nodejs/node/pull/34351
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2020-07-23 06:52:53 -07:00

60 lines
1.5 KiB
JavaScript

// Flags: --no-warnings
'use strict';
// Test that destroying a QuicStream immediately and synchronously
// after creation does not crash the process and closes the streams
// abruptly on both ends of the connection.
const common = require('../common');
if (!common.hasQuic)
common.skip('missing quic');
const assert = require('assert');
const { once } = require('events');
const { key, cert, ca } = require('../common/quic');
const { createQuicSocket } = require('net');
const options = { key, cert, ca, alpn: 'zzz' };
const client = createQuicSocket({ client: options });
const server = createQuicSocket({ server: options });
(async function() {
server.on('session', common.mustCall((session) => {
session.on('stream', common.mustCall((stream) => {
stream.destroy();
stream.on('close', common.mustCall());
stream.on('error', common.mustNotCall());
assert(stream.destroyed);
}));
}));
await server.listen();
const req = await client.connect({
address: common.localhostIPv4,
port: server.endpoints[0].address.port
});
const stream = await req.openStream();
stream.end('foo');
// Do not explicitly end the stream here.
stream.resume();
stream.on('end', common.mustCall());
stream.on('close', common.mustCall(() => {
assert(stream.destroyed);
client.close();
server.close();
}));
req.on('close', common.mustCall());
await Promise.all([
once(server, 'close'),
once(client, 'close')
]);
})().then(common.mustCall());