From f8645099914914c6360b5975b6db19eedd30139a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BA=D0=BE=D0=B2=D0=BE=D1=80=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=90=D0=BD=D0=B4=D1=80?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Sat, 24 Feb 2018 19:52:14 +0300 Subject: [PATCH] test,benchmark: use new Buffer API where appropriate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Daijiro Wachi Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Michael Dawson Reviewed-By: Tobias Nießen --- benchmark/streams/pipe.js | 2 +- benchmark/streams/readable-bigread.js | 2 +- benchmark/streams/readable-bigunevenread.js | 2 +- benchmark/streams/readable-readall.js | 2 +- benchmark/streams/readable-unevenread.js | 2 +- test/async-hooks/test-udpsendwrap.js | 2 +- test/parallel/test-buffer-badhex.js | 14 +++++++------- test/parallel/test-buffer-fill.js | 2 +- test/parallel/test-buffer-indexof.js | 4 ++-- test/parallel/test-buffer-zero-fill.js | 1 + test/parallel/test-net-socket-byteswritten.js | 2 +- test/parallel/test-zlib-empty-buffer.js | 2 +- 12 files changed, 19 insertions(+), 18 deletions(-) diff --git a/benchmark/streams/pipe.js b/benchmark/streams/pipe.js index a7d67b7d6908c8..4baeeb2d2e7706 100644 --- a/benchmark/streams/pipe.js +++ b/benchmark/streams/pipe.js @@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, { }); function main({ n }) { - const b = new Buffer(1024); + const b = Buffer.alloc(1024); const r = new Readable(); const w = new Writable(); diff --git a/benchmark/streams/readable-bigread.js b/benchmark/streams/readable-bigread.js index 99213afaeb8f28..62d1af874fb22a 100644 --- a/benchmark/streams/readable-bigread.js +++ b/benchmark/streams/readable-bigread.js @@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, { }); function main({ n }) { - const b = new Buffer(32); + const b = Buffer.alloc(32); const s = new Readable(); function noop() {} s._read = noop; diff --git a/benchmark/streams/readable-bigunevenread.js b/benchmark/streams/readable-bigunevenread.js index e2f2c1406a1da0..e13769189a69da 100644 --- a/benchmark/streams/readable-bigunevenread.js +++ b/benchmark/streams/readable-bigunevenread.js @@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, { }); function main({ n }) { - const b = new Buffer(32); + const b = Buffer.alloc(32); const s = new Readable(); function noop() {} s._read = noop; diff --git a/benchmark/streams/readable-readall.js b/benchmark/streams/readable-readall.js index 5715e42017c795..3c177ec4c39988 100644 --- a/benchmark/streams/readable-readall.js +++ b/benchmark/streams/readable-readall.js @@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, { }); function main({ n }) { - const b = new Buffer(32); + const b = Buffer.alloc(32); const s = new Readable(); function noop() {} s._read = noop; diff --git a/benchmark/streams/readable-unevenread.js b/benchmark/streams/readable-unevenread.js index d7a408b1c56a31..f8b501ab4729ec 100644 --- a/benchmark/streams/readable-unevenread.js +++ b/benchmark/streams/readable-unevenread.js @@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, { }); function main({ n }) { - const b = new Buffer(32); + const b = Buffer.alloc(32); const s = new Readable(); function noop() {} s._read = noop; diff --git a/test/async-hooks/test-udpsendwrap.js b/test/async-hooks/test-udpsendwrap.js index 10deaca8452d3d..d8ab77730f4921 100644 --- a/test/async-hooks/test-udpsendwrap.js +++ b/test/async-hooks/test-udpsendwrap.js @@ -18,7 +18,7 @@ const sock = dgram function onlistening() { sock.send( - new Buffer(2), 0, 2, sock.address().port, + Buffer.alloc(2), 0, 2, sock.address().port, undefined, common.mustCall(onsent)); // init not called synchronously because dns lookup always wraps diff --git a/test/parallel/test-buffer-badhex.js b/test/parallel/test-buffer-badhex.js index 94de97181d38e0..61086659fa7b6e 100644 --- a/test/parallel/test-buffer-badhex.js +++ b/test/parallel/test-buffer-badhex.js @@ -6,12 +6,12 @@ const assert = require('assert'); { const buf = Buffer.alloc(4); assert.strictEqual(buf.length, 4); - assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); + assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0])); assert.strictEqual(buf.write('abcdxx', 0, 'hex'), 2); - assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0x00, 0x00])); + assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0x00, 0x00])); assert.strictEqual(buf.toString('hex'), 'abcd0000'); assert.strictEqual(buf.write('abcdef01', 0, 'hex'), 4); - assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0xef, 0x01])); + assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0xef, 0x01])); assert.strictEqual(buf.toString('hex'), 'abcdef01'); const copy = Buffer.from(buf.toString('hex'), 'hex'); @@ -26,13 +26,13 @@ const assert = require('assert'); { const buf = Buffer.alloc(4); - assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); + assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0])); assert.strictEqual(buf.write('xxabcd', 0, 'hex'), 0); - assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); + assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0])); assert.strictEqual(buf.write('xxab', 1, 'hex'), 0); - assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0])); + assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0])); assert.strictEqual(buf.write('cdxxab', 0, 'hex'), 1); - assert.deepStrictEqual(buf, new Buffer([0xcd, 0, 0, 0])); + assert.deepStrictEqual(buf, Buffer.from([0xcd, 0, 0, 0])); } { diff --git a/test/parallel/test-buffer-fill.js b/test/parallel/test-buffer-fill.js index 2b36af38d0b2b8..14f44767c4452a 100644 --- a/test/parallel/test-buffer-fill.js +++ b/test/parallel/test-buffer-fill.js @@ -427,7 +427,7 @@ common.expectsError(() => { // Test that bypassing 'length' won't cause an abort. common.expectsError(() => { - const buf = new Buffer('w00t'); + const buf = Buffer.from('w00t'); Object.defineProperty(buf, 'length', { value: 1337, enumerable: true diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index 08d640b1dc1924..357558c74d2edc 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -504,7 +504,7 @@ assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'binary'), -1); assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好'), 7), -1); // Test lastIndexOf on a longer buffer: -const bufferString = new Buffer('a man a plan a canal panama'); +const bufferString = Buffer.from('a man a plan a canal panama'); assert.strictEqual(15, bufferString.lastIndexOf('canal')); assert.strictEqual(21, bufferString.lastIndexOf('panama')); assert.strictEqual(0, bufferString.lastIndexOf('a man a plan a canal panama')); @@ -566,7 +566,7 @@ const parts = []; for (let i = 0; i < 1000000; i++) { parts.push((countBits(i) % 2 === 0) ? 'yolo' : 'swag'); } -const reallyLong = new Buffer(parts.join(' ')); +const reallyLong = Buffer.from(parts.join(' ')); assert.strictEqual('yolo swag swag yolo', reallyLong.slice(0, 19).toString()); // Expensive reverse searches. Stress test lastIndexOf: diff --git a/test/parallel/test-buffer-zero-fill.js b/test/parallel/test-buffer-zero-fill.js index 1aca4e9a5c438e..7a9f0c12500481 100644 --- a/test/parallel/test-buffer-zero-fill.js +++ b/test/parallel/test-buffer-zero-fill.js @@ -3,6 +3,7 @@ require('../common'); const assert = require('assert'); +// Tests deprecated Buffer API on purpose const buf1 = Buffer(100); const buf2 = new Buffer(100); diff --git a/test/parallel/test-net-socket-byteswritten.js b/test/parallel/test-net-socket-byteswritten.js index 6f3ce8a3c6c8d1..b7b7af89e215db 100644 --- a/test/parallel/test-net-socket-byteswritten.js +++ b/test/parallel/test-net-socket-byteswritten.js @@ -16,7 +16,7 @@ server.listen(0, common.mustCall(function() { socket.cork(); socket.write('one'); - socket.write(new Buffer('twø', 'utf8')); + socket.write(Buffer.from('twø', 'utf8')); socket.uncork(); diff --git a/test/parallel/test-zlib-empty-buffer.js b/test/parallel/test-zlib-empty-buffer.js index 908c89cbbcef2e..8b299f8728282d 100644 --- a/test/parallel/test-zlib-empty-buffer.js +++ b/test/parallel/test-zlib-empty-buffer.js @@ -3,7 +3,7 @@ const common = require('../common'); const zlib = require('zlib'); const { inspect, promisify } = require('util'); const assert = require('assert'); -const emptyBuffer = new Buffer(0); +const emptyBuffer = Buffer.alloc(0); common.crashOnUnhandledRejection();