|
| 1 | +/* Copyright (C) 2020 NooBaa */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const assert = require('assert'); |
| 7 | +const buffer_utils = require('../../../util/buffer_utils'); |
| 8 | +const native_fs_utils = require('../../../util/native_fs_utils'); |
| 9 | +const { FileReader } = require('../../../util/file_reader'); |
| 10 | +const { multi_buffer_pool } = require('../../../sdk/namespace_fs'); |
| 11 | + |
| 12 | +const fs_context = {}; |
| 13 | + |
| 14 | +describe('FileReader', () => { |
| 15 | + |
| 16 | + const test_files = fs.readdirSync(__dirname).map(file => path.join(__dirname, file)); |
| 17 | + |
| 18 | + /** |
| 19 | + * @param {(file_path: string, start?: number, end?: number) => void} tester |
| 20 | + */ |
| 21 | + function describe_read_cases(tester) { |
| 22 | + describe('list files and read entire', () => { |
| 23 | + for (const file_path of test_files) { |
| 24 | + tester(file_path); |
| 25 | + } |
| 26 | + }); |
| 27 | + describe('skip start cases', () => { |
| 28 | + tester(__filename, 1, Infinity); |
| 29 | + tester(__filename, 3, Infinity); |
| 30 | + tester(__filename, 11, Infinity); |
| 31 | + tester(__filename, 1023, Infinity); |
| 32 | + tester(__filename, 1024, Infinity); |
| 33 | + tester(__filename, 1025, Infinity); |
| 34 | + }); |
| 35 | + describe('edge cases', () => { |
| 36 | + tester(__filename, 0, 1); |
| 37 | + tester(__filename, 0, 2); |
| 38 | + tester(__filename, 0, 3); |
| 39 | + tester(__filename, 1, 2); |
| 40 | + tester(__filename, 1, 3); |
| 41 | + tester(__filename, 2, 3); |
| 42 | + tester(__filename, 0, 1023); |
| 43 | + tester(__filename, 0, 1024); |
| 44 | + tester(__filename, 0, 1025); |
| 45 | + tester(__filename, 1, 1023); |
| 46 | + tester(__filename, 1, 1024); |
| 47 | + tester(__filename, 1, 1025); |
| 48 | + tester(__filename, 1023, 1024); |
| 49 | + tester(__filename, 1023, 1025); |
| 50 | + tester(__filename, 1024, 1025); |
| 51 | + tester(__filename, 123, 345); |
| 52 | + tester(__filename, 1000000000, Infinity); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + describe('as stream.Readable', () => { |
| 57 | + |
| 58 | + describe_read_cases(tester); |
| 59 | + |
| 60 | + function tester(file_path, start = 0, end = Infinity) { |
| 61 | + const basename = path.basename(file_path); |
| 62 | + it(`test read ${start}-${end} ${basename}`, async () => { |
| 63 | + await native_fs_utils.use_file({ |
| 64 | + fs_context, |
| 65 | + bucket_path: file_path, |
| 66 | + open_path: file_path, |
| 67 | + scope: async file => { |
| 68 | + const stat = await file.stat(fs_context); |
| 69 | + const aborter = new AbortController(); |
| 70 | + const signal = aborter.signal; |
| 71 | + const file_reader = new FileReader({ |
| 72 | + fs_context, |
| 73 | + file, |
| 74 | + file_path, |
| 75 | + stat, |
| 76 | + start, |
| 77 | + end, |
| 78 | + signal, |
| 79 | + multi_buffer_pool, |
| 80 | + highWaterMark: 1024, // bytes |
| 81 | + }); |
| 82 | + const data = await buffer_utils.read_stream_join(file_reader); |
| 83 | + const node_fs_stream = fs.createReadStream(file_path, { start, end: end > 0 ? end - 1 : 0 }); |
| 84 | + const node_fs_data = await buffer_utils.read_stream_join(node_fs_stream); |
| 85 | + assert.strictEqual(data.length, node_fs_data.length); |
| 86 | + assert.strictEqual(data.toString(), node_fs_data.toString()); |
| 87 | + } |
| 88 | + }); |
| 89 | + }); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + describe('read_into_stream with buffer pooling', () => { |
| 94 | + |
| 95 | + describe_read_cases(tester); |
| 96 | + |
| 97 | + function tester(file_path, start = 0, end = Infinity) { |
| 98 | + const basename = path.basename(file_path); |
| 99 | + it(`test read ${start}-${end} ${basename}`, async () => { |
| 100 | + await native_fs_utils.use_file({ |
| 101 | + fs_context, |
| 102 | + bucket_path: file_path, |
| 103 | + open_path: file_path, |
| 104 | + scope: async file => { |
| 105 | + const stat = await file.stat(fs_context); |
| 106 | + const aborter = new AbortController(); |
| 107 | + const signal = aborter.signal; |
| 108 | + const file_reader = new FileReader({ |
| 109 | + fs_context, |
| 110 | + file, |
| 111 | + file_path, |
| 112 | + stat, |
| 113 | + start, |
| 114 | + end, |
| 115 | + signal, |
| 116 | + multi_buffer_pool, |
| 117 | + highWaterMark: 1024, // bytes |
| 118 | + }); |
| 119 | + const writable = buffer_utils.write_stream(); |
| 120 | + await file_reader.read_into_stream(writable); |
| 121 | + const data = writable.join(); |
| 122 | + const node_fs_stream = fs.createReadStream(file_path, { start, end: end > 0 ? end - 1 : 0 }); |
| 123 | + const node_fs_data = await buffer_utils.read_stream_join(node_fs_stream); |
| 124 | + assert.strictEqual(data.length, node_fs_data.length); |
| 125 | + assert.strictEqual(data.toString(), node_fs_data.toString()); |
| 126 | + } |
| 127 | + }); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + }); |
| 132 | + |
| 133 | + |
| 134 | + // Abort tests are disabled temporarily due to flakiness |
| 135 | + // |
| 136 | + // describe('abort during read_into_stream', () => { |
| 137 | + // tester(__filename); |
| 138 | + // function tester(file_path, start = 0, end = Infinity) { |
| 139 | + // const basename = path.basename(file_path); |
| 140 | + // it(`test abort read ${start}-${end} ${basename}`, async () => { |
| 141 | + // await native_fs_utils.use_file({ |
| 142 | + // fs_context, |
| 143 | + // bucket_path: file_path, |
| 144 | + // open_path: file_path, |
| 145 | + // scope: async file => { |
| 146 | + // const stat = await file.stat(fs_context); |
| 147 | + // const aborter = new AbortController(); |
| 148 | + // const signal = aborter.signal; |
| 149 | + // const file_reader = new FileReader({ |
| 150 | + // fs_context, |
| 151 | + // file, |
| 152 | + // file_path, |
| 153 | + // stat, |
| 154 | + // start, |
| 155 | + // end, |
| 156 | + // signal, |
| 157 | + // multi_buffer_pool, |
| 158 | + // highWaterMark: 1024, // bytes |
| 159 | + // }); |
| 160 | + // const writable = buffer_utils.write_stream(); |
| 161 | + // const promise = file_reader.read_into_stream(writable); |
| 162 | + // setImmediate(() => aborter.abort()); // abort quickly |
| 163 | + // await assert.rejects(promise, { name: 'AbortError' }); |
| 164 | + // } |
| 165 | + // }); |
| 166 | + // }); |
| 167 | + // } |
| 168 | + // }); |
| 169 | + |
| 170 | +}); |
0 commit comments