11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst net = require('net');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst tcp = net.Server(common.mustCall((s) => {
71cb0ef41Sopenharmony_ci  tcp.close();
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci  let buf = '';
101cb0ef41Sopenharmony_ci  s.setEncoding('utf8');
111cb0ef41Sopenharmony_ci  s.on('data', function(d) {
121cb0ef41Sopenharmony_ci    buf += d;
131cb0ef41Sopenharmony_ci  });
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  s.on('end', common.mustCall(function() {
161cb0ef41Sopenharmony_ci    console.error('SERVER: end', buf);
171cb0ef41Sopenharmony_ci    assert.strictEqual(buf, "L'État, c'est moi");
181cb0ef41Sopenharmony_ci    s.end();
191cb0ef41Sopenharmony_ci  }));
201cb0ef41Sopenharmony_ci}));
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_citcp.listen(0, common.mustCall(function() {
231cb0ef41Sopenharmony_ci  const socket = net.Stream({ highWaterMark: 0 });
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  let connected = false;
261cb0ef41Sopenharmony_ci  assert.strictEqual(socket.pending, true);
271cb0ef41Sopenharmony_ci  socket.connect(this.address().port, common.mustCall(() => connected = true));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  assert.strictEqual(socket.pending, true);
301cb0ef41Sopenharmony_ci  assert.strictEqual(socket.connecting, true);
311cb0ef41Sopenharmony_ci  assert.strictEqual(socket.readyState, 'opening');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  // Write a string that contains a multi-byte character sequence to test that
341cb0ef41Sopenharmony_ci  // `bytesWritten` is incremented with the # of bytes, not # of characters.
351cb0ef41Sopenharmony_ci  const a = "L'État, c'est ";
361cb0ef41Sopenharmony_ci  const b = 'moi';
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  // We're still connecting at this point so the datagram is first pushed onto
391cb0ef41Sopenharmony_ci  // the connect queue. Make sure that it's not added to `bytesWritten` again
401cb0ef41Sopenharmony_ci  // when the actual write happens.
411cb0ef41Sopenharmony_ci  const r = socket.write(a, common.mustCall((er) => {
421cb0ef41Sopenharmony_ci    console.error('write cb');
431cb0ef41Sopenharmony_ci    assert.ok(connected);
441cb0ef41Sopenharmony_ci    assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length);
451cb0ef41Sopenharmony_ci    assert.strictEqual(socket.pending, false);
461cb0ef41Sopenharmony_ci  }));
471cb0ef41Sopenharmony_ci  socket.on('close', common.mustCall(() => {
481cb0ef41Sopenharmony_ci    assert.strictEqual(socket.pending, true);
491cb0ef41Sopenharmony_ci  }));
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  assert.strictEqual(socket.bytesWritten, Buffer.from(a).length);
521cb0ef41Sopenharmony_ci  assert.strictEqual(r, false);
531cb0ef41Sopenharmony_ci  socket.end(b);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  assert.strictEqual(socket.readyState, 'opening');
561cb0ef41Sopenharmony_ci}));
57