Skip to content

Commit

Permalink
periodicFlush: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
user committed Aug 6, 2024
1 parent 14db716 commit 051d37a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function SonicBoom (opts) {
this.maxLength = maxLength || 0
this.maxWrite = maxWrite || MAX_WRITE
this._periodicFlush = periodicFlush || 0
this._periodicFlushIID = 0
this._periodicFlushTimer = undefined
this.sync = sync || false
this.writable = true
this._fsync = fsync || false
Expand Down Expand Up @@ -246,7 +246,8 @@ function SonicBoom (opts) {
})

if (this._periodicFlush !== 0) {
this._periodicFlushIID = setInterval(() => this.flush(null), this._periodicFlush)
this._periodicFlushTimer = setInterval(() => this.flush(null), this._periodicFlush)
this._periodicFlushTimer.unref()
}
}

Expand Down Expand Up @@ -652,8 +653,8 @@ function actualClose (sonic) {
return
}

if (sonic._periodicFlushIID !== 0) {
clearInterval(sonic._periodicFlushIID)
if (sonic._periodicFlushTimer !== undefined) {
clearInterval(sonic._periodicFlushTimer)
}

sonic.destroyed = true
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"homepage": "https://github.com/pinojs/sonic-boom#readme",
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"@sinonjs/fake-timers": "^11.2.2",
"@types/node": "^22.0.0",
"fastbench": "^1.0.1",
"proxyquire": "^2.1.3",
Expand Down
9 changes: 9 additions & 0 deletions test/periodicflush.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const FakeTimers = require('@sinonjs/fake-timers')
const fs = require('fs')
const SonicBoom = require('../')
const { file, runTests } = require('./helper')
Expand All @@ -13,6 +14,7 @@ function buildTests (test, sync) {
test('periodicflush_off', (t) => {
t.plan(4)

const clock = FakeTimers.install()
const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom({ fd, sync, minLength: 5000 })
Expand All @@ -28,11 +30,15 @@ function buildTests (test, sync) {
t.pass('file empty')
})
}, 2000)

clock.tick(2000)
clock.uninstall()
})

test('periodicflush_on', (t) => {
t.plan(4)

const clock = FakeTimers.install()
const dest = file()
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom({ fd, sync, minLength: 5000, periodicFlush: 1000 })
Expand All @@ -48,5 +54,8 @@ function buildTests (test, sync) {
t.pass('file not empty')
})
}, 2000)

clock.tick(2000)
clock.uninstall()
})
}
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type SonicBoomOpts = {
maxLength?: number
minLength?: number
maxWrite?: number
periodicFlush?: number
sync?: boolean
fsync?: boolean
append?: boolean
Expand Down
2 changes: 1 addition & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sonic.end();
sonic.destroy();

const extraSonic = new SonicBoom({fd: 1, minLength: 0, maxWrite: 16384, sync: true, append: true, mode: 0o644, mkdir: true});
new SonicBoom({fd: 1, minLength: 0, maxWrite: 16384, sync: true, append: true, mode: 0o644, mkdir: true, dest: '/dev/null'});
new SonicBoom({fd: 1, minLength: 0, maxWrite: 16384, periodicFlush: 15000, sync: true, append: true, mode: 0o644, mkdir: true, dest: '/dev/null'});
new SonicBoom({fd: 1, minLength: 0, maxWrite: 16384, sync: true, append: true, mode: 0o644, mkdir: true, dest: 1});
new SonicBoom({fd: 1, minLength: 0, maxWrite: 16384, sync: true, fsync: true, append: true, mode: 0o644, mkdir: true});
new SonicBoom({fd: 1, minLength: 0, maxWrite: 16384, sync: true, fsync: true, append: true, mode: 0o644, mkdir: true, contentMode: 'buffer' });
Expand Down

0 comments on commit 051d37a

Please sign in to comment.