<commit_before>/* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */

"use strict";

var rewire = require('rewire');
var should = require('should');
var common = require(__dirname + '/mock.js');

var Child = rewire(__dirname + '/../lib/child.js');

var PROCESS;
beforeEach(function () {

  common.resetAllStatic();

  PROCESS = common.mockProcess();
  PROCESS.makesureCleanAllMessage();
  PROCESS.__getOutMessage().should.eql([]);
  PROCESS.__getEvents().should.eql([]);

  var sub = common.mockFork();
  sub.makesureCleanAllMessage;
  Child.__set__({
    'PROCESS' : PROCESS,
    'fork'  : common.mockFork,
  });
});

describe('child manager', function () {

  /* {{{ should_public_method_works_fine() */
  it('should_public_method_works_fine', function (_done) {
    var _me = Child.create(['filename.js', 'b'], {
      'listen' : __dirname + '/a.socket,,33047', 'children' : 3,
    });

    var _messages = [];
    _me.on('fork', function (pid) {
      _messages.push(['fork', pid]);
    });
    _me.on('broadcast', function (who, msg, by) {
      _messages.push(['broadcast', who, JSON.stringify(msg), by]);
    });

    var done = function () {
      _messages.should.eql([
        ['fork', 1],
        ['fork', 2],
        ['fork', 3],
        ['fork', 4],
        ['fork', 5],
        ['fork', 6],
        ['broadcast', 'FBX', '"fuck"', 1],
        ]);
      _done();
    };

    common.resetAllStatic();

    var one = _me._fork();
    one.__getArguments().should.eql({
      '0' : 'filename.js',
      '1' : ['b'],
      '2' : {
        'cwd' : __dirname, 'env' : {'test-env' : 'lalal'}
      }
    });

    _me.start();
    _me.running.should.eql(1);
    Object.keys(_me.pstatus).should.eql(['1', '2', '3']);

    _me.reload();
    _me.start();
    Object.keys(_me.dielist).should.eql(['1', '2', '3']);
    Object.keys(_me.pstatus).should.eql(['1', '2', '3', '4', '5', '6']);

    _me.broadcast(['world'], 'Obama', -1);

    var expects = [];
    for (var i = 1; i < 7; i++) {
      expects.push(JSON.stringify([i, {
        'type' : 'hello',
        'data' : ['world'],
        'from' : 'Obama',
        '_pid' : -1,
      }, null]));
    }
    one.__getGlobalMessage().should.eql(expects);

    one.emit('message');
    one.emit('message', {'data' : 1});

    /**
     * @ 心跳信息
     */
    one.emit('message', {'type' : 'heartbeat', 'data' : {'aa' : 1}});
    _me.pstatus['1'].should.have.property('aa', 1);
    _me.pstatus['1'].should.have.property('_time');

    /**
     * @ 请求广播
     */
    one.emit('message', {'type' : 'broadcast', 'data' : {'who' : 'FBX'}});
    one.emit('message', {'type' : 'broadcast', 'data' : {'who' : 'FBX', 'msg' : 'fuck'}});

    one.__getOutMessage().pop();

    /**
     * @ 请求句柄
     */
    one.emit('message', {'type' : 'ready'});
    one.__getOutMessage().pop()[0].should.eql({'type' : 'listen', 'data' : 33047});

    setTimeout(function () {
      Object.keys(_me.dielist).should.eql(['2', '3']);
      Object.keys(_me.pstatus).should.eql(['2', '3', '4', '5', '6']);
      _me.stop();
      _me.running.should.eql(0);
      done();
    }, 10);
  });
  /* }}} */

  /**
   * XXX: 这个case逻辑上还有点问题
   */
  /* {{{ should_max_fatals_works_fine() */
  it('should_max_fatals_works_fine', function (done) {

    common.resetAllStatic();
<commit_msg>found bug in reload, only the first process in dielist will be kill<commit_after>/* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */

"use strict";

var rewire = require('rewire');
var should = require('should');
var common = require(__dirname + '/mock.js');

var Child = rewire(__dirname + '/../lib/child.js');

var PROCESS;
beforeEach(function () {

  common.resetAllStatic();

  PROCESS = common.mockProcess();
  PROCESS.makesureCleanAllMessage();
  PROCESS.__getOutMessage().should.eql([]);
  PROCESS.__getEvents().should.eql([]);

  var sub = common.mockFork();
  sub.makesureCleanAllMessage;
  Child.__set__({
    'PROCESS' : PROCESS,
    'fork'  : common.mockFork,
  });
});

describe('child manager', function () {

  /* {{{ should_public_method_works_fine() */
  it('should_public_method_works_fine', function (_done) {
    var _me = Child.create(['filename.js', 'b'], {
      'listen' : __dirname + '/a.socket,,33047', 'children' : 3,
    });

    var _messages = [];
    _me.on('fork', function (pid) {
      _messages.push(['fork', pid]);
    });
    _me.on('broadcast', function (who, msg, by) {
      _messages.push(['broadcast', who, JSON.stringify(msg), by]);
    });

    var done = function () {
      _messages.should.eql([
        ['fork', 1],
        ['fork', 2],
        ['fork', 3],
        ['fork', 4],
        ['fork', 5],
        ['fork', 6],
        ['broadcast', 'FBX', '"fuck"', 1],
        ]);
      _done();
    };

    common.resetAllStatic();

    var one = _me._fork();
    one.__getArguments().should.eql({
      '0' : 'filename.js',
      '1' : ['b'],
      '2' : {
        'cwd' : __dirname, 'env' : {'test-env' : 'lalal'}
      }
    });

    _me.start();
    _me.running.should.eql(1);
    Object.keys(_me.pstatus).should.eql(['1', '2', '3']);

    _me.reload();
    _me.start();
    Object.keys(_me.dielist).should.eql(['1', '2', '3']);
    Object.keys(_me.pstatus).should.eql(['1', '2', '3', '4', '5', '6']);

    _me.broadcast(['world'], 'Obama', -1);

    var expects = [];
    for (var i = 1; i < 7; i++) {
      expects.push(JSON.stringify([i, {
        'type' : 'hello',
        'data' : ['world'],
        'from' : 'Obama',
        '_pid' : -1,
      }, null]));
    }
    one.__getGlobalMessage().should.eql(expects);

    one.emit('message');
    one.emit('message', {'data' : 1});

    /**
     * @ 心跳信息
     */
    one.emit('message', {'type' : 'heartbeat', 'data' : {'aa' : 1}});
    _me.pstatus['1'].should.have.property('aa', 1);
    _me.pstatus['1'].should.have.property('_time');

    /**
     * @ 请求广播
     */
    one.emit('message', {'type' : 'broadcast', 'data' : {'who' : 'FBX'}});
    one.emit('message', {'type' : 'broadcast', 'data' : {'who' : 'FBX', 'msg' : 'fuck'}});

    one.__getOutMessage().pop();

    /**
     * @ 请求句柄
     */
    one.emit('message', {'type' : 'ready'});
    one.__getOutMessage().pop()[0].should.eql({'type' : 'listen', 'data' : 33047});

    /**
     * @ 模拟两个进程ready，dielist应该至少杀掉2个
     */
    one.emit('message', {'type' : 'ready'});
    setTimeout(function () {
      Object.keys(_me.dielist).length.should.below(2);
      Object.keys(_me.pstatus).should.eql(['2', '3', '4', '5', '6']);
      _me.stop();
      _me.running.should.eql(0);
      done();
    }, 10);
  });
  /* }}} */

  /**
   * XXX: 这个case逻辑上还有点问题
   */
  /* {{{ should_max_fatals_works_fine() */
  it('should_max_fatals_works_fine', function (done) {

    common.resetAllStatic();

{'fraction_non_alphanumeric': 0.2341715976331361, 'fraction_numerical': 0.016568047337278107, 'mean_word_length': 2.7291781577495864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3173969', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2501, 'n_words': 425}