node/test/parallel/test-net-socket-byteswritten.js
Сковорода Никита Андреевич 8ed44ff1c4
test,benchmark: use new Buffer API where appropriate
For tests / benchmarks that are creating Buffer instances for
any reason other than to test Buffer constructor, use the new
Buffer.alloc/Buffer.from API instead of the deprecated API.

PR-URL: https://github.com/nodejs/node/pull/18980
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
2018-03-04 12:50:15 +01:00

36 lines
856 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const server = net.createServer(function(socket) {
socket.end();
});
server.listen(0, common.mustCall(function() {
const socket = net.connect(server.address().port);
// Cork the socket, then write twice; this should cause a writev, which
// previously caused an err in the bytesWritten count.
socket.cork();
socket.write('one');
socket.write(Buffer.from('twø', 'utf8'));
socket.uncork();
// one = 3 bytes, twø = 4 bytes
assert.strictEqual(socket.bytesWritten, 3 + 4);
socket.on('connect', common.mustCall(function() {
assert.strictEqual(socket.bytesWritten, 3 + 4);
}));
socket.on('end', common.mustCall(function() {
assert.strictEqual(socket.bytesWritten, 3 + 4);
server.close();
}));
}));