From 1335d31c479462e0adaaa9e35a2f1f1c34edb81f Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Wed, 25 Nov 2015 12:09:30 +0100 Subject: [PATCH] EMFILE test done --- test/persistence.test.js | 2 +- test_lac/openFds.test.js | 41 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/test/persistence.test.js b/test/persistence.test.js index d00c217..b1bae53 100755 --- a/test/persistence.test.js +++ b/test/persistence.test.js @@ -872,7 +872,7 @@ describe('Persistence', function () { }); }); - it.only("Cannot cause EMFILE errors by opening too many file descriptors", function (done) { + it("Cannot cause EMFILE errors by opening too many file descriptors", function (done) { child_process.execFile('test_lac/openFdsLaunch.sh', function (err, stdout, stderr) { if (err) { return done(err); } diff --git a/test_lac/openFds.test.js b/test_lac/openFds.test.js index 7ee937f..2e81805 100644 --- a/test_lac/openFds.test.js +++ b/test_lac/openFds.test.js @@ -1,8 +1,10 @@ var fs = require('fs') , child_process = require('child_process') , async = require('async') - , N = 64 // One file descriptor too many - , i =0 + , Nedb = require('../lib/datastore') + , db = new Nedb({ filename: './workspace/openfds.db', autoload: true }) + , N = 64 // Half the allowed file descriptors + , i, fds ; function multipleOpen (filename, N, callback) { @@ -10,23 +12,56 @@ function multipleOpen (filename, N, callback) { , function (cb) { fs.open(filename, 'r', function (err, fd) { i += 1; + if (fd) { fds.push(fd); } return cb(err); }); } , callback); } - async.waterfall([ + // Check that ulimit has been set to the correct value function (cb) { + i = 0; + fds = []; 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; + async.whilst( function () { return i < 2 * N + 1; } + , function (cb) { + db.persistence.persistCachedDatabase(function (err) { + if (err) { return cb(err); } + i += 1; + return cb(); + }); + } + , function (err) { + if (err) { console.log("Got unexpected error during one peresistence operation: " + err); } + } + ); + + }); + }); + } ]); +