-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5549149
commit 3195648
Showing
3 changed files
with
83 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}); |