-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed crash on random concurrent requests
- Loading branch information
Showing
3 changed files
with
86 additions
and
33 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,10 +1,57 @@ | ||
const test = require('random-access-test') | ||
const test = require('tape') | ||
const randomAccessTest = require('random-access-test') | ||
const racf = require('./') | ||
|
||
const createStorage = (root) => (file, opts) => racf(`${root}/${file}`, opts) | ||
|
||
const storage = createStorage('tests-' + Math.random()) | ||
|
||
test(function (name, options, callback) { | ||
randomAccessTest(function (name, options, callback) { | ||
callback(storage(name, options)) | ||
}, {}) | ||
|
||
test('write/read concurrent requests', async t => { | ||
const st = storage('random') | ||
|
||
const rand = (min, max) => Math.floor(Math.random() * max) + min | ||
|
||
const read = (...args) => new Promise((resolve, reject) => { | ||
st.read(...args, (err) => { | ||
if (err) return reject(err) | ||
resolve() | ||
}) | ||
}) | ||
|
||
const write = (...args) => new Promise((resolve, reject) => { | ||
st.write(...args, (err) => { | ||
if (err) return reject(err) | ||
resolve() | ||
}) | ||
}) | ||
|
||
try { | ||
await new Promise(resolve => st.open(() => resolve())) | ||
|
||
const buf = Buffer.alloc(1) | ||
|
||
await Promise.all([...Array(1000).keys()].map(from => { | ||
return write(from, buf) | ||
})) | ||
|
||
await Promise.all([...Array(1000).keys()].map(() => { | ||
const row = rand(0, 2) | ||
const from = rand(0, 1000) | ||
const to = 1 | ||
|
||
if (row === 0) { | ||
return read(from, to) | ||
} | ||
return write(from, buf) | ||
})) | ||
|
||
t.pass('should work ok with random concurrent request') | ||
t.end() | ||
} catch (err) { | ||
t.end(err) | ||
} | ||
}) |