{"size": 1661, "ext": "js", "max_stars_repo_path": "test/unit/catch-exit.js", "max_stars_repo_name": "iandotkelly/spam", "max_stars_repo_stars_event_min_datetime": "2020-07-28T15:33:40.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-28T15:33:40.000Z", "max_issues_repo_path": "test/unit/catch-exit.js", "max_issues_repo_name": "iandotkelly/spam", "max_issues_repo_issues_event_min_datetime": "2019-07-04T04:35:08.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-07T10:33:49.000Z", "max_forks_repo_path": "test/unit/catch-exit.js", "max_forks_repo_name": "iandotkelly/spam", "max_forks_repo_forks_event_min_datetime": "2018-05-03T05:00:39.000Z", "max_forks_repo_forks_event_max_datetime": "2018-05-03T05:00:39.000Z"}
{"max_stars_count": 1, "max_issues_count": 5, "max_forks_count": 1, "avg_line_length": 22.4459459459, "max_line_length": 75, "alphanum_fraction": 0.6399759181}
/*jshint -W068 */

/**
 * @description Unit tests for the spam.js module
 */


'use strict';

var path = require('path'),
	spamRootPath = path.join(__dirname, '../../index.js'),
	spamPath = path.join(__dirname, '../../lib/spam.js'),
	spam;

// make sure no pre-existing SPAM is in the cache
if (require.cache[spamPath]) {
	delete require.cache[spamPath];
}

if (require.cache[spamRootPath]) {
	delete require.cache[spamRootPath];
}

spam = require('../..');
require('should');

describe('spam with a script that will exit every 5 seconds', function () {

	var exits = [], spawnCount = 0;

	this.timeout(12000);

	// count exits
	spam.on('status', function (status) {
		if (status.event === 'spawn') {
			spawnCount++;
		} else if (status.event === 'exit') {
			exits.push(status);
		}
	});

	before(function () {
		spam.setScript('./test/fixtures/worker');
		// we want to run the child script with the listener result
		process.env['SPAMTEST'] = 'badexit';
	});

	it('will force the failing script to restart', function (done) {
		spam.children.length.should.be.equal(0);
		exits.length.should.be.equal(0);
		spawnCount.should.be.equal(0);
		spam.spawn({ timeout: 0, readyOn: 'ready', number: 1 }, function (err) {
			if (err) {
				throw err;
			}
			spawnCount.should.be.equal(1);
			spam.children.length.should.be.equal(1);
			exits.length.should.be.equal(0);

			// wait 6 seconds for it to fail
			setTimeout(function () {
				exits.length.should.be.equal(1);
				exits[0].id.should.be.equal(1);
				exits[0].event.should.be.equal('exit');
				exits[0].code.should.be.equal(1);
				spawnCount.should.be.equal(2);
				done();
			}, 6000);
		});
	});


});
