From 542daa48c1206241428bf015a45285ee4ce9a7ae Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 9 May 2019 08:15:33 +1000 Subject: [PATCH] fix: sighup handler was just nonsense --- lib/appenders/file.js | 4 ++-- test/tap/file-sighup-test.js | 42 ++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/appenders/file.js b/lib/appenders/file.js index e392776..70c0c2c 100644 --- a/lib/appenders/file.js +++ b/lib/appenders/file.js @@ -49,14 +49,14 @@ function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset timezoneOffset, ')' ); - const writer = openTheStream(file, logSize, numBackups, options); + let writer = openTheStream(file, logSize, numBackups, options); const app = function (loggingEvent) { writer.write(layout(loggingEvent, timezoneOffset) + eol, 'utf8'); }; app.reopen = function () { - writer.closeTheStream(writer.openTheStream.bind(writer)); + writer.end(() => { writer = openTheStream(file, logSize, numBackups, options); }); }; app.sighupHandler = function () { diff --git a/test/tap/file-sighup-test.js b/test/tap/file-sighup-test.js index b96d486..06d469a 100644 --- a/test/tap/file-sighup-test.js +++ b/test/tap/file-sighup-test.js @@ -7,42 +7,46 @@ test('file appender SIGHUP', (t) => { let closeCalled = 0; let openCalled = 0; - sandbox.require( + const appender = sandbox.require( '../../lib/appenders/file', { requires: { streamroller: { - RollingFileStream: function () { - this.openTheStream = function () { + RollingFileStream: class RollingFileStream { + constructor() { openCalled++; - }; + this.ended = false; + } - this.closeTheStream = function (cb) { + on() { + this.dummy = 'easier than turning off lint rule'; + } + + end(cb) { + this.ended = true; closeCalled++; - if (cb) { - cb(); + cb(); + } + + write() { + if (this.ended) { + throw new Error('write after end'); } - }; - - this.on = function () { - }; - - this.end = function () { - }; - - this.write = function () { return true; - }; + } } } } } - ).configure({ type: 'file', filename: 'sighup-test-file' }, { basicLayout: function () {} }); + ).configure({ type: 'file', filename: 'sighup-test-file' }, { basicLayout: function () { return 'whatever'; } }); + appender('something to log'); process.kill(process.pid, 'SIGHUP'); + t.plan(2); setTimeout(() => { - t.equal(openCalled, 1, 'open should be called once'); + appender('something to log after sighup'); + t.equal(openCalled, 2, 'open should be called twice'); t.equal(closeCalled, 1, 'close should be called once'); t.end(); }, 100);