• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const net = require('net');
5
6const tcp = net.Server(common.mustCall((s) => {
7  tcp.close();
8
9  let buf = '';
10  s.setEncoding('utf8');
11  s.on('data', function(d) {
12    buf += d;
13  });
14
15  s.on('end', common.mustCall(function() {
16    console.error('SERVER: end', buf);
17    assert.strictEqual(buf, "L'État, c'est moi");
18    s.end();
19  }));
20}));
21
22tcp.listen(0, common.mustCall(function() {
23  const socket = net.Stream({ highWaterMark: 0 });
24
25  let connected = false;
26  assert.strictEqual(socket.pending, true);
27  socket.connect(this.address().port, common.mustCall(() => connected = true));
28
29  assert.strictEqual(socket.pending, true);
30  assert.strictEqual(socket.connecting, true);
31  assert.strictEqual(socket.readyState, 'opening');
32
33  // Write a string that contains a multi-byte character sequence to test that
34  // `bytesWritten` is incremented with the # of bytes, not # of characters.
35  const a = "L'État, c'est ";
36  const b = 'moi';
37
38  // We're still connecting at this point so the datagram is first pushed onto
39  // the connect queue. Make sure that it's not added to `bytesWritten` again
40  // when the actual write happens.
41  const r = socket.write(a, common.mustCall((er) => {
42    console.error('write cb');
43    assert.ok(connected);
44    assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length);
45    assert.strictEqual(socket.pending, false);
46  }));
47  socket.on('close', common.mustCall(() => {
48    assert.strictEqual(socket.pending, true);
49  }));
50
51  assert.strictEqual(socket.bytesWritten, Buffer.from(a).length);
52  assert.strictEqual(r, false);
53  socket.end(b);
54
55  assert.strictEqual(socket.readyState, 'opening');
56}));
57