• 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';
23require('../common');
24const assert = require('assert');
25const net = require('net');
26const debuglog = require('util').debuglog('test');
27
28let chars_recved = 0;
29let npauses = 0;
30let totalLength = 0;
31
32const server = net.createServer((connection) => {
33  const body = 'C'.repeat(1024);
34  let n = 1;
35  debuglog('starting write loop');
36  while (connection.write(body)) {
37    n++;
38  }
39  debuglog('ended write loop');
40  // Now that we're throttled, do some more writes to make sure the data isn't
41  // lost.
42  connection.write(body);
43  connection.write(body);
44  n += 2;
45  totalLength = n * body.length;
46  assert.ok(connection.bufferSize >= 0, `bufferSize: ${connection.bufferSize}`);
47  assert.ok(
48    connection.writableLength <= totalLength,
49    `writableLength: ${connection.writableLength}, totalLength: ${totalLength}`
50  );
51  connection.end();
52});
53
54server.listen(0, () => {
55  const port = server.address().port;
56  debuglog(`server started on port ${port}`);
57  let paused = false;
58  const client = net.createConnection(port);
59  client.setEncoding('ascii');
60  client.on('data', (d) => {
61    chars_recved += d.length;
62    debuglog(`got ${chars_recved}`);
63    if (!paused) {
64      client.pause();
65      npauses += 1;
66      paused = true;
67      debuglog('pause');
68      const x = chars_recved;
69      setTimeout(() => {
70        assert.strictEqual(chars_recved, x);
71        client.resume();
72        debuglog('resume');
73        paused = false;
74      }, 100);
75    }
76  });
77
78  client.on('end', () => {
79    server.close();
80    client.end();
81  });
82});
83
84
85process.on('exit', () => {
86  assert.strictEqual(chars_recved, totalLength);
87  assert.strictEqual(npauses > 2, true);
88});
89