• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4
5// This is similar to simple/test-socket-write-after-fin, except that
6// we don't set allowHalfOpen.  Then we write after the client has sent
7// a FIN, and this is an error.  However, the standard "write after end"
8// message is too vague, and doesn't actually tell you what happens.
9
10const net = require('net');
11let serverData = '';
12let gotServerEnd = false;
13let clientData = '';
14let gotClientEnd = false;
15let gotServerError = false;
16
17const server = net.createServer(function(sock) {
18  sock.setEncoding('utf8');
19  sock.on('error', function() {});
20
21  sock.on('data', function(c) {
22    serverData += c;
23  });
24  sock.on('end', function() {
25    gotServerEnd = true;
26    setImmediate(() => {
27      sock.write(serverData, function(er) {
28        console.error(`${er.code}: ${er.message}`);
29        gotServerError = er;
30      });
31      sock.end();
32    });
33  });
34  server.close();
35});
36server.listen(0, function() {
37  const sock = net.connect(this.address().port);
38  sock.setEncoding('utf8');
39  sock.on('data', function(c) {
40    clientData += c;
41  });
42
43  sock.on('end', function() {
44    gotClientEnd = true;
45  });
46
47  process.on('exit', function() {
48    assert.strictEqual(clientData, '');
49    assert.strictEqual(serverData, 'hello1hello2hello3\nTHUNDERMUSCLE!');
50    assert(gotClientEnd);
51    assert(gotServerEnd);
52    assert(gotServerError);
53    assert.strictEqual(gotServerError.code, 'EPIPE');
54    assert.notStrictEqual(gotServerError.message, 'write after end');
55    console.log('ok');
56  });
57
58  sock.write('hello1');
59  sock.write('hello2');
60  sock.write('hello3\n');
61  sock.end('THUNDERMUSCLE!');
62});
63