@@ -7,50 +7,142 @@ if (!common.hasCrypto)
7
7
const assert = require ( 'assert' ) ;
8
8
const h2 = require ( 'http2' ) ;
9
9
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
+ ] ;
12
19
13
- server . on ( 'listening' , common . mustCall ( function ( ) {
14
- const port = this . address ( ) . port ;
20
+ let remaining = destroyCallbacks . length ;
15
21
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 ;
20
28
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
+ ) ;
22
38
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 ( ) ) ;
27
41
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 ) ;
33
43
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
+ ) ;
36
48
37
- destroyCallback ( client ) ;
49
+ // Must must be closed
50
+ client . on (
51
+ 'close' ,
52
+ common . mustCall ( ( ) => {
53
+ assert ( client . destroyed ) ;
54
+ } )
55
+ ) ;
38
56
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
+ ) ;
40
64
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
+ }
45
74
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 } ` ) ;
50
82
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