diff --git a/lib/fs.provider.js b/lib/fs.provider.js index d4c12b1c..3971c233 100644 --- a/lib/fs.provider.js +++ b/lib/fs.provider.js @@ -14,10 +14,8 @@ const THROTTLE_TIMEOUT = 5000; class FsProvider extends StorageProvider { // FsProvider constructor // options - - // path - , database location constructor(options) { super(options); - this.path = options.path; this.stat = null; this.storage = null; this.dbOptions = {}; diff --git a/package.json b/package.json index af0070d3..c2edf061 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/fs.js b/test/fs.js index cef73e7b..52531bb8 100644 --- a/test/fs.js +++ b/test/fs.js @@ -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 }); + } +});