Skip to content

Commit

Permalink
Test no event loop and fd settling
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Feb 7, 2017
1 parent 06cf7ba commit 8044371
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 28 deletions.
59 changes: 31 additions & 28 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,39 @@ function pino (opts, stream) {
iopts.level = 'silent'
}

var logger = new Pino(iopts, istream)
if (iopts.cache) {
// setImmediate is causing a very weird crash:
// Assertion failed: (cb_v->IsFunction()), function MakeCallback...
// but setTimeout isn't *shrug*
setTimeout(function () {
if (!tools.streamIsBlockable(istream)) {
logger.emit('error', new Error('stream must have a file descriptor in extreme mode'))
}
}, 100)

events.onExit(function (code, evt) {
var buf = iopts.cache.buf
if (buf) {
// We need to block the process exit long enough to flush the buffer
// to the destination stream. We do that by forcing a synchronous
// write directly to the stream's file descriptor.
var fd = (istream.fd) ? istream.fd : istream._handle.fd
fs.writeSync(fd, buf)
}
if (!process._events[evt] || process._events[evt].length < 2 || !process._events[evt].filter(function (f) {
return f + '' !== events.onExit.passCode + '' && f + '' !== events.onExit.insertCode + ''
}).length) {
process.exit(code)
} else {
return 'no exit'
}
})
var settleTries = 0
function waitForFDSettle () {
var isBlockable = tools.streamIsBlockable(istream)
if (isBlockable === false && settleTries > 10) {
return logger.emit('error', Error('stream must have a file descriptor in extreme mode'))
} else if (isBlockable === true) {
return events.onExit(extremeModeExitHandler)
}
settleTries += 1
setTimeout(waitForFDSettle, 100)
}

function extremeModeExitHandler (code, evt) {
var buf = iopts.cache.buf
if (buf) {
// We need to block the process exit long enough to flush the buffer
// to the destination stream. We do that by forcing a synchronous
// write directly to the stream's file descriptor.
var fd = (istream.fd) ? istream.fd : istream._handle.fd
fs.writeSync(fd, buf)
}
if (!process._events[evt] || process._events[evt].length < 2 || !process._events[evt].filter(function (f) {
return f + '' !== events.onExit.passCode + '' && f + '' !== events.onExit.insertCode + ''
}).length) {
process.exit(code)
} else {
return 'no exit'
}
}

var logger = new Pino(iopts, istream)
if (iopts.cache) setTimeout(waitForFDSettle, 100)

return logger
}

Expand Down
44 changes: 44 additions & 0 deletions test/events.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

var test = require('tap').test
var path = require('path')
var writeStream = require('flush-write-stream')
var fork = require('child_process').fork
var fixturesPath = path.join(__dirname, 'fixtures', 'events')

test('no event loop logs successfully', function (t) {
t.plan(1)
var output = ''
var child = fork(path.join(fixturesPath, 'no-event-loop.js'), {silent: true})

child.stdout.pipe(writeStream(function (s, enc, cb) {
output += s
cb()
}))

child.on('close', function () {
t.notEqual(output.match(/"msg":"h"/), null)
})
})

test('handles no file descriptor in extreme mode', function (t) {
t.plan(2)
var output = ''
var errorOutput = ''
var child = fork(path.join(fixturesPath, 'no-fd.js'), {silent: true})

child.stdout.pipe(writeStream(function (s, enc, cb) {
output += s
cb()
}))

child.stderr.pipe(writeStream(function (s, enc, cb) {
errorOutput += s
cb()
}))

child.on('close', function () {
t.is(output, '')
t.notEqual(errorOutput.match(/stream must have/g), null)
})
})
6 changes: 6 additions & 0 deletions test/fixtures/events/no-event-loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global.process = { __proto__: process, pid: 123456 }
Date.now = function () { return 1459875739796 }
require('os').hostname = function () { return 'abcdefghijklmnopqr' }
var pino = require(require.resolve('./../../../'))
var log = pino()
log.info('h')
9 changes: 9 additions & 0 deletions test/fixtures/events/no-fd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global.process = { __proto__: process, pid: 123456 }
Date.now = function () { return 1459875739796 }
require('os').hostname = function () { return 'abcdefghijklmnopqr' }
var stream = require('stream')
var dest = new stream.PassThrough()
dest.pipe(process.stdout)
var pino = require(require.resolve('./../../../'))
var log = pino({extreme: true}, dest)
log.info('h')

0 comments on commit 8044371

Please sign in to comment.