diff --git a/index.js b/index.js index 5b2edee..fadb037 100644 --- a/index.js +++ b/index.js @@ -249,7 +249,10 @@ ReadRequest.prototype.onread = function (err, buf) { const req = this.req if (err && this.retry) { - this.retry = false + if (err.code !== 0) { + this.retry = false + } + if (this.lock(this)) { this.file.clearFile() this.run(req) @@ -294,7 +297,7 @@ class EntryFile { } updateSize (size) { - if (!this._truncated) { + if (!this._truncated && size > this._size) { this._size = size } diff --git a/test.js b/test.js index f1d67b8..1793044 100644 --- a/test.js +++ b/test.js @@ -1,3 +1,4 @@ +const { promisify } = require('util') const test = require('tape') const randomAccessTest = require('random-access-test') const racf = require('./') @@ -14,20 +15,8 @@ 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() - }) - }) + const read = promisify(st.read.bind(st)) + const write = promisify(st.write.bind(st)) try { await new Promise(resolve => st.open(() => resolve())) @@ -55,3 +44,24 @@ test('write/read concurrent requests', async t => { t.end(err) } }) + +test('write concurrent requests over the same offset different size', async t => { + const st = storage('random') + + const write = promisify(st.write.bind(st)) + + try { + await new Promise(resolve => st.open(() => resolve())) + + await Promise.all([ + write(0, Buffer.alloc(10)), + write(0, Buffer.alloc(1)), + write(0, Buffer.alloc(5)) + ]) + + t.pass('should write multiple requests over the same offset different size') + t.end() + } catch (err) { + t.end(err) + } +})