summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-writev.js
diff options
context:
space:
mode:
authorAnas Aboureada <anas.ieee@gmail.com>2019-02-04 17:18:39 +0100
committerRich Trott <rtrott@gmail.com>2019-08-16 23:24:02 -0700
commite4bbbcc84bd10be1e9e8e66d42542c70c1a02056 (patch)
treeb68c4aab9bf147c9670df4acb171b34857bf195f /test/parallel/test-fs-writev.js
parent5e3b4d6ed957ca03ae7178c7697549f470b6d86e (diff)
downloadandroid-node-v8-e4bbbcc84bd10be1e9e8e66d42542c70c1a02056.tar.gz
android-node-v8-e4bbbcc84bd10be1e9e8e66d42542c70c1a02056.tar.bz2
android-node-v8-e4bbbcc84bd10be1e9e8e66d42542c70c1a02056.zip
fs: add fs.writev() which exposes syscall writev()
fs with writev allow many buffers to be pushed to underlying OS APIs in one batch, so this should improve write speed to files. Refs: https://github.com/nodejs/node/issues/2298 PR-URL: https://github.com/nodejs/node/pull/25925 Fixes: https://github.com/nodejs/node/issues/2298 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test/parallel/test-fs-writev.js')
-rw-r--r--test/parallel/test-fs-writev.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/test/parallel/test-fs-writev.js b/test/parallel/test-fs-writev.js
new file mode 100644
index 0000000000..6a66c631f9
--- /dev/null
+++ b/test/parallel/test-fs-writev.js
@@ -0,0 +1,92 @@
+'use strict';
+
+const common = require('../common');
+const assert = require('assert');
+const path = require('path');
+const fs = require('fs');
+const tmpdir = require('../common/tmpdir');
+
+tmpdir.refresh();
+
+const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف';
+
+const getFileName = (i) => path.join(tmpdir.path, `writev_${i}.txt`);
+
+/**
+ * Testing with a array of buffers input
+ */
+
+// fs.writev with array of buffers with all parameters
+{
+ const filename = getFileName(1);
+ const fd = fs.openSync(filename, 'w');
+
+ const buffer = Buffer.from(expected);
+ const bufferArr = [buffer, buffer];
+
+ const done = common.mustCall((err, written, buffers) => {
+ assert.ifError(err);
+
+ assert.deepStrictEqual(bufferArr, buffers);
+ const expectedLength = bufferArr.length * buffer.byteLength;
+ assert.deepStrictEqual(written, expectedLength);
+ fs.closeSync(fd);
+
+ assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
+ });
+
+ fs.writev(fd, bufferArr, null, done);
+}
+
+// fs.writev with array of buffers without position
+{
+ const filename = getFileName(2);
+ const fd = fs.openSync(filename, 'w');
+
+ const buffer = Buffer.from(expected);
+ const bufferArr = [buffer, buffer];
+
+ const done = common.mustCall((err, written, buffers) => {
+ assert.ifError(err);
+
+ assert.deepStrictEqual(bufferArr, buffers);
+
+ const expectedLength = bufferArr.length * buffer.byteLength;
+ assert.deepStrictEqual(written, expectedLength);
+ fs.closeSync(fd);
+
+ assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename)));
+ });
+
+ fs.writev(fd, bufferArr, done);
+}
+
+/**
+ * Testing with wrong input types
+ */
+{
+ const filename = getFileName(3);
+ const fd = fs.openSync(filename, 'w');
+
+ [false, 'test', {}, [{}], ['sdf'], null, undefined].forEach((i) => {
+ common.expectsError(
+ () => fs.writev(fd, i, null, common.mustNotCall()), {
+ code: 'ERR_INVALID_ARG_TYPE',
+ type: TypeError
+ }
+ );
+ });
+
+ fs.closeSync(fd);
+}
+
+// fs.writev with wrong fd types
+[false, 'test', {}, [{}], null, undefined].forEach((i) => {
+ common.expectsError(
+ () => fs.writev(i, common.mustNotCall()),
+ {
+ code: 'ERR_INVALID_ARG_TYPE',
+ type: TypeError
+ }
+ );
+});