From 68597d34147c6f39e16b39476a970b574a0ba663 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 11 Dec 2018 11:14:59 +0100 Subject: [PATCH] Added missing tests --- test/destination-exit.test.js | 23 ------------- test/exit.test.js | 53 +++++++++++++++++++++++++++++ test/final.test.js | 12 +++++++ test/fixtures/extreme-exit.js | 9 +++++ test/fixtures/extreme-flush-exit.js | 10 ++++++ 5 files changed, 84 insertions(+), 23 deletions(-) delete mode 100644 test/destination-exit.test.js create mode 100644 test/exit.test.js create mode 100644 test/fixtures/extreme-exit.js create mode 100644 test/fixtures/extreme-flush-exit.js diff --git a/test/destination-exit.test.js b/test/destination-exit.test.js deleted file mode 100644 index 7fdd1f5fd..000000000 --- a/test/destination-exit.test.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict' - -const { test } = require('tap') -const { join } = require('path') -const execa = require('execa') -const writer = require('flush-write-stream') -const { once } = require('./helper') - -// https://github.com/pinojs/pino/issues/542 -test('log everything when calling process.exit(0)', async ({ isNot }) => { - var actual = '' - const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'destination-exit.js')]) - - child.stdout.pipe(writer((s, enc, cb) => { - actual += s - cb() - })) - - await once(child, 'close') - - isNot(actual.match(/hello/), null) - isNot(actual.match(/world/), null) -}) diff --git a/test/exit.test.js b/test/exit.test.js new file mode 100644 index 000000000..fe7542a32 --- /dev/null +++ b/test/exit.test.js @@ -0,0 +1,53 @@ +'use strict' + +const { test } = require('tap') +const { join } = require('path') +const execa = require('execa') +const writer = require('flush-write-stream') +const { once } = require('./helper') + +// https://github.com/pinojs/pino/issues/542 +test('pino.destination log everything when calling process.exit(0)', async ({ isNot }) => { + var actual = '' + const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'destination-exit.js')]) + + child.stdout.pipe(writer((s, enc, cb) => { + actual += s + cb() + })) + + await once(child, 'close') + + isNot(actual.match(/hello/), null) + isNot(actual.match(/world/), null) +}) + +test('pino.extreme does not log everything when calling process.exit(0)', async ({ is }) => { + var actual = '' + const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'extreme-exit.js')]) + + child.stdout.pipe(writer((s, enc, cb) => { + actual += s + cb() + })) + + await once(child, 'close') + + is(actual.match(/hello/), null) + is(actual.match(/world/), null) +}) + +test('pino.extreme logs everything when calling flushSync', async ({ isNot }) => { + var actual = '' + const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'extreme-flush-exit.js')]) + + child.stdout.pipe(writer((s, enc, cb) => { + actual += s + cb() + })) + + await once(child, 'close') + + isNot(actual.match(/hello/), null) + isNot(actual.match(/world/), null) +}) diff --git a/test/final.test.js b/test/final.test.js index 40f4b1f80..abceb5380 100644 --- a/test/final.test.js +++ b/test/final.test.js @@ -54,6 +54,18 @@ test('listener function immediately sync flushes when fired', async ({ pass, fai if (passed === false) fail('flushSync not called') }) +test('listener function immediately sync flushes when fired (pino.destination)', async ({ pass, fail }) => { + const dest = pino.destination('/dev/null') + var passed = false + dest.flushSync = () => { + passed = true + pass('flushSync called') + } + pino.final(pino(dest), () => {})() + await sleep(10) + if (passed === false) fail('flushSync not called') +}) + test('swallows the non-ready error', async ({ doesNotThrow }) => { const dest = pino.extreme('/dev/null') doesNotThrow(() => { diff --git a/test/fixtures/extreme-exit.js b/test/fixtures/extreme-exit.js new file mode 100644 index 000000000..a90cc67c9 --- /dev/null +++ b/test/fixtures/extreme-exit.js @@ -0,0 +1,9 @@ +global.process = { __proto__: process, pid: 123456 } +Date.now = function () { return 1459875739796 } +require('os').hostname = function () { return 'abcdefghijklmnopqr' } +var pino = require(require.resolve('./../../')) +var dest = pino.extreme(1) +var logger = pino({}, dest) +logger.info('hello') +logger.info('world') +process.exit(0) diff --git a/test/fixtures/extreme-flush-exit.js b/test/fixtures/extreme-flush-exit.js new file mode 100644 index 000000000..3abf7e0ec --- /dev/null +++ b/test/fixtures/extreme-flush-exit.js @@ -0,0 +1,10 @@ +global.process = { __proto__: process, pid: 123456 } +Date.now = function () { return 1459875739796 } +require('os').hostname = function () { return 'abcdefghijklmnopqr' } +var pino = require(require.resolve('./../../')) +var dest = pino.extreme(1) +var logger = pino({}, dest) +logger.info('hello') +logger.info('world') +dest.flushSync() +process.exit(0)