From 051d37a5abf4480483614a407b609901cf2e17f0 Mon Sep 17 00:00:00 2001 From: user <> Date: Tue, 6 Aug 2024 11:56:37 +0300 Subject: [PATCH] periodicFlush: improvements --- index.js | 9 +++++---- package.json | 1 + test/periodicflush.test.js | 9 +++++++++ types/index.d.ts | 1 + types/index.test-d.ts | 2 +- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index b09d819..6fc2d99 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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() } } @@ -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 diff --git a/package.json b/package.json index 17cee7c..7b477bd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/periodicflush.test.js b/test/periodicflush.test.js index 24f77ac..5c0e093 100644 --- a/test/periodicflush.test.js +++ b/test/periodicflush.test.js @@ -1,5 +1,6 @@ 'use strict' +const FakeTimers = require('@sinonjs/fake-timers') const fs = require('fs') const SonicBoom = require('../') const { file, runTests } = require('./helper') @@ -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 }) @@ -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 }) @@ -48,5 +54,8 @@ function buildTests (test, sync) { t.pass('file not empty') }) }, 2000) + + clock.tick(2000) + clock.uninstall() }) } diff --git a/types/index.d.ts b/types/index.d.ts index 9df073d..97057f6 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -12,6 +12,7 @@ export type SonicBoomOpts = { maxLength?: number minLength?: number maxWrite?: number + periodicFlush?: number sync?: boolean fsync?: boolean append?: boolean diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 564f7f2..1469a4a 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -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' });