Skip to content

Commit

Permalink
keep writable while fs.write throws
Browse files Browse the repository at this point in the history
  • Loading branch information
xiedacon committed Mar 13, 2021
1 parent ce7d7a1 commit 04635c0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ function SonicBoom (opts) {
}, BUSY_WRITE_TIMEOUT)
}
} else {
// The error maybe recoverable later, so just put data back to this._buf
this._buf = this._writingBuf + this._buf
this._writingBuf = ''
this._writing = false

this.emit('error', err)
}
return
Expand Down
55 changes: 55 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,61 @@ function buildTests (test, sync) {
t.is(code, 0)
})
})

test('write later on recoverable error', (t) => {
t.plan(8)

const fakeFs = Object.create(fs)
const SonicBoom = proxyquire('.', {
fs: fakeFs
})

const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom({ fd, minLength: 0, sync })

stream.on('ready', () => {
t.pass('ready emitted')
})
stream.on('error', () => {
t.pass('error emitted')
})

if (sync) {
fakeFs.writeSync = function (fd, buf, enc) {
t.pass('fake fs.writeSync called')
throw new Error('recoverable error')
}
} else {
fakeFs.write = function (fd, buf, enc, cb) {
t.pass('fake fs.write called')
setTimeout(() => cb(new Error('recoverable error')), 0)
}
}

t.ok(stream.write('hello world\n'))

setTimeout(() => {
if (sync) {
fakeFs.writeSync = fs.writeSync
} else {
fakeFs.write = fs.write
}

t.ok(stream.write('something else\n'))

stream.end()
stream.on('finish', () => {
fs.readFile(dest, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'hello world\nsomething else\n')
})
})
stream.on('close', () => {
t.pass('close emitted')
})
}, 0)
})
}

test('retry on EAGAIN', (t) => {
Expand Down

0 comments on commit 04635c0

Please sign in to comment.