mirror of https://github.com/seald/nedb
parent
8b776b5c45
commit
dbcc3647d1
@ -1,133 +1,59 @@ |
|||||||
/* eslint-env mocha */ |
/* eslint-env mocha */ |
||||||
/* global DEBUG */ |
|
||||||
/** |
/** |
||||||
* Load and modify part of fs to ensure writeFile will crash after writing 5000 bytes |
* Load and modify part of fs to ensure writeFile will crash after writing 5000 bytes |
||||||
*/ |
*/ |
||||||
const fs = require('fs') |
const fs = require('fs') |
||||||
|
const { Writable } = require('stream') |
||||||
function rethrow () { |
const { callbackify } = require('util') |
||||||
// Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
|
|
||||||
// is fairly slow to generate.
|
fs.promises.writeFile = async function (path, data) { |
||||||
if (DEBUG) { |
let onePassDone = false |
||||||
const backtrace = new Error() |
const options = { encoding: 'utf8', mode: 0o666, flag: 'w' } // we don't care about the actual options passed
|
||||||
return function (err) { |
|
||||||
if (err) { |
const filehandle = await fs.promises.open(path, options.flag, options.mode) |
||||||
backtrace.stack = err.name + ': ' + err.message + |
const buffer = (data instanceof Buffer) ? data : Buffer.from('' + data, options.encoding || 'utf8') |
||||||
backtrace.stack.substr(backtrace.name.length) |
let length = buffer.length |
||||||
throw backtrace |
let offset = 0 |
||||||
} |
|
||||||
} |
try { |
||||||
} |
while (length > 0) { |
||||||
|
if (onePassDone) { process.exit(1) } // Crash on purpose before rewrite done
|
||||||
return function (err) { |
const { bytesWritten } = await filehandle.write(buffer, offset, Math.min(5000, length)) // Force write by chunks of 5000 bytes to ensure data will be incomplete on crash
|
||||||
if (err) { |
onePassDone = true |
||||||
throw err // Forgot a callback but don't know where? Use NODE_DEBUG=fs
|
offset += bytesWritten |
||||||
|
length -= bytesWritten |
||||||
} |
} |
||||||
|
} finally { |
||||||
|
await filehandle.close() |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
function maybeCallback (cb) { |
class FakeFsWriteStream extends Writable { |
||||||
return typeof cb === 'function' ? cb : rethrow() |
constructor (filename) { |
||||||
} |
super() |
||||||
|
this.filename = filename |
||||||
function isFd (path) { |
this._content = Buffer.alloc(0) |
||||||
return (path >>> 0) === path |
|
||||||
} |
|
||||||
|
|
||||||
function assertEncoding (encoding) { |
|
||||||
if (encoding && !Buffer.isEncoding(encoding)) { |
|
||||||
throw new Error('Unknown encoding: ' + encoding) |
|
||||||
} |
} |
||||||
} |
|
||||||
|
|
||||||
let onePassDone = false |
|
||||||
|
|
||||||
function writeAll (fd, isUserFd, buffer, offset, length, position, callback_) { |
|
||||||
const callback = maybeCallback(arguments[arguments.length - 1]) |
|
||||||
|
|
||||||
if (onePassDone) { process.exit(1) } // Crash on purpose before rewrite done
|
|
||||||
const l = Math.min(5000, length) // Force write by chunks of 5000 bytes to ensure data will be incomplete on crash
|
|
||||||
|
|
||||||
// write(fd, buffer, offset, length, position, callback)
|
_write (chunk, encoding, callback) { |
||||||
fs.write(fd, buffer, offset, l, position, function (writeErr, written) { |
this._content = Buffer.concat([this._content, Buffer.from(chunk, encoding)]) |
||||||
if (writeErr) { |
callback() |
||||||
if (isUserFd) { |
|
||||||
if (callback) callback(writeErr) |
|
||||||
} else { |
|
||||||
fs.close(fd, function () { |
|
||||||
if (callback) callback(writeErr) |
|
||||||
}) |
|
||||||
} |
|
||||||
} else { |
|
||||||
onePassDone = true |
|
||||||
if (written === length) { |
|
||||||
if (isUserFd) { |
|
||||||
if (callback) callback(null) |
|
||||||
} else { |
|
||||||
fs.close(fd, callback) |
|
||||||
} |
|
||||||
} else { |
|
||||||
offset += written |
|
||||||
length -= written |
|
||||||
if (position !== null) { |
|
||||||
position += written |
|
||||||
} |
|
||||||
writeAll(fd, isUserFd, buffer, offset, length, position, callback) |
|
||||||
} |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
fs.writeFile = function (path, data, options, callback_) { |
|
||||||
const callback = maybeCallback(arguments[arguments.length - 1]) |
|
||||||
|
|
||||||
if (!options || typeof options === 'function') { |
|
||||||
options = { encoding: 'utf8', mode: 438, flag: 'w' } // Mode 438 == 0o666 (compatibility with older Node releases)
|
|
||||||
} else if (typeof options === 'string') { |
|
||||||
options = { encoding: options, mode: 438, flag: 'w' } // Mode 438 == 0o666 (compatibility with older Node releases)
|
|
||||||
} else if (typeof options !== 'object') { |
|
||||||
throw new Error(`throwOptionsError${options}`) |
|
||||||
} |
} |
||||||
|
|
||||||
assertEncoding(options.encoding) |
_end (chunk, encoding, callback) { |
||||||
|
this._content = Buffer.concat([this._content, Buffer.from(chunk, encoding)]) |
||||||
const flag = options.flag || 'w' |
callback() |
||||||
|
|
||||||
if (isFd(path)) { |
|
||||||
writeFd(path, true) |
|
||||||
return |
|
||||||
} |
} |
||||||
|
|
||||||
fs.open(path, flag, options.mode, function (openErr, fd) { |
close (callback) { |
||||||
if (openErr) { |
callbackify(fs.promises.writeFile)(this.filename, this._content, 'utf8', callback) |
||||||
if (callback) callback(openErr) |
|
||||||
} else { |
|
||||||
writeFd(fd, false) |
|
||||||
} |
|
||||||
}) |
|
||||||
|
|
||||||
function writeFd (fd, isUserFd) { |
|
||||||
const buffer = (data instanceof Buffer) ? data : Buffer.from('' + data, options.encoding || 'utf8') |
|
||||||
const position = /a/.test(flag) ? null : 0 |
|
||||||
|
|
||||||
writeAll(fd, isUserFd, buffer, 0, buffer.length, position, callback) |
|
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
fs.createWriteStream = function (path) { |
fs.createWriteStream = path => new FakeFsWriteStream(path) |
||||||
let content = '' |
|
||||||
return { |
|
||||||
write (data) { |
|
||||||
content += data |
|
||||||
}, |
|
||||||
close (callback) { |
|
||||||
fs.writeFile(path, content, callback) |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// End of fs modification
|
// End of fs monkey patching
|
||||||
const Nedb = require('../lib/datastore.js') |
const Nedb = require('../lib/datastore.js') |
||||||
const db = new Nedb({ filename: 'workspace/lac.db' }) |
const db = new Nedb({ filename: 'workspace/lac.db' }) |
||||||
|
|
||||||
db.loadDatabase() |
db.loadDatabaseAsync() // no need to await
|
||||||
|
@ -1,65 +1,61 @@ |
|||||||
const fs = require('fs') |
const fs = require('fs') |
||||||
const { waterfall, whilst } = require('../test/utils.test.js') |
const fsPromises = fs.promises |
||||||
const Nedb = require('../lib/datastore') |
const Nedb = require('../lib/datastore') |
||||||
const { callbackify } = require('util') |
|
||||||
const db = new Nedb({ filename: './workspace/openfds.db', autoload: true }) |
|
||||||
const N = 64 |
const N = 64 |
||||||
let i |
|
||||||
let fds |
|
||||||
|
|
||||||
function multipleOpen (filename, N, callback) { |
// A console.error triggers an error of the parent test
|
||||||
whilst(function () { return i < N } |
|
||||||
, function (cb) { |
const test = async () => { |
||||||
fs.open(filename, 'r', function (err, fd) { |
let filehandles = [] |
||||||
i += 1 |
try { |
||||||
if (fd) { fds.push(fd) } |
for (let i = 0; i < 2 * N + 1; i++) { |
||||||
return cb(err) |
const filehandle = await fsPromises.open('./test_lac/openFdsTestFile', 'r') |
||||||
}) |
filehandles.push(filehandle) |
||||||
} |
} |
||||||
, callback) |
console.error('No error occurred while opening a file too many times') |
||||||
} |
process.exit(1) |
||||||
|
} catch (error) { |
||||||
|
if (error.code !== 'EMFILE') { |
||||||
|
console.error(error) |
||||||
|
process.exit(1) |
||||||
|
} |
||||||
|
} finally { |
||||||
|
for (const filehandle of filehandles) { |
||||||
|
await filehandle.close() |
||||||
|
} |
||||||
|
filehandles = [] |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
for (let i = 0; i < N; i++) { |
||||||
|
const filehandle = await fsPromises.open('./test_lac/openFdsTestFile2', 'r') |
||||||
|
filehandles.push(filehandle) |
||||||
|
} |
||||||
|
} catch (error) { |
||||||
|
console.error(`An unexpected error occurred when opening file not too many times at i: ${i} with error: ${error}`) |
||||||
|
process.exit(1) |
||||||
|
} finally { |
||||||
|
for (const filehandle of filehandles) { |
||||||
|
await filehandle.close() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
waterfall([ |
try { |
||||||
// Check that ulimit has been set to the correct value
|
const db = new Nedb({ filename: './workspace/openfds.db' }) |
||||||
function (cb) { |
await db.loadDatabaseAsync() |
||||||
i = 0 |
await db.removeAsync({}, { multi: true }) |
||||||
fds = [] |
await db.insertAsync({ hello: 'world' }) |
||||||
multipleOpen('./test_lac/openFdsTestFile', 2 * N + 1, function (err) { |
|
||||||
if (!err) { console.log('No error occured while opening a file too many times') } |
|
||||||
fds.forEach(function (fd) { fs.closeSync(fd) }) |
|
||||||
return cb() |
|
||||||
}) |
|
||||||
}, |
|
||||||
function (cb) { |
|
||||||
i = 0 |
|
||||||
fds = [] |
|
||||||
multipleOpen('./test_lac/openFdsTestFile2', N, function (err) { |
|
||||||
if (err) { console.log('An unexpected error occured when opening file not too many times: ' + err) } |
|
||||||
fds.forEach(function (fd) { fs.closeSync(fd) }) |
|
||||||
return cb() |
|
||||||
}) |
|
||||||
}, |
|
||||||
// Then actually test NeDB persistence
|
|
||||||
function () { |
|
||||||
db.remove({}, { multi: true }, function (err) { |
|
||||||
if (err) { console.log(err) } |
|
||||||
db.insert({ hello: 'world' }, function (err) { |
|
||||||
if (err) { console.log(err) } |
|
||||||
|
|
||||||
i = 0 |
for (let i = 0; i < 2 * N + 1; i++) { |
||||||
whilst(function () { return i < 2 * N + 1 } |
await db.persistence.persistCachedDatabaseAsync() |
||||||
, function (cb) { |
} |
||||||
callbackify(() => db.persistence.persistCachedDatabaseAsync())(function (err) { |
} catch (error) { |
||||||
if (err) { return cb(err) } |
console.error(`Got unexpected error during one persistence operation at ${i}: with error: ${error}`) |
||||||
i += 1 |
|
||||||
return cb() |
|
||||||
}) |
|
||||||
} |
|
||||||
, function (err) { |
|
||||||
if (err) { console.log('Got unexpected error during one peresistence operation: ' + err) } |
|
||||||
} |
|
||||||
) |
|
||||||
}) |
|
||||||
}) |
|
||||||
} |
} |
||||||
], () => {}) |
} |
||||||
|
try { |
||||||
|
test() |
||||||
|
} catch (error) { |
||||||
|
console.error(error) |
||||||
|
process.exit(1) |
||||||
|
} |
||||||
|
Loading…
Reference in new issue