summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-client-destroy.js
diff options
context:
space:
mode:
authorTrivikram Kamat <trivikr.dev@gmail.com>2017-10-02 22:13:48 -0700
committerMatteo Collina <hello@matteocollina.com>2017-10-07 09:46:58 -0700
commitd6031bc1c6f6b5b630790cf916bebcfb51a948b1 (patch)
tree48dbd25bd60116b866808d36977701f3432107d1 /test/parallel/test-http2-client-destroy.js
parentce4903426dac9b0c851cf8dba2511e3eebd5c352 (diff)
downloadnode-new-d6031bc1c6f6b5b630790cf916bebcfb51a948b1.tar.gz
test: http2 client destroy tests in one file
Refs: #14985 PR-URL: https://github.com/nodejs/node/pull/15749 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'test/parallel/test-http2-client-destroy.js')
-rw-r--r--test/parallel/test-http2-client-destroy.js164
1 files changed, 128 insertions, 36 deletions
diff --git a/test/parallel/test-http2-client-destroy.js b/test/parallel/test-http2-client-destroy.js
index 698f752b10..94f66930aa 100644
--- a/test/parallel/test-http2-client-destroy.js
+++ b/test/parallel/test-http2-client-destroy.js
@@ -6,50 +6,142 @@ if (!common.hasCrypto)
const assert = require('assert');
const h2 = require('http2');
-const server = h2.createServer();
-server.listen(0);
+{
+ const server = h2.createServer();
+ server.listen(
+ 0,
+ common.mustCall(() => {
+ const destroyCallbacks = [
+ (client) => client.destroy(),
+ (client) => client.socket.destroy()
+ ];
-server.on('listening', common.mustCall(function() {
- const port = this.address().port;
+ let remaining = destroyCallbacks.length;
- const destroyCallbacks = [
- (client) => client.destroy(),
- (client) => client.socket.destroy()
- ];
+ destroyCallbacks.forEach((destroyCallback) => {
+ const client = h2.connect(`http://localhost:${server.address().port}`);
+ client.on(
+ 'connect',
+ common.mustCall(() => {
+ const socket = client.socket;
- let remaining = destroyCallbacks.length;
+ assert(client.socket, 'client session has associated socket');
+ assert(
+ !client.destroyed,
+ 'client has not been destroyed before destroy is called'
+ );
+ assert(
+ !socket.destroyed,
+ 'socket has not been destroyed before destroy is called'
+ );
- destroyCallbacks.forEach((destroyCallback) => {
- const client = h2.connect(`http://localhost:${port}`);
- client.on('connect', common.mustCall(() => {
- const socket = client.socket;
+ // Ensure that 'close' event is emitted
+ client.on('close', common.mustCall());
- assert(client.socket, 'client session has associated socket');
- assert(!client.destroyed,
- 'client has not been destroyed before destroy is called');
- assert(!socket.destroyed,
- 'socket has not been destroyed before destroy is called');
+ destroyCallback(client);
- // Ensure that 'close' event is emitted
- client.on('close', common.mustCall());
+ assert(
+ !client.socket,
+ 'client.socket undefined after destroy is called'
+ );
- destroyCallback(client);
+ // Must must be closed
+ client.on(
+ 'close',
+ common.mustCall(() => {
+ assert(client.destroyed);
+ })
+ );
- assert(!client.socket, 'client.socket undefined after destroy is called');
+ // socket will close on process.nextTick
+ socket.on(
+ 'close',
+ common.mustCall(() => {
+ assert(socket.destroyed);
+ })
+ );
- // Must must be closed
- client.on('close', common.mustCall(() => {
- assert(client.destroyed);
- }));
+ if (--remaining === 0) {
+ server.close();
+ }
+ })
+ );
+ });
+ })
+ );
+}
- // socket will close on process.nextTick
- socket.on('close', common.mustCall(() => {
- assert(socket.destroyed);
- }));
+// test destroy before connect
+{
+ const server = h2.createServer();
+ server.listen(
+ 0,
+ common.mustCall(() => {
+ const client = h2.connect(`http://localhost:${server.address().port}`);
- if (--remaining === 0) {
- server.close();
- }
- }));
- });
-}));
+ const req = client.request({ ':path': '/' });
+ client.destroy();
+
+ req.on('response', common.mustNotCall());
+ req.resume();
+ req.on(
+ 'end',
+ common.mustCall(() => {
+ server.close();
+ })
+ );
+ req.end();
+ })
+ );
+}
+
+// test destroy before request
+{
+ const server = h2.createServer();
+ server.listen(
+ 0,
+ common.mustCall(() => {
+ const client = h2.connect(`http://localhost:${server.address().port}`);
+ client.destroy();
+
+ assert.throws(
+ () => client.request({ ':path': '/' }),
+ common.expectsError({
+ code: 'ERR_HTTP2_INVALID_SESSION',
+ message: 'The session has been destroyed'
+ })
+ );
+
+ server.close();
+ })
+ );
+}
+
+// test destroy before goaway
+{
+ const server = h2.createServer();
+ server.on(
+ 'stream',
+ common.mustCall((stream) => {
+ stream.on('error', common.mustCall());
+ stream.session.shutdown();
+ })
+ );
+ server.listen(
+ 0,
+ common.mustCall(() => {
+ const client = h2.connect(`http://localhost:${server.address().port}`);
+
+ client.on(
+ 'goaway',
+ common.mustCall(() => {
+ // We ought to be able to destroy the client in here without an error
+ server.close();
+ client.destroy();
+ })
+ );
+
+ client.request();
+ })
+ );
+}