2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2020-02-11 19:18:27 -08:00
|
|
|
require('../common');
|
2016-04-01 21:40:21 -07:00
|
|
|
const assert = require('assert');
|
|
|
|
const net = require('net');
|
2020-05-09 11:01:51 -07:00
|
|
|
const debuglog = require('util').debuglog('test');
|
2010-03-02 21:14:23 -08:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let chars_recved = 0;
|
|
|
|
let npauses = 0;
|
2020-05-09 11:01:51 -07:00
|
|
|
let totalLength = 0;
|
2010-03-02 21:14:23 -08:00
|
|
|
|
2018-11-20 12:49:01 +05:30
|
|
|
const server = net.createServer((connection) => {
|
2020-05-09 11:01:51 -07:00
|
|
|
const body = 'C'.repeat(1024);
|
|
|
|
let n = 1;
|
|
|
|
debuglog('starting write loop');
|
|
|
|
while (connection.write(body)) {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
debuglog('ended write loop');
|
|
|
|
// Now that we're throttled, do some more writes to make sure the data isn't
|
|
|
|
// lost.
|
|
|
|
connection.write(body);
|
|
|
|
connection.write(body);
|
|
|
|
n += 2;
|
|
|
|
totalLength = n * body.length;
|
|
|
|
assert.ok(connection.bufferSize >= 0, `bufferSize: ${connection.bufferSize}`);
|
|
|
|
assert.ok(
|
|
|
|
connection.writableLength <= totalLength,
|
2023-02-12 17:36:39 +01:00
|
|
|
`writableLength: ${connection.writableLength}, totalLength: ${totalLength}`,
|
2020-05-09 11:01:51 -07:00
|
|
|
);
|
2013-02-28 23:25:29 +01:00
|
|
|
connection.end();
|
2009-08-09 18:49:51 +02:00
|
|
|
});
|
2010-12-04 16:11:57 -08:00
|
|
|
|
2020-02-11 19:18:27 -08:00
|
|
|
server.listen(0, () => {
|
|
|
|
const port = server.address().port;
|
2020-05-09 11:01:51 -07:00
|
|
|
debuglog(`server started on port ${port}`);
|
2017-01-08 13:19:00 +00:00
|
|
|
let paused = false;
|
2020-02-11 19:18:27 -08:00
|
|
|
const client = net.createConnection(port);
|
2010-12-03 04:03:18 +03:00
|
|
|
client.setEncoding('ascii');
|
2018-11-20 12:49:01 +05:30
|
|
|
client.on('data', (d) => {
|
2010-08-13 11:54:40 -04:00
|
|
|
chars_recved += d.length;
|
2020-05-09 11:01:51 -07:00
|
|
|
debuglog(`got ${chars_recved}`);
|
2010-08-13 11:54:40 -04:00
|
|
|
if (!paused) {
|
|
|
|
client.pause();
|
|
|
|
npauses += 1;
|
|
|
|
paused = true;
|
2020-05-09 11:01:51 -07:00
|
|
|
debuglog('pause');
|
2017-01-08 13:19:00 +00:00
|
|
|
const x = chars_recved;
|
2018-11-20 12:49:01 +05:30
|
|
|
setTimeout(() => {
|
2018-11-17 17:07:09 +05:30
|
|
|
assert.strictEqual(chars_recved, x);
|
2010-08-13 11:54:40 -04:00
|
|
|
client.resume();
|
2020-05-09 11:01:51 -07:00
|
|
|
debuglog('resume');
|
2010-08-13 11:54:40 -04:00
|
|
|
paused = false;
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
});
|
2010-03-02 21:14:23 -08:00
|
|
|
|
2018-11-20 12:49:01 +05:30
|
|
|
client.on('end', () => {
|
2010-08-13 11:54:40 -04:00
|
|
|
server.close();
|
|
|
|
client.end();
|
|
|
|
});
|
2010-03-02 21:14:23 -08:00
|
|
|
});
|
2009-08-26 18:22:00 +02:00
|
|
|
|
2010-08-13 11:54:40 -04:00
|
|
|
|
2018-11-20 12:49:01 +05:30
|
|
|
process.on('exit', () => {
|
2020-05-09 11:01:51 -07:00
|
|
|
assert.strictEqual(chars_recved, totalLength);
|
2023-06-29 15:21:00 +02:00
|
|
|
assert.ok(npauses > 1, `${npauses} > 1`);
|
2009-08-26 18:51:04 +02:00
|
|
|
});
|