|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const StreamWrap = require('_stream_wrap'); |
| 5 | +const net = require('net'); |
| 6 | + |
| 7 | +// This test ensures that when we directly call `socket.destroy()` without |
| 8 | +// having called `socket.end()` on an instance of streamWrap, it will |
| 9 | +// still emit EOF which makes the socket on the other side emit "end" and |
| 10 | +// "close" events, and vice versa. |
| 11 | +{ |
| 12 | + let port; |
| 13 | + const server = net.createServer((socket) => { |
| 14 | + socket.on('error', common.mustNotCall()); |
| 15 | + socket.on('end', common.mustNotCall()); |
| 16 | + socket.on('close', common.mustCall()); |
| 17 | + socket.destroy(); |
| 18 | + }); |
| 19 | + |
| 20 | + server.listen(() => { |
| 21 | + port = server.address().port; |
| 22 | + createSocket(); |
| 23 | + }); |
| 24 | + |
| 25 | + function createSocket() { |
| 26 | + let streamWrap; |
| 27 | + const socket = new net.connect({ |
| 28 | + port, |
| 29 | + }, () => { |
| 30 | + socket.on('error', common.mustNotCall()); |
| 31 | + socket.on('end', common.mustCall()); |
| 32 | + socket.on('close', common.mustCall()); |
| 33 | + |
| 34 | + streamWrap.on('error', common.mustNotCall()); |
| 35 | + // The "end" events will be emitted which is as same as |
| 36 | + // the same situation for an instance of `net.Socket` without |
| 37 | + // `StreamWrap`. |
| 38 | + streamWrap.on('end', common.mustCall()); |
| 39 | + // Destroying a socket in the server side should emit EOF and cause |
| 40 | + // the corresponding client-side socket closed. |
| 41 | + streamWrap.on('close', common.mustCall(() => { |
| 42 | + server.close(); |
| 43 | + })); |
| 44 | + }); |
| 45 | + streamWrap = new StreamWrap(socket); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Destroy the streamWrap and test again. |
| 50 | +{ |
| 51 | + let port; |
| 52 | + const server = net.createServer((socket) => { |
| 53 | + socket.on('error', common.mustNotCall()); |
| 54 | + socket.on('end', common.mustCall()); |
| 55 | + socket.on('close', common.mustCall(() => { |
| 56 | + server.close(); |
| 57 | + })); |
| 58 | + // Do not `socket.end()` and directly `socket.destroy()`. |
| 59 | + }); |
| 60 | + |
| 61 | + server.listen(() => { |
| 62 | + port = server.address().port; |
| 63 | + createSocket(); |
| 64 | + }); |
| 65 | + |
| 66 | + function createSocket() { |
| 67 | + let streamWrap; |
| 68 | + const socket = new net.connect({ |
| 69 | + port, |
| 70 | + }, () => { |
| 71 | + socket.on('error', common.mustNotCall()); |
| 72 | + socket.on('end', common.mustNotCall()); |
| 73 | + socket.on('close', common.mustCall()); |
| 74 | + |
| 75 | + streamWrap.on('error', common.mustNotCall()); |
| 76 | + streamWrap.on('end', common.mustNotCall()); |
| 77 | + // Destroying a socket in the server side should emit EOF and cause |
| 78 | + // the corresponding client-side socket closed. |
| 79 | + streamWrap.on('close', common.mustCall()); |
| 80 | + streamWrap.destroy(); |
| 81 | + }); |
| 82 | + streamWrap = new StreamWrap(socket); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Destroy the client socket and test again. |
| 87 | +{ |
| 88 | + let port; |
| 89 | + const server = net.createServer((socket) => { |
| 90 | + socket.on('error', common.mustNotCall()); |
| 91 | + socket.on('end', common.mustCall()); |
| 92 | + socket.on('close', common.mustCall(() => { |
| 93 | + server.close(); |
| 94 | + })); |
| 95 | + }); |
| 96 | + |
| 97 | + server.listen(() => { |
| 98 | + port = server.address().port; |
| 99 | + createSocket(); |
| 100 | + }); |
| 101 | + |
| 102 | + function createSocket() { |
| 103 | + let streamWrap; |
| 104 | + const socket = new net.connect({ |
| 105 | + port, |
| 106 | + }, () => { |
| 107 | + socket.on('error', common.mustNotCall()); |
| 108 | + socket.on('end', common.mustNotCall()); |
| 109 | + socket.on('close', common.mustCall()); |
| 110 | + |
| 111 | + streamWrap.on('error', common.mustNotCall()); |
| 112 | + streamWrap.on('end', common.mustNotCall()); |
| 113 | + streamWrap.on('close', common.mustCall()); |
| 114 | + socket.destroy(); |
| 115 | + }); |
| 116 | + streamWrap = new StreamWrap(socket); |
| 117 | + } |
| 118 | +} |
0 commit comments