{"size": 3174, "ext": "js", "max_stars_repo_path": "node_modules/pino/test/events.test.js", "max_stars_repo_name": "trendy77/autoJobbrr", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "node_modules/pino/test/events.test.js", "max_issues_repo_name": "trendy77/autoJobbrr", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "node_modules/pino/test/events.test.js", "max_forks_repo_name": "trendy77/autoJobbrr", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.0454545455, "max_line_length": 117, "alphanum_fraction": 0.6238185255}
'use strict'

var test = require('tap').test
var path = require('path')
var writeStream = require('flush-write-stream')
var fork = require('child_process').fork
var spawn = require('child_process').spawn
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.match(output, /"msg":"h"/)
  })
})

test('terminates when uncaughtException is fired with onTerminate registered', function (t) {
  t.plan(3)
  var output = ''
  var errorOutput = ''
  var child = spawn(process.argv[0], [path.join(fixturesPath, 'uncaught-exception.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.match(output, /"msg":"h"/)
    t.match(output, /terminated/g)
    t.match(errorOutput, /this is not caught/g)
  })
})

test('terminates when uncaughtException is fired without onTerminate registered', function (t) {
  t.plan(2)
  var output = ''
  var child = spawn(process.argv[0], [path.join(fixturesPath, 'uncaught-exception-no-terminate.js')], {silent: true})

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

  child.on('exit', function (code) {
    t.is(code, 1)
  })

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

test('terminates on SIGHUP when no other handlers registered', function (t) {
  t.plan(2)
  var output = ''
  var child = spawn(process.argv[0], [path.join(fixturesPath, 'sighup-no-handler.js')], {silent: true})

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

  child.stderr.pipe(process.stdout)

  child.on('exit', function (code) {
    t.is(code, 0)
  })

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

  setTimeout(function () { child.kill('SIGHUP') }, 2000)
})

test('lets app terminate when SIGHUP received with multiple handlers', function (t) {
  t.plan(3)
  var output = ''
  var child = spawn(process.argv[0], [path.join(fixturesPath, 'sighup-with-handler.js')], {silent: true})

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

  child.on('exit', function (code) {
    t.is(code, 0)
  })

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

  setTimeout(function () { child.kill('SIGHUP') }, 2000)
})

test('destination', function (t) {
  t.plan(2)
  var output = ''
  var child = spawn(process.argv[0], [path.join(fixturesPath, 'destination.js')], {silent: true})

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

  child.stderr.pipe(process.stdout)

  child.on('exit', function (code) {
    t.is(code, 0)
  })

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