Skip to content

Commit

Permalink
fix: only flush the queue after open if not already writing (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf authored Jan 30, 2023
1 parent 54a6a0d commit 97116ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ class WriteStream extends EE {
} else {
this[_fd] = fd
this.emit('open', fd)
this[_flush]()
if (!this[_writing]) {
this[_flush]()
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,33 @@ t.test('multiple writes', t => {
s.on('finish', () => check(t))
})
})

t.test('async after open, writev delayed', t => {
const _fsm = t.mock('../', {
fs: {
...fs,
writev: (...args) => {
setTimeout(fs.writev, 1000, ...args) // make writev very slow
},
},
})

const s = new _fsm.WriteStream(p)
s.on('open', fd => {
t.type(fd, 'number')
t.ok(s.write('a'))
t.notOk(s.write('b'))
t.notOk(s.write('c'))
t.notOk(s.write('d'))
t.notOk(s.write('e'))
t.notOk(s.write('f'))
t.notOk(s.write(Buffer.from('676869', 'hex')))
t.notOk(s.write('jklm'))
t.notOk(s.write(Buffer.from('nop')))
s.end()
s.on('finish', _ => check(t))
})
})
t.end()
})

Expand Down

0 comments on commit 97116ba

Please sign in to comment.