<commit_before>const chai = require('chai');
const path = require('path');
const sinon = require('sinon');
chai.should();

const Task = require('../lib/task');
const Git = require('../lib/git');
const sleep = require('util').promisify(setTimeout);

describe('Task', function() {
  this.timeout(60 * 1000);
  const workspace = path.join(__dirname, 'testdata');
  const repo1Path = path.join(workspace, 'repo1');

  it('start then stop', async () => {
    const git = sinon.createStubInstance(Git, {
      getUrl: '<EMAIL>:sarosia/repo1.git',
      getRepoPath: repo1Path,
      getRevision: null,
      update: Promise.resolve(false),
    });
    const task = new Task({name: 'repo1'}, git, 1000);
    let json = await task.toJson();
    json.should.deep.equal({
      'git': '<EMAIL>:sarosia/repo1.git',
      'logs': {
        'stdout.log': 'log1\nlog2\nlog3\n',
        'stderr.log': 'log1\nlog2\nlog3\n',
      },
      'name': 'repo1',
      'path': repo1Path,
      'pid': -1,
      'revision': null,
      'startTime': null,
      'status': 'IDLE',
      'code': null,
    });

    try {
      await task.start();
      json = await task.toJson();
      json.startTime.should.not.equal(null);
      json.pid.should.not.equal(-1);
    } finally {
      await task.stop();
      json = await task.toJson();
      json.startTime.should.not.equal(null);
      delete json['startTime'];
      json.should.deep.equal({
        'git': '<EMAIL>:sarosia/repo1.git',
        'logs': {
          'stdout.log': 'log1\nlog2\nlog3\n',
          'stderr.log': 'log1\nlog2\nlog3\n',
        },
        'name': 'repo1',
        'path': repo1Path,
        'pid': -1,
        'revision': null,
        'status': 'STOPPED',
        'code': 'SIGTERM',
      });
    }
  });

  it('restart', async () => {
    const git = sinon.createStubInstance(Git, {
      getUrl: '<EMAIL>:sarosia/repo1.git',
      getRepoPath: repo1Path,
      getRevision: null,
      update: Promise.resolve(false),
    });
    const task = new Task({name: 'repo1'}, git, 500);
    try {
      await task.start();
      let json = await task.toJson();
      json.startTime.should.not.equal(null);
      json.pid.should.not.equal(-1);
      const oldPid = json.pid;

      // Call restart twice to simulate race condition.
      task.restart();
      await task.restart();

      json = await task.toJson();
      json.startTime.should.not.equal(null);
      json.pid.should.not.equal(-1);
      json.pid.should.not.equal(oldPid);
    } finally {
      await task.stop();
    }
  });

  it('pollUpdates', async () => {
    const git = sinon.createStubInstance(Git, {
      getUrl: '<EMAIL>:sarosia/repo1.git',
      getRepoPath: repo1Path,
      getRevision: null,
      update: Promise.resolve(false),
    });
    git.update.onCall(0).resolves(false);
    git.update.onCall(1).resolves(false);
    git.update.onCall(2).resolves(true);

    const task = new Task({name: 'repo1'}, git, 500);
    try {
      await task.start();
      json = await task.toJson();
      git.update.onCall(0).resolves(true);
      json.pid.should.not.equal(-1);
      const oldPid = json.pid;

      await task.pollUpdates();
      await sleep(10 * 1000);
      json = await task.toJson();
      json.pid.should.not.equal(-1);
      json.pid.should.not.equal(oldPid);
    } finally {
      await task.stop();
    }
  });
});<commit_msg>Reduce polling interval for testing.<commit_after>const chai = require('chai');
const path = require('path');
const sinon = require('sinon');
chai.should();

const Task = require('../lib/task');
const Git = require('../lib/git');
const sleep = require('util').promisify(setTimeout);

describe('Task', function() {
  this.timeout(60 * 1000);
  const workspace = path.join(__dirname, 'testdata');
  const repo1Path = path.join(workspace, 'repo1');

  it('start then stop', async () => {
    const git = sinon.createStubInstance(Git, {
      getUrl: '<EMAIL>:sarosia/repo1.git',
      getRepoPath: repo1Path,
      getRevision: null,
      update: Promise.resolve(false),
    });
    const task = new Task({name: 'repo1'}, git, 1000);
    let json = await task.toJson();
    json.should.deep.equal({
      'git': '<EMAIL>:sarosia/repo1.git',
      'logs': {
        'stdout.log': 'log1\nlog2\nlog3\n',
        'stderr.log': 'log1\nlog2\nlog3\n',
      },
      'name': 'repo1',
      'path': repo1Path,
      'pid': -1,
      'revision': null,
      'startTime': null,
      'status': 'IDLE',
      'code': null,
    });

    try {
      await task.start();
      json = await task.toJson();
      json.startTime.should.not.equal(null);
      json.pid.should.not.equal(-1);
    } finally {
      await task.stop();
      json = await task.toJson();
      json.startTime.should.not.equal(null);
      delete json['startTime'];
      json.should.deep.equal({
        'git': '<EMAIL>:sarosia/repo1.git',
        'logs': {
          'stdout.log': 'log1\nlog2\nlog3\n',
          'stderr.log': 'log1\nlog2\nlog3\n',
        },
        'name': 'repo1',
        'path': repo1Path,
        'pid': -1,
        'revision': null,
        'status': 'STOPPED',
        'code': 'SIGTERM',
      });
    }
  });

  it('restart', async () => {
    const git = sinon.createStubInstance(Git, {
      getUrl: '<EMAIL>:sarosia/repo1.git',
      getRepoPath: repo1Path,
      getRevision: null,
      update: Promise.resolve(false),
    });
    const task = new Task({name: 'repo1'}, git, 1000);
    try {
      await task.start();
      let json = await task.toJson();
      json.startTime.should.not.equal(null);
      json.pid.should.not.equal(-1);
      const oldPid = json.pid;

      // Call restart twice to simulate race condition.
      task.restart();
      await task.restart();

      json = await task.toJson();
      json.startTime.should.not.equal(null);
      json.pid.should.not.equal(-1);
      json.pid.should.not.equal(oldPid);
    } finally {
      await task.stop();
    }
  });

  it('pollUpdates', async () => {
    const git = sinon.createStubInstance(Git, {
      getUrl: '<EMAIL>:sarosia/repo1.git',
      getRepoPath: repo1Path,
      getRevision: null,
      update: Promise.resolve(false),
    });
    git.update.onCall(0).resolves(false);
    git.update.onCall(1).resolves(false);
    git.update.onCall(2).resolves(true);

    const task = new Task({name: 'repo1'}, git, 1000);
    try {
      await task.start();
      json = await task.toJson();
      git.update.onCall(0).resolves(true);
      json.pid.should.not.equal(-1);
      const oldPid = json.pid;

      await task.pollUpdates();
      await sleep(10 * 1000);
      json = await task.toJson();
      json.pid.should.not.equal(-1);
      json.pid.should.not.equal(oldPid);
    } finally {
      await task.stop();
    }
  });
});
{'fraction_non_alphanumeric': 0.17772196779435662, 'fraction_numerical': 0.01861427094105481, 'mean_word_length': 2.4435401831129195, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '5952055', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2335, 'n_words': 432}