UNPKG

2.14 kBJavaScriptView Raw
1var fs = require('fs')
2 , child_process = require('child_process')
3 , async = require('async')
4 , Nedb = require('../lib/datastore')
5 , db = new Nedb({ filename: './workspace/openfds.db', autoload: true })
6 , N = 64 // Half the allowed file descriptors
7 , i, fds
8 ;
9
10function multipleOpen (filename, N, callback) {
11 async.whilst( function () { return i < N; }
12 , function (cb) {
13 fs.open(filename, 'r', function (err, fd) {
14 i += 1;
15 if (fd) { fds.push(fd); }
16 return cb(err);
17 });
18 }
19 , callback);
20}
21
22async.waterfall([
23 // Check that ulimit has been set to the correct value
24 function (cb) {
25 i = 0;
26 fds = [];
27 multipleOpen('./test_lac/openFdsTestFile', 2 * N + 1, function (err) {
28 if (!err) { console.log("No error occured while opening a file too many times"); }
29 fds.forEach(function (fd) { fs.closeSync(fd); });
30 return cb();
31 })
32 }
33, function (cb) {
34 i = 0;
35 fds = [];
36 multipleOpen('./test_lac/openFdsTestFile2', N, function (err) {
37 if (err) { console.log('An unexpected error occured when opening file not too many times: ' + err); }
38 fds.forEach(function (fd) { fs.closeSync(fd); });
39 return cb();
40 })
41 }
42 // Then actually test NeDB persistence
43, function () {
44 db.remove({}, { multi: true }, function (err) {
45 if (err) { console.log(err); }
46 db.insert({ hello: 'world' }, function (err) {
47 if (err) { console.log(err); }
48
49 i = 0;
50 async.whilst( function () { return i < 2 * N + 1; }
51 , function (cb) {
52 db.persistence.persistCachedDatabase(function (err) {
53 if (err) { return cb(err); }
54 i += 1;
55 return cb();
56 });
57 }
58 , function (err) {
59 if (err) { console.log("Got unexpected error during one peresistence operation: " + err); }
60 }
61 );
62
63 });
64 });
65 }
66]);
67