summaryrefslogtreecommitdiff
path: root/test/parallel/test-wrap-js-stream-destroy.js
blob: 5c1ed1e7e65d109a0634bc52fb46443456a17612 (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
// Flags: --expose-internals
'use strict';

const common = require('../common');
const StreamWrap = require('internal/js_stream_socket');
const net = require('net');

// This test ensures that when we directly call `socket.destroy()` without
// having called `socket.end()` on an instance of streamWrap, it will
// still emit EOF which makes the socket on the other side emit "end" and
// "close" events, and vice versa.
{
  let port;
  const server = net.createServer((socket) => {
    socket.on('error', common.mustNotCall());
    socket.on('end', common.mustNotCall());
    socket.on('close', common.mustCall());
    socket.destroy();
  });

  server.listen(() => {
    port = server.address().port;
    createSocket();
  });

  function createSocket() {
    let streamWrap;
    const socket = new net.connect({
      port,
    }, () => {
      socket.on('error', common.mustNotCall());
      socket.on('end', common.mustCall());
      socket.on('close', common.mustCall());

      streamWrap.on('error', common.mustNotCall());
      // The "end" events will be emitted which is as same as
      // the same situation for an instance of `net.Socket` without
      // `StreamWrap`.
      streamWrap.on('end', common.mustCall());
      // Destroying a socket in the server side should emit EOF and cause
      // the corresponding client-side socket closed.
      streamWrap.on('close', common.mustCall(() => {
        server.close();
      }));
    });
    streamWrap = new StreamWrap(socket);
  }
}

// Destroy the streamWrap and test again.
{
  let port;
  const server = net.createServer((socket) => {
    socket.on('error', common.mustNotCall());
    socket.on('end', common.mustCall());
    socket.on('close', common.mustCall(() => {
      server.close();
    }));
    // Do not `socket.end()` and directly `socket.destroy()`.
  });

  server.listen(() => {
    port = server.address().port;
    createSocket();
  });

  function createSocket() {
    let streamWrap;
    const socket = new net.connect({
      port,
    }, () => {
      socket.on('error', common.mustNotCall());
      socket.on('end', common.mustNotCall());
      socket.on('close', common.mustCall());

      streamWrap.on('error', common.mustNotCall());
      streamWrap.on('end', common.mustNotCall());
      // Destroying a socket in the server side should emit EOF and cause
      // the corresponding client-side socket closed.
      streamWrap.on('close', common.mustCall());
      streamWrap.destroy();
    });
    streamWrap = new StreamWrap(socket);
  }
}

// Destroy the client socket and test again.
{
  let port;
  const server = net.createServer((socket) => {
    socket.on('error', common.mustNotCall());
    socket.on('end', common.mustCall());
    socket.on('close', common.mustCall(() => {
      server.close();
    }));
  });

  server.listen(() => {
    port = server.address().port;
    createSocket();
  });

  function createSocket() {
    let streamWrap;
    const socket = new net.connect({
      port,
    }, () => {
      socket.on('error', common.mustNotCall());
      socket.on('end', common.mustNotCall());
      socket.on('close', common.mustCall());

      streamWrap.on('error', common.mustNotCall());
      streamWrap.on('end', common.mustNotCall());
      streamWrap.on('close', common.mustCall());
      socket.destroy();
    });
    streamWrap = new StreamWrap(socket);
  }
}