• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const net = require('net');
26
27const tcp = net.Server(common.mustCall((s) => {
28  tcp.close();
29
30  let buf = '';
31  s.setEncoding('utf8');
32  s.on('data', function(d) {
33    buf += d;
34  });
35
36  s.on('end', common.mustCall(function() {
37    console.error('SERVER: end', buf);
38    assert.strictEqual(buf, "L'État, c'est moi");
39    s.end();
40  }));
41}));
42
43tcp.listen(0, common.mustCall(function() {
44  const socket = net.Stream({ highWaterMark: 0 });
45
46  let connected = false;
47  assert.strictEqual(socket.pending, true);
48  socket.connect(this.address().port, common.mustCall(() => connected = true));
49
50  assert.strictEqual(socket.pending, true);
51  assert.strictEqual(socket.connecting, true);
52  assert.strictEqual(socket.readyState, 'opening');
53
54  // Write a string that contains a multi-byte character sequence to test that
55  // `bytesWritten` is incremented with the # of bytes, not # of characters.
56  const a = "L'État, c'est ";
57  const b = 'moi';
58
59  // We're still connecting at this point so the datagram is first pushed onto
60  // the connect queue. Make sure that it's not added to `bytesWritten` again
61  // when the actual write happens.
62  const r = socket.write(a, common.mustCall((er) => {
63    console.error('write cb');
64    assert.ok(connected);
65    assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length);
66    assert.strictEqual(socket.pending, false);
67  }));
68  socket.on('close', common.mustCall(() => {
69    assert.strictEqual(socket.pending, true);
70  }));
71
72  assert.strictEqual(socket.bytesWritten, Buffer.from(a).length);
73  assert.strictEqual(r, false);
74  socket.end(b);
75
76  assert.strictEqual(socket.readyState, 'opening');
77}));
78