summaryrefslogtreecommitdiff
path: root/test/parallel/test-quic-quicstream-destroy.js
blob: 3f0b0bf49e7237fa8f99e79f9624ec67132d74b3 (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
// Flags: --no-warnings
'use strict';

// Test that destroying a QuicStream immediately and synchronously
// after creation does not crash the process and closes the streams
// abruptly on both ends of the connection.

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

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

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

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

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

(async function() {
  server.on('session', common.mustCall((session) => {
    session.on('stream', common.mustCall((stream) => {
      stream.destroy();
      stream.on('close', common.mustCall());
      stream.on('error', common.mustNotCall());
      assert(stream.destroyed);
    }));
  }));

  await server.listen();

  const req = await client.connect({
    address: common.localhostIPv4,
    port: server.endpoints[0].address.port
  });

  const stream = await req.openStream();
  stream.end('foo');
  // Do not explicitly end the stream here.

  stream.resume();
  stream.on('end', common.mustCall());

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

  req.on('close', common.mustCall());

  await Promise.all([
    once(server, 'close'),
    once(client, 'close')
  ]);
})().then(common.mustCall());