Skip to content

Commit

Permalink
revert tests to original and update comment and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
admataz authored and mcollina committed Jul 15, 2020
1 parent 8b112cb commit 2899c37
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 69 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ The options are:
* `sync`: perform writes synchronously (similar to `console.log`).

For `sync:false` a `SonicBoom` instance will emit the `'ready'` event when a file descriptor is available.
For `sync:true` the file descriptor will be available when the instance is created, before the `ready` event is fired.
For `sync:true` this is not relevant because the `'ready'` event will be fired when the `SonicBoom` instance is created, before it can be subscribed to.


### SonicBoom#write(string)

Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function openFile (file, sonic) {
sonic._writing = true
sonic.file = file

// NOTE: 'error' and 'ready' events emitted below only relevant when sonic.sync===false
// for sync mode, there is no way to add a listener that will receive these

function fileOpened (err, fd) {
if (err) {
sonic.emit('error', err)
Expand Down
126 changes: 58 additions & 68 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ function buildTests (test, sync) {
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom({ fd, sync })

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

stream.on('ready', onReady)
})

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))
Expand Down Expand Up @@ -131,11 +129,9 @@ function buildTests (test, sync) {
const dest = file()
const stream = new SonicBoom({ dest, sync })

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

stream.on('ready', onReady)
})

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))
Expand Down Expand Up @@ -220,11 +216,9 @@ function buildTests (test, sync) {
const dest = file()
const stream = new SonicBoom({ dest, minLength: 4096, sync })

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

stream.on('ready', onReady)
})

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))
Expand Down Expand Up @@ -262,11 +256,9 @@ function buildTests (test, sync) {
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom({ fd, minLength: 4096, sync })

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

stream.on('ready', onReady)
})

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))
Expand Down Expand Up @@ -299,25 +291,22 @@ function buildTests (test, sync) {
fs.renameSync(dest, after)
stream.reopen()

const onDrain = () => {
fs.readFile(after, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'hello world\nsomething else\n')
fs.readFile(dest, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'after reopen\n')
stream.end()
})
})
}

const onReady = () => {
stream.once('ready', () => {
t.pass('ready emitted')
t.ok(stream.write('after reopen\n'))
stream.on('drain', onDrain)
}

stream.once('ready', onReady)
stream.on('drain', () => {
fs.readFile(after, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'hello world\nsomething else\n')
fs.readFile(dest, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'after reopen\n')
stream.end()
})
})
})
})
})
})

Expand All @@ -332,35 +321,31 @@ function buildTests (test, sync) {

const after = dest + '-moved'

const innerOnReady = () => {
t.pass('ready emitted')
t.ok(stream.write('after reopen\n'))
stream.flush()

stream.on('drain', () => {
fs.readFile(after, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'hello world\nsomething else\n')
fs.readFile(dest, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'after reopen\n')
stream.end()
})
})
})
}

const onReady = () => {
stream.once('ready', () => {
t.pass('drain emitted')

stream.flush()
fs.renameSync(dest, after)
stream.reopen()

stream.once('ready', innerOnReady)
}
stream.once('ready', () => {
t.pass('ready emitted')
t.ok(stream.write('after reopen\n'))
stream.flush()

stream.once('ready', onReady)
stream.on('drain', () => {
fs.readFile(after, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'hello world\nsomething else\n')
fs.readFile(dest, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'after reopen\n')
stream.end()
})
})
})
})
})
})

test('reopen if not open', (t) => {
Expand All @@ -385,7 +370,8 @@ function buildTests (test, sync) {

const dest = file()
const stream = new SonicBoom({ dest, minLength: 4096, sync })
const onReady = () => {

stream.once('ready', () => {
t.pass('ready emitted')
const after = dest + '-moved'
stream.reopen(after)
Expand All @@ -398,9 +384,7 @@ function buildTests (test, sync) {
})
})
stream.end()
}

stream.once('ready', onReady)
})
})

test('end after 2x reopen', (t) => {
Expand All @@ -409,7 +393,7 @@ function buildTests (test, sync) {
const dest = file()
const stream = new SonicBoom({ dest, minLength: 4096, sync })

const onReady = () => {
stream.once('ready', () => {
t.pass('ready emitted')
stream.reopen(dest + '-moved')
const after = dest + '-moved-moved'
Expand All @@ -423,9 +407,7 @@ function buildTests (test, sync) {
})
})
stream.end()
}

stream.once('ready', onReady)
})
})

test('end if not ready', (t) => {
Expand Down Expand Up @@ -461,7 +443,8 @@ function buildTests (test, sync) {
t.pass('drain emitted')

stream.reopen(after)
const onReady = () => {

stream.once('ready', () => {
t.pass('ready emitted')
t.ok(stream.write('after reopen\n'))

Expand All @@ -476,9 +459,7 @@ function buildTests (test, sync) {
})
})
})
}

stream.once('ready', onReady)
})
})
})

Expand Down Expand Up @@ -544,7 +525,7 @@ test('retry on EAGAIN', (t) => {
})

test('retry on EAGAIN (sync)', (t) => {
t.plan(6)
t.plan(7)

const fakeFs = Object.create(fs)
fakeFs.writeSync = function (fd, buf, enc, cb) {
Expand All @@ -562,6 +543,10 @@ test('retry on EAGAIN (sync)', (t) => {
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom({ fd, minLength: 0, sync: true })

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

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

Expand Down Expand Up @@ -597,6 +582,7 @@ test('write buffers that are not totally written', (t) => {
const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom({ fd, minLength: 0, sync: false })

stream.on('ready', () => {
t.pass('ready emitted')
})
Expand All @@ -618,7 +604,7 @@ test('write buffers that are not totally written', (t) => {
})

test('write buffers that are not totally written with sync mode', (t) => {
t.plan(8)
t.plan(9)

const fakeFs = Object.create(fs)
fakeFs.writeSync = function (fd, buf, enc) {
Expand All @@ -637,6 +623,10 @@ test('write buffers that are not totally written with sync mode', (t) => {
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom({ fd, minLength: 0, sync: true })

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

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

Expand Down

0 comments on commit 2899c37

Please sign in to comment.