summaryrefslogtreecommitdiff
path: root/test/parallel/test-socket-write-after-fin.js
blob: 2551d3f54f651f732bffc234fd05742f065cdbb2 (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
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const expected = 'hello1hello2hello3\nbye';

const server = net.createServer({
  allowHalfOpen: true
}, common.mustCall(function(sock) {
  let serverData = '';

  sock.setEncoding('utf8');
  sock.on('data', function(c) {
    serverData += c;
  });
  sock.on('end', common.mustCall(function() {
    assert.strictEqual(serverData, expected);
    sock.end(serverData);
    server.close();
  }));
}));
server.listen(0, common.mustCall(function() {
  const sock = net.connect(this.address().port);
  let clientData = '';

  sock.setEncoding('utf8');
  sock.on('data', function(c) {
    clientData += c;
  });

  sock.on('end', common.mustCall(function() {
    assert.strictEqual(clientData, expected);
  }));

  sock.write('hello1');
  sock.write('hello2');
  sock.write('hello3\n');
  assert.strictEqual(sock.end('bye'), sock);

}));