• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const path = require('path');
5const fs = require('fs').promises;
6const tmpdir = require('../common/tmpdir');
7
8tmpdir.refresh();
9
10const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف';
11const exptectedBuff = Buffer.from(expected);
12
13let cnt = 0;
14function getFileName() {
15  return path.join(tmpdir.path, `readv_promises_${++cnt}.txt`);
16}
17
18const allocateEmptyBuffers = (combinedLength) => {
19  const bufferArr = [];
20  // Allocate two buffers, each half the size of exptectedBuff
21  bufferArr[0] = Buffer.alloc(Math.floor(combinedLength / 2));
22  bufferArr[1] = Buffer.alloc(combinedLength - bufferArr[0].length);
23
24  return bufferArr;
25};
26
27(async () => {
28  {
29    const filename = getFileName();
30    await fs.writeFile(filename, exptectedBuff);
31    const handle = await fs.open(filename, 'r');
32    const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
33    const expectedLength = exptectedBuff.length;
34
35    let { bytesRead, buffers } = await handle.readv([Buffer.from('')],
36                                                    null);
37    assert.strictEqual(bytesRead, 0);
38    assert.deepStrictEqual(buffers, [Buffer.from('')]);
39
40    ({ bytesRead, buffers } = await handle.readv(bufferArr, null));
41    assert.strictEqual(bytesRead, expectedLength);
42    assert.deepStrictEqual(buffers, bufferArr);
43    assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename)));
44    handle.close();
45  }
46
47  {
48    const filename = getFileName();
49    await fs.writeFile(filename, exptectedBuff);
50    const handle = await fs.open(filename, 'r');
51    const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
52    const expectedLength = exptectedBuff.length;
53
54    let { bytesRead, buffers } = await handle.readv([Buffer.from('')]);
55    assert.strictEqual(bytesRead, 0);
56    assert.deepStrictEqual(buffers, [Buffer.from('')]);
57
58    ({ bytesRead, buffers } = await handle.readv(bufferArr));
59    assert.strictEqual(bytesRead, expectedLength);
60    assert.deepStrictEqual(buffers, bufferArr);
61    assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename)));
62    handle.close();
63  }
64})().then(common.mustCall());
65