11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst net = require('net');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst message = Buffer.from('hello world');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Test typical usage
91cb0ef41Sopenharmony_cinet.createServer(common.mustCall(function(socket) {
101cb0ef41Sopenharmony_ci  this.close();
111cb0ef41Sopenharmony_ci  socket.end(message);
121cb0ef41Sopenharmony_ci})).listen(0, function() {
131cb0ef41Sopenharmony_ci  let received = 0;
141cb0ef41Sopenharmony_ci  const buffers = [];
151cb0ef41Sopenharmony_ci  const sockBuf = Buffer.alloc(8);
161cb0ef41Sopenharmony_ci  net.connect({
171cb0ef41Sopenharmony_ci    port: this.address().port,
181cb0ef41Sopenharmony_ci    onread: {
191cb0ef41Sopenharmony_ci      buffer: sockBuf,
201cb0ef41Sopenharmony_ci      callback: function(nread, buf) {
211cb0ef41Sopenharmony_ci        assert.strictEqual(buf, sockBuf);
221cb0ef41Sopenharmony_ci        received += nread;
231cb0ef41Sopenharmony_ci        buffers.push(Buffer.from(buf.slice(0, nread)));
241cb0ef41Sopenharmony_ci      }
251cb0ef41Sopenharmony_ci    }
261cb0ef41Sopenharmony_ci  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
271cb0ef41Sopenharmony_ci    assert.strictEqual(received, message.length);
281cb0ef41Sopenharmony_ci    assert.deepStrictEqual(Buffer.concat(buffers), message);
291cb0ef41Sopenharmony_ci  }));
301cb0ef41Sopenharmony_ci});
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// Test Uint8Array support
331cb0ef41Sopenharmony_cinet.createServer(common.mustCall(function(socket) {
341cb0ef41Sopenharmony_ci  this.close();
351cb0ef41Sopenharmony_ci  socket.end(message);
361cb0ef41Sopenharmony_ci})).listen(0, function() {
371cb0ef41Sopenharmony_ci  let received = 0;
381cb0ef41Sopenharmony_ci  let incoming = new Uint8Array(0);
391cb0ef41Sopenharmony_ci  const sockBuf = new Uint8Array(8);
401cb0ef41Sopenharmony_ci  net.connect({
411cb0ef41Sopenharmony_ci    port: this.address().port,
421cb0ef41Sopenharmony_ci    onread: {
431cb0ef41Sopenharmony_ci      buffer: sockBuf,
441cb0ef41Sopenharmony_ci      callback: function(nread, buf) {
451cb0ef41Sopenharmony_ci        assert.strictEqual(buf, sockBuf);
461cb0ef41Sopenharmony_ci        received += nread;
471cb0ef41Sopenharmony_ci        const newIncoming = new Uint8Array(incoming.length + nread);
481cb0ef41Sopenharmony_ci        newIncoming.set(incoming);
491cb0ef41Sopenharmony_ci        newIncoming.set(buf.slice(0, nread), incoming.length);
501cb0ef41Sopenharmony_ci        incoming = newIncoming;
511cb0ef41Sopenharmony_ci      }
521cb0ef41Sopenharmony_ci    }
531cb0ef41Sopenharmony_ci  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
541cb0ef41Sopenharmony_ci    assert.strictEqual(received, message.length);
551cb0ef41Sopenharmony_ci    assert.deepStrictEqual(incoming, new Uint8Array(message));
561cb0ef41Sopenharmony_ci  }));
571cb0ef41Sopenharmony_ci});
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci// Test Buffer callback usage
601cb0ef41Sopenharmony_cinet.createServer(common.mustCall(function(socket) {
611cb0ef41Sopenharmony_ci  this.close();
621cb0ef41Sopenharmony_ci  socket.end(message);
631cb0ef41Sopenharmony_ci})).listen(0, function() {
641cb0ef41Sopenharmony_ci  let received = 0;
651cb0ef41Sopenharmony_ci  const incoming = [];
661cb0ef41Sopenharmony_ci  const bufPool = [ Buffer.alloc(2), Buffer.alloc(2), Buffer.alloc(2) ];
671cb0ef41Sopenharmony_ci  let bufPoolIdx = -1;
681cb0ef41Sopenharmony_ci  let bufPoolUsage = 0;
691cb0ef41Sopenharmony_ci  net.connect({
701cb0ef41Sopenharmony_ci    port: this.address().port,
711cb0ef41Sopenharmony_ci    onread: {
721cb0ef41Sopenharmony_ci      buffer: () => {
731cb0ef41Sopenharmony_ci        ++bufPoolUsage;
741cb0ef41Sopenharmony_ci        bufPoolIdx = (bufPoolIdx + 1) % bufPool.length;
751cb0ef41Sopenharmony_ci        return bufPool[bufPoolIdx];
761cb0ef41Sopenharmony_ci      },
771cb0ef41Sopenharmony_ci      callback: function(nread, buf) {
781cb0ef41Sopenharmony_ci        assert.strictEqual(buf, bufPool[bufPoolIdx]);
791cb0ef41Sopenharmony_ci        received += nread;
801cb0ef41Sopenharmony_ci        incoming.push(Buffer.from(buf.slice(0, nread)));
811cb0ef41Sopenharmony_ci      }
821cb0ef41Sopenharmony_ci    }
831cb0ef41Sopenharmony_ci  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
841cb0ef41Sopenharmony_ci    assert.strictEqual(received, message.length);
851cb0ef41Sopenharmony_ci    assert.deepStrictEqual(Buffer.concat(incoming), message);
861cb0ef41Sopenharmony_ci    assert.strictEqual(bufPoolUsage, 7);
871cb0ef41Sopenharmony_ci  }));
881cb0ef41Sopenharmony_ci});
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci// Test Uint8Array callback support
911cb0ef41Sopenharmony_cinet.createServer(common.mustCall(function(socket) {
921cb0ef41Sopenharmony_ci  this.close();
931cb0ef41Sopenharmony_ci  socket.end(message);
941cb0ef41Sopenharmony_ci})).listen(0, function() {
951cb0ef41Sopenharmony_ci  let received = 0;
961cb0ef41Sopenharmony_ci  let incoming = new Uint8Array(0);
971cb0ef41Sopenharmony_ci  const bufPool = [ new Uint8Array(2), new Uint8Array(2), new Uint8Array(2) ];
981cb0ef41Sopenharmony_ci  let bufPoolIdx = -1;
991cb0ef41Sopenharmony_ci  let bufPoolUsage = 0;
1001cb0ef41Sopenharmony_ci  net.connect({
1011cb0ef41Sopenharmony_ci    port: this.address().port,
1021cb0ef41Sopenharmony_ci    onread: {
1031cb0ef41Sopenharmony_ci      buffer: () => {
1041cb0ef41Sopenharmony_ci        ++bufPoolUsage;
1051cb0ef41Sopenharmony_ci        bufPoolIdx = (bufPoolIdx + 1) % bufPool.length;
1061cb0ef41Sopenharmony_ci        return bufPool[bufPoolIdx];
1071cb0ef41Sopenharmony_ci      },
1081cb0ef41Sopenharmony_ci      callback: function(nread, buf) {
1091cb0ef41Sopenharmony_ci        assert.strictEqual(buf, bufPool[bufPoolIdx]);
1101cb0ef41Sopenharmony_ci        received += nread;
1111cb0ef41Sopenharmony_ci        const newIncoming = new Uint8Array(incoming.length + nread);
1121cb0ef41Sopenharmony_ci        newIncoming.set(incoming);
1131cb0ef41Sopenharmony_ci        newIncoming.set(buf.slice(0, nread), incoming.length);
1141cb0ef41Sopenharmony_ci        incoming = newIncoming;
1151cb0ef41Sopenharmony_ci      }
1161cb0ef41Sopenharmony_ci    }
1171cb0ef41Sopenharmony_ci  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
1181cb0ef41Sopenharmony_ci    assert.strictEqual(received, message.length);
1191cb0ef41Sopenharmony_ci    assert.deepStrictEqual(incoming, new Uint8Array(message));
1201cb0ef41Sopenharmony_ci    assert.strictEqual(bufPoolUsage, 7);
1211cb0ef41Sopenharmony_ci  }));
1221cb0ef41Sopenharmony_ci});
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci// Test explicit socket pause
1251cb0ef41Sopenharmony_cinet.createServer(common.mustCall(function(socket) {
1261cb0ef41Sopenharmony_ci  this.close();
1271cb0ef41Sopenharmony_ci  socket.end(message);
1281cb0ef41Sopenharmony_ci})).listen(0, function() {
1291cb0ef41Sopenharmony_ci  let received = 0;
1301cb0ef41Sopenharmony_ci  const buffers = [];
1311cb0ef41Sopenharmony_ci  const sockBuf = Buffer.alloc(8);
1321cb0ef41Sopenharmony_ci  let paused = false;
1331cb0ef41Sopenharmony_ci  net.connect({
1341cb0ef41Sopenharmony_ci    port: this.address().port,
1351cb0ef41Sopenharmony_ci    onread: {
1361cb0ef41Sopenharmony_ci      buffer: sockBuf,
1371cb0ef41Sopenharmony_ci      callback: function(nread, buf) {
1381cb0ef41Sopenharmony_ci        assert.strictEqual(paused, false);
1391cb0ef41Sopenharmony_ci        assert.strictEqual(buf, sockBuf);
1401cb0ef41Sopenharmony_ci        received += nread;
1411cb0ef41Sopenharmony_ci        buffers.push(Buffer.from(buf.slice(0, nread)));
1421cb0ef41Sopenharmony_ci        paused = true;
1431cb0ef41Sopenharmony_ci        this.pause();
1441cb0ef41Sopenharmony_ci        setTimeout(() => {
1451cb0ef41Sopenharmony_ci          paused = false;
1461cb0ef41Sopenharmony_ci          this.resume();
1471cb0ef41Sopenharmony_ci        }, 100);
1481cb0ef41Sopenharmony_ci      }
1491cb0ef41Sopenharmony_ci    }
1501cb0ef41Sopenharmony_ci  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
1511cb0ef41Sopenharmony_ci    assert.strictEqual(received, message.length);
1521cb0ef41Sopenharmony_ci    assert.deepStrictEqual(Buffer.concat(buffers), message);
1531cb0ef41Sopenharmony_ci  }));
1541cb0ef41Sopenharmony_ci});
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci// Test implicit socket pause
1571cb0ef41Sopenharmony_cinet.createServer(common.mustCall(function(socket) {
1581cb0ef41Sopenharmony_ci  this.close();
1591cb0ef41Sopenharmony_ci  socket.end(message);
1601cb0ef41Sopenharmony_ci})).listen(0, function() {
1611cb0ef41Sopenharmony_ci  let received = 0;
1621cb0ef41Sopenharmony_ci  const buffers = [];
1631cb0ef41Sopenharmony_ci  const sockBuf = Buffer.alloc(8);
1641cb0ef41Sopenharmony_ci  let paused = false;
1651cb0ef41Sopenharmony_ci  net.connect({
1661cb0ef41Sopenharmony_ci    port: this.address().port,
1671cb0ef41Sopenharmony_ci    onread: {
1681cb0ef41Sopenharmony_ci      buffer: sockBuf,
1691cb0ef41Sopenharmony_ci      callback: function(nread, buf) {
1701cb0ef41Sopenharmony_ci        assert.strictEqual(paused, false);
1711cb0ef41Sopenharmony_ci        assert.strictEqual(buf, sockBuf);
1721cb0ef41Sopenharmony_ci        received += nread;
1731cb0ef41Sopenharmony_ci        buffers.push(Buffer.from(buf.slice(0, nread)));
1741cb0ef41Sopenharmony_ci        paused = true;
1751cb0ef41Sopenharmony_ci        setTimeout(() => {
1761cb0ef41Sopenharmony_ci          paused = false;
1771cb0ef41Sopenharmony_ci          this.resume();
1781cb0ef41Sopenharmony_ci        }, 100);
1791cb0ef41Sopenharmony_ci        return false;
1801cb0ef41Sopenharmony_ci      }
1811cb0ef41Sopenharmony_ci    }
1821cb0ef41Sopenharmony_ci  }).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
1831cb0ef41Sopenharmony_ci    assert.strictEqual(received, message.length);
1841cb0ef41Sopenharmony_ci    assert.deepStrictEqual(Buffer.concat(buffers), message);
1851cb0ef41Sopenharmony_ci  }));
1861cb0ef41Sopenharmony_ci});
187