summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-client-destroy.js
blob: c407ef1f5084663602d76efb2eea4e2be31a69a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Flags: --expose-internals

'use strict';

const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');
const { kSocket } = require('internal/http2/util');
const Countdown = require('../common/countdown');
const { getEventListeners } = require('events');
{
  const server = h2.createServer();
  server.listen(0, common.mustCall(() => {
    const destroyCallbacks = [
      (client) => client.destroy(),
      (client) => client[kSocket].destroy(),
    ];

    const countdown = new Countdown(destroyCallbacks.length, () => {
      server.close();
    });

    destroyCallbacks.forEach((destroyCallback) => {
      const client = h2.connect(`http://localhost:${server.address().port}`);
      client.on('connect', common.mustCall(() => {
        const socket = client[kSocket];

        assert(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);

        client.on('close', common.mustCall(() => {
          assert(client.destroyed);
        }));

        countdown.dec();
      }));
    });
  }));
}

// Test destroy before client operations
{
  const server = h2.createServer();
  server.listen(0, common.mustCall(() => {
    const client = h2.connect(`http://localhost:${server.address().port}`);
    const socket = client[kSocket];
    socket.on('close', common.mustCall(() => {
      assert(socket.destroyed);
    }));

    const req = client.request();
    req.on('error', common.expectsError({
      code: 'ERR_HTTP2_STREAM_CANCEL',
      name: 'Error',
      message: 'The pending stream has been canceled'
    }));

    client.destroy();

    req.on('response', common.mustNotCall());

    const sessionError = {
      name: 'Error',
      code: 'ERR_HTTP2_INVALID_SESSION',
      message: 'The session has been destroyed'
    };

    assert.throws(() => client.setNextStreamID(), sessionError);
    assert.throws(() => client.setLocalWindowSize(), sessionError);
    assert.throws(() => client.ping(), sessionError);
    assert.throws(() => client.settings({}), sessionError);
    assert.throws(() => client.goaway(), sessionError);
    assert.throws(() => client.request(), sessionError);
    client.close();  // Should be a non-op at this point

    // Wait for setImmediate call from destroy() to complete
    // so that state.destroyed is set to true
    setImmediate(() => {
      assert.throws(() => client.setNextStreamID(), sessionError);
      assert.throws(() => client.setLocalWindowSize(), sessionError);
      assert.throws(() => client.ping(), sessionError);
      assert.throws(() => client.settings({}), sessionError);
      assert.throws(() => client.goaway(), sessionError);
      assert.throws(() => client.request(), sessionError);
      client.close();  // Should be a non-op at this point
    });

    req.resume();
    req.on('end', common.mustNotCall());
    req.on('close', common.mustCall(() => server.close()));
  }));
}

// Test destroy before goaway
{
  const server = h2.createServer();
  server.on('stream', common.mustCall((stream) => {
    stream.session.destroy();
  }));

  server.listen(0, common.mustCall(() => {
    const client = h2.connect(`http://localhost:${server.address().port}`);

    client.on('close', () => {
      server.close();
      // Calling destroy in here should not matter
      client.destroy();
    });

    client.request();
  }));
}

// Test destroy before connect
{
  const server = h2.createServer();
  server.on('stream', common.mustNotCall());

  server.listen(0, common.mustCall(() => {
    const client = h2.connect(`http://localhost:${server.address().port}`);

    server.on('connection', common.mustCall(() => {
      server.close();
      client.close();
    }));

    const req = client.request();
    req.destroy();
  }));
}

// Test close before connect
{
  const server = h2.createServer();

  server.on('stream', common.mustNotCall());
  server.listen(0, common.mustCall(() => {
    const client = h2.connect(`http://localhost:${server.address().port}`);
    client.on('close', common.mustCall());
    const socket = client[kSocket];
    socket.on('close', common.mustCall(() => {
      assert(socket.destroyed);
    }));

    const req = client.request();
    // Should throw goaway error
    req.on('error', common.expectsError({
      code: 'ERR_HTTP2_GOAWAY_SESSION',
      name: 'Error',
      message: 'New streams cannot be created after receiving a GOAWAY'
    }));

    client.close();
    req.resume();
    req.on('end', common.mustCall());
    req.on('close', common.mustCall(() => server.close()));
  }));
}

// Destroy with AbortSignal
{
  const server = h2.createServer();
  const controller = new AbortController();

  server.on('stream', common.mustNotCall());
  server.listen(0, common.mustCall(() => {
    const client = h2.connect(`http://localhost:${server.address().port}`);
    client.on('close', common.mustCall());

    const { signal } = controller;
    assert.strictEqual(getEventListeners(signal, 'abort').length, 0);

    client.on('error', common.mustCall(() => {
      // After underlying stream dies, signal listener detached
      assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
    }));

    const req = client.request({}, { signal });

    req.on('error', common.mustCall((err) => {
      assert.strictEqual(err.code, 'ABORT_ERR');
      assert.strictEqual(err.name, 'AbortError');
    }));
    req.on('close', common.mustCall(() => server.close()));

    assert.strictEqual(req.aborted, false);
    assert.strictEqual(req.destroyed, false);
    // Signal listener attached
    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);

    controller.abort();

    assert.strictEqual(req.aborted, false);
    assert.strictEqual(req.destroyed, true);
  }));
}
// Pass an already destroyed signal to abort immediately.
{
  const server = h2.createServer();
  const controller = new AbortController();

  server.on('stream', common.mustNotCall());
  server.listen(0, common.mustCall(() => {
    const client = h2.connect(`http://localhost:${server.address().port}`);
    client.on('close', common.mustCall());

    const { signal } = controller;
    controller.abort();

    assert.strictEqual(getEventListeners(signal, 'abort').length, 0);

    client.on('error', common.mustCall(() => {
      // After underlying stream dies, signal listener detached
      assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
    }));

    const req = client.request({}, { signal });
    // Signal already aborted, so no event listener attached.
    assert.strictEqual(getEventListeners(signal, 'abort').length, 0);

    assert.strictEqual(req.aborted, false);
    // Destroyed on same tick as request made
    assert.strictEqual(req.destroyed, true);

    req.on('error', common.mustCall((err) => {
      assert.strictEqual(err.code, 'ABORT_ERR');
      assert.strictEqual(err.name, 'AbortError');
    }));
    req.on('close', common.mustCall(() => server.close()));
  }));
}


// Destroy ClientHttpSession with AbortSignal
{
  function testH2ConnectAbort(secure) {
    const server = secure ? h2.createSecureServer() : h2.createServer();
    const controller = new AbortController();

    server.on('stream', common.mustNotCall());
    server.listen(0, common.mustCall(() => {
      const { signal } = controller;
      const protocol = secure ? 'https' : 'http';
      const client = h2.connect(`${protocol}://localhost:${server.address().port}`, {
        signal,
      });
      client.on('close', common.mustCall());
      assert.strictEqual(getEventListeners(signal, 'abort').length, 1);

      client.on('error', common.mustCall(common.mustCall((err) => {
        assert.strictEqual(err.code, 'ABORT_ERR');
        assert.strictEqual(err.name, 'AbortError');
      })));

      const req = client.request({}, {});
      assert.strictEqual(getEventListeners(signal, 'abort').length, 1);

      req.on('error', common.mustCall((err) => {
        assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_CANCEL');
        assert.strictEqual(err.name, 'Error');
        assert.strictEqual(req.aborted, false);
        assert.strictEqual(req.destroyed, true);
      }));
      req.on('close', common.mustCall(() => server.close()));

      assert.strictEqual(req.aborted, false);
      assert.strictEqual(req.destroyed, false);
      // Signal listener attached
      assert.strictEqual(getEventListeners(signal, 'abort').length, 1);

      controller.abort();
    }));
  }
  testH2ConnectAbort(false);
  testH2ConnectAbort(true);
}