Skip to content

Commit f39c792

Browse files
trivikrMylesBorins
authored andcommitted
test: http2 client destroy tests in one file
Refs: #14985 Backport-PR-URL: #16070 PR-URL: #15749 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 21a8a82 commit f39c792

File tree

5 files changed

+129
-126
lines changed

5 files changed

+129
-126
lines changed

lib/internal/http2/core.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,6 @@ function submitShutdown(options) {
670670

671671
function finishSessionDestroy(self, socket) {
672672
const state = self[kState];
673-
if (state.destroyed)
674-
return;
675673

676674
if (!socket.destroyed)
677675
socket.destroy();
@@ -954,6 +952,7 @@ class Http2Session extends EventEmitter {
954952
return;
955953
debug(`[${sessionName(this[kType])}] destroying nghttp2session`);
956954
state.destroying = true;
955+
state.destroyed = false;
957956

958957
// Unenroll the timer
959958
this.setTimeout(0, sessionOnTimeout);
@@ -968,9 +967,6 @@ class Http2Session extends EventEmitter {
968967
delete this[kSocket];
969968
delete this[kServer];
970969

971-
state.destroyed = false;
972-
state.destroying = true;
973-
974970
if (this[kHandle] !== undefined)
975971
this[kHandle].destroying();
976972

test/parallel/test-http2-client-destroy-before-connect.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/parallel/test-http2-client-destroy-before-request.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/parallel/test-http2-client-destroy-goaway.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/parallel/test-http2-client-destroy.js

Lines changed: 128 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,142 @@ if (!common.hasCrypto)
77
const assert = require('assert');
88
const h2 = require('http2');
99

10-
const server = h2.createServer();
11-
server.listen(0);
10+
{
11+
const server = h2.createServer();
12+
server.listen(
13+
0,
14+
common.mustCall(() => {
15+
const destroyCallbacks = [
16+
(client) => client.destroy(),
17+
(client) => client.socket.destroy()
18+
];
1219

13-
server.on('listening', common.mustCall(function() {
14-
const port = this.address().port;
20+
let remaining = destroyCallbacks.length;
1521

16-
const destroyCallbacks = [
17-
(client) => client.destroy(),
18-
(client) => client.socket.destroy()
19-
];
22+
destroyCallbacks.forEach((destroyCallback) => {
23+
const client = h2.connect(`http://localhost:${server.address().port}`);
24+
client.on(
25+
'connect',
26+
common.mustCall(() => {
27+
const socket = client.socket;
2028

21-
let remaining = destroyCallbacks.length;
29+
assert(client.socket, 'client session has associated socket');
30+
assert(
31+
!client.destroyed,
32+
'client has not been destroyed before destroy is called'
33+
);
34+
assert(
35+
!socket.destroyed,
36+
'socket has not been destroyed before destroy is called'
37+
);
2238

23-
destroyCallbacks.forEach((destroyCallback) => {
24-
const client = h2.connect(`http://localhost:${port}`);
25-
client.on('connect', common.mustCall(() => {
26-
const socket = client.socket;
39+
// Ensure that 'close' event is emitted
40+
client.on('close', common.mustCall());
2741

28-
assert(client.socket, 'client session has associated socket');
29-
assert(!client.destroyed,
30-
'client has not been destroyed before destroy is called');
31-
assert(!socket.destroyed,
32-
'socket has not been destroyed before destroy is called');
42+
destroyCallback(client);
3343

34-
// Ensure that 'close' event is emitted
35-
client.on('close', common.mustCall());
44+
assert(
45+
!client.socket,
46+
'client.socket undefined after destroy is called'
47+
);
3648

37-
destroyCallback(client);
49+
// Must must be closed
50+
client.on(
51+
'close',
52+
common.mustCall(() => {
53+
assert(client.destroyed);
54+
})
55+
);
3856

39-
assert(!client.socket, 'client.socket undefined after destroy is called');
57+
// socket will close on process.nextTick
58+
socket.on(
59+
'close',
60+
common.mustCall(() => {
61+
assert(socket.destroyed);
62+
})
63+
);
4064

41-
// Must must be closed
42-
client.on('close', common.mustCall(() => {
43-
assert(client.destroyed);
44-
}));
65+
if (--remaining === 0) {
66+
server.close();
67+
}
68+
})
69+
);
70+
});
71+
})
72+
);
73+
}
4574

46-
// socket will close on process.nextTick
47-
socket.on('close', common.mustCall(() => {
48-
assert(socket.destroyed);
49-
}));
75+
// test destroy before connect
76+
{
77+
const server = h2.createServer();
78+
server.listen(
79+
0,
80+
common.mustCall(() => {
81+
const client = h2.connect(`http://localhost:${server.address().port}`);
5082

51-
if (--remaining === 0) {
52-
server.close();
53-
}
54-
}));
55-
});
56-
}));
83+
const req = client.request({ ':path': '/' });
84+
client.destroy();
85+
86+
req.on('response', common.mustNotCall());
87+
req.resume();
88+
req.on(
89+
'end',
90+
common.mustCall(() => {
91+
server.close();
92+
})
93+
);
94+
req.end();
95+
})
96+
);
97+
}
98+
99+
// test destroy before request
100+
{
101+
const server = h2.createServer();
102+
server.listen(
103+
0,
104+
common.mustCall(() => {
105+
const client = h2.connect(`http://localhost:${server.address().port}`);
106+
client.destroy();
107+
108+
assert.throws(
109+
() => client.request({ ':path': '/' }),
110+
common.expectsError({
111+
code: 'ERR_HTTP2_INVALID_SESSION',
112+
message: 'The session has been destroyed'
113+
})
114+
);
115+
116+
server.close();
117+
})
118+
);
119+
}
120+
121+
// test destroy before goaway
122+
{
123+
const server = h2.createServer();
124+
server.on(
125+
'stream',
126+
common.mustCall((stream) => {
127+
stream.on('error', common.mustCall());
128+
stream.session.shutdown();
129+
})
130+
);
131+
server.listen(
132+
0,
133+
common.mustCall(() => {
134+
const client = h2.connect(`http://localhost:${server.address().port}`);
135+
136+
client.on(
137+
'goaway',
138+
common.mustCall(() => {
139+
// We ought to be able to destroy the client in here without an error
140+
server.close();
141+
client.destroy();
142+
})
143+
);
144+
145+
client.request();
146+
})
147+
);
148+
}

0 commit comments

Comments
 (0)