Skip to content

Commit

Permalink
fixup! Update FsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
SemenchenkoVitaliy committed Feb 12, 2019
1 parent 5549149 commit 3195648
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 52 deletions.
2 changes: 0 additions & 2 deletions lib/fs.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ const THROTTLE_TIMEOUT = 5000;
class FsProvider extends StorageProvider {
// FsProvider constructor
// options - <Object>
// path - <string>, database location
constructor(options) {
super(options);
this.path = options.path;
this.stat = null;
this.storage = null;
this.dbOptions = {};
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
"metasync": "^0.3.31",
"pg": "^7.7.1"
},
"optionalDependencies": {
"mkdirp": "^0.5.1"
},
"devDependencies": {
"@babel/cli": "^7.2.0",
"@babel/core": "^7.2.0",
Expand Down
130 changes: 83 additions & 47 deletions test/fs.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,101 @@
'use strict';

const gs = require('..');

const fs = require('fs');

const metasync = require('metasync');
const metatests = require('metatests');
const { Uint64 } = require('@metarhia/common');

module.exports = (data, done) => {
metatests.test('fs test', test => {
const queue = metasync.queue(100, 100);
const provider = gs('fs', {
serverSuffix: new Uint64(0x4000000),
serverBitmask: new Uint64(0x7ffffff),
});

const processItem = (item, callback) => {
gs.create(item, () => {
item.Name = item.Id % 2 ? 'Marcus' : 'Aurelius';
gs.update(item, () => {
if (item.Id % 3) {
gs.delete(item.Id, callback);
} else {
callback();
const fsTest = metatests.test('FsProvider');

fsTest.beforeEach((test, cb) => {
provider.open({ path: './data' }, err => {
if (err) {
test.error(err, 'Cannot open database');
test.end();
return;
}
cb();
});
});

fsTest.afterEach((test, cb) =>
provider.close(() =>
setTimeout(
() =>
fs.writeFile(
'./data/.gs',
`{server:'0',count:0,size:0,next:0}`,
err => {
test.error(err);
cb();
}
});
});
};
),
6000
)
)
);

queue.process(processItem);
fsTest.test('fs test', test => {
const item = { Num: 123 };
const nextId = provider.stat.next;
provider.create(item, (err, id) => {
if (err) {
test.error(err);
test.end();
return;
}
test.strictSame(id, nextId);
test.strictSame(item.Id, nextId);
test.end();
});
});

gs.open(
{
gs,
provider: 'fs',
path: './data',
},
err => {
fsTest.test('fs test', test => {
const processItem = (item, callback) => {
provider.create(item, (err, id) => {
if (err) {
callback(err);
return;
}
item.Name = id % 2 ? 'Marcus' : 'Aurelius';
provider.update(item, err => {
if (err) {
test.error(err, 'error opening gs');
callback(err);
return;
}
console.time('insert');
for (let i = 0; i < 10; i++) {
queue.add({ Num: i });
if (id % 3) {
provider.delete(id, callback);
} else {
callback(null);
}
gs.select({ category: 'Person', Name: 'Marcus' })
.limit(10)
.fetch((err, data) => {
console.log(err);
if (err) {
test.throws(err, 'error fetching Marcus');
return;
}
test.strictSame([err, data], [null, []]);
test.end('select test end');
});
}
);
});
});
};

queue.timeout(1000, () => {
const queue = metasync
.queue(100, 100)
.process(processItem)
.timeout(1000, () => {
test.notOk('Insert test error, file writing timeout');
test.end();
})
.failure(err => {
test.error(err, 'insert test failed');
test.end();
})
.drain(() => {
test.end();
});

queue.drain(() => {
console.timeEnd('insert');
test.end('insert test done');
done();
});
});
};
for (let i = 0; i < 10; i++) {
queue.add({ Num: i });
}
});

0 comments on commit 3195648

Please sign in to comment.