Skip to content

Commit

Permalink
quic: refactor/improve/document QuicSocket listening event
Browse files Browse the repository at this point in the history
PR-URL: #34247
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
jasnell committed Jul 9, 2020
1 parent afc9390 commit 94372b1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
11 changes: 11 additions & 0 deletions doc/api/quic.md
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,17 @@ Emitted before the `'close'` event if the `QuicSocket` was destroyed with an

The `'error'` event will not be emitted multiple times.

#### Event: `'listening'`
<!-- YAML
added: REPLACEME
-->

Emitted after `quicsocket.listen()` is called and the `QuicSocket` has started
listening for incoming `QuicServerSession`s. The callback is invoked with
no arguments.

The `'listening'` event will not be emitted multiple times.

#### Event: `'ready'`
<!-- YAML
added: REPLACEME
Expand Down
8 changes: 7 additions & 1 deletion lib/internal/quic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,13 @@ class QuicSocket extends EventEmitter {
port,
state.alpn,
options);
process.nextTick(emit.bind(this, 'listening'));
process.nextTick(() => {
try {
this.emit('listening');
} catch (error) {
this.destroy(error);
}
});
}

// When the QuicSocket listen() function is called, the first step is to bind
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-quic-server-listening-event-error-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Flags: --no-warnings
'use strict';

const common = require('../common');
if (!common.hasQuic)
common.skip('missing quic');

const assert = require('assert');
const {
key,
cert,
ca,
} = require('../common/quic');

const { createQuicSocket } = require('net');

const options = { key, cert, ca, alpn: 'zzz' };

const server = createQuicSocket({ server: options });

server.on('session', common.mustNotCall());

server.listen();

server.on('error', common.mustCall((error) => {
assert.strictEqual(error.message, 'boom');
}));

server.on('ready', common.mustCall());

server.on('listening', common.mustCall(async () => {
throw new Error('boom');
}));
33 changes: 33 additions & 0 deletions test/parallel/test-quic-server-listening-event-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Flags: --no-warnings
'use strict';

const common = require('../common');
if (!common.hasQuic)
common.skip('missing quic');

const assert = require('assert');
const {
key,
cert,
ca,
} = require('../common/quic');

const { createQuicSocket } = require('net');

const options = { key, cert, ca, alpn: 'zzz' };

const server = createQuicSocket({ server: options });

server.on('session', common.mustNotCall());

server.listen();

server.on('error', common.mustCall((error) => {
assert.strictEqual(error.message, 'boom');
}));

server.on('ready', common.mustCall());

server.on('listening', common.mustCall(() => {
throw new Error('boom');
}));

0 comments on commit 94372b1

Please sign in to comment.