summaryrefslogtreecommitdiff
path: root/test/parallel/test-net-bytes-read.js
blob: d569d78403e1d562c7cf32088d51a82aafc791e3 (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
41
42
43
44
45
46
47
'use strict';

const common = require('../common');
const assert = require('assert');
const net = require('net');

const big = Buffer.alloc(1024 * 1024);

const handler = common.mustCall((socket) => {
  socket.end(big);
  server.close();
});

const onListen = common.mustCall(() => {
  let prev = 0;

  function checkRaise(value) {
    assert(value > prev);
    prev = value;
  }

  const onData = common.mustCallAtLeast((chunk) => {
    checkRaise(socket.bytesRead);
  });

  const onEnd = common.mustCall(() => {
    assert.strictEqual(socket.bytesRead, prev);
    assert.strictEqual(big.length, prev);
  });

  const onClose = common.mustCall(() => {
    assert(!socket._handle);
    assert.strictEqual(socket.bytesRead, prev);
    assert.strictEqual(big.length, prev);
  });

  const onConnect = common.mustCall(() => {
    socket.on('data', onData);
    socket.on('end', onEnd);
    socket.on('close', onClose);
    socket.end();
  });

  const socket = net.connect(server.address().port, onConnect);
});

const server = net.createServer(handler).listen(0, onListen);