<commit_before>var assert = require('assert'),
    ChildPool = require('../lib/child-pool');

describe('child-pool', function() {
  beforeEach(function() {
    ChildPool.isBackground(false);
  });

  it('should run child', function(done) {
    var pool = new ChildPool(__dirname + '/artifacts/child-worker');

    pool.send('bar', function(err, data) {
      assert.equal(err, undefined);
      assert.deepEqual(data, {data: 'foo', input: 'bar'});
      done();
    });
  });

  describe('limit', function() {
    it('should run up to pool limit', function(done) {
      var pool = new ChildPool(__dirname + '/artifacts/child-worker', {workers: 2});

      exec(10, pool, done);
      assert.equal(pool.workers.length, 2);
    });
    it('should run up to global limit', function(done) {
      var numCPUs = require('os').cpus().length,
          pool = new ChildPool(__dirname + '/artifacts/child-worker', {workers: numCPUs+2});

      exec(numCPUs+2, pool, done);
      assert.equal(pool.workers.length, numCPUs);
    });
    it('should run up to the global background limit', function(done) {
      var numCPUs = require('os').cpus().length,
          pool = new ChildPool(__dirname + '/artifacts/child-worker', {workers: numCPUs+2});

      ChildPool.isBackground(true);

      exec(numCPUs+2, pool, done);
      assert.equal(pool.workers.length, numCPUs-1);
    });

    function exec(execCount, pool, done) {
      var seenCount = 0;

      for (var i = 0; i < execCount; i++) {
        pool.send('bar', function(err, data) {
          seenCount++;

          if (seenCount === execCount) {
            done();
          } else if (seenCount > execCount) {
            assert.fail('Too many responses');
          }
        });
      }
    }
  });

  it('should kill idle workers');

  describe('worker', function() {
    it('should expose message API');
    it('should expose error API');

    it('should notify parent of errors');
    it('should notify parent of exceptions');
  });
});<commit_msg>Add idle workers test<commit_after>var assert = require('assert'),
    ChildPool = require('../lib/child-pool');

describe('child-pool', function() {
  beforeEach(function() {
    ChildPool.isBackground(false);
  });

  it('should run child', function(done) {
    var pool = new ChildPool(__dirname + '/artifacts/child-worker');

    pool.send('bar', function(err, data) {
      assert.equal(err, undefined);
      assert.deepEqual(data, {data: 'foo', input: 'bar'});
      done();
    });
  });

  it('should kill idle workers', function(done) {
    var pool = new ChildPool(__dirname + '/artifacts/child-worker', {workers: 2, keepAlive: 10});
    exec(2, pool, function() {
      setTimeout(function() {
        assert.equal(pool.workers.length, 0);
        done();
      }, 50);
    });
  });

  describe('limit', function() {
    it('should run up to pool limit', function(done) {
      var pool = new ChildPool(__dirname + '/artifacts/child-worker', {workers: 2});

      exec(10, pool, done);
      assert.equal(pool.workers.length, 2);
    });
    it('should run up to global limit', function(done) {
      var numCPUs = require('os').cpus().length,
          pool = new ChildPool(__dirname + '/artifacts/child-worker', {workers: numCPUs+2});

      exec(numCPUs+2, pool, done);
      assert.equal(pool.workers.length, numCPUs);
    });
    it('should run up to the global background limit', function(done) {
      var numCPUs = require('os').cpus().length,
          pool = new ChildPool(__dirname + '/artifacts/child-worker', {workers: numCPUs+2});

      ChildPool.isBackground(true);

      exec(numCPUs+2, pool, done);
      assert.equal(pool.workers.length, numCPUs-1);
    });
  });

  describe('worker', function() {
    it('should expose message API');
    it('should expose error API');

    it('should notify parent of errors');
    it('should notify parent of exceptions');
  });
});

function exec(execCount, pool, done) {
  var seenCount = 0;

  for (var i = 0; i < execCount; i++) {
    pool.send('bar', function(err, data) {
      seenCount++;

      if (seenCount === execCount) {
        done();
      } else if (seenCount > execCount) {
        assert.fail('Too many responses');
      }
    });
  }
}
{'fraction_non_alphanumeric': 0.16820222064729506, 'fraction_numerical': 0.006850933144342074, 'mean_word_length': 2.9203703703703705, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3555904', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1275, 'n_words': 317}