<commit_before>const Promise = require('bluebird');
const assert = require('assert');
const _ = require('lodash');
const {IOSemaphore} = require('../src/locks.js');

class Deferred {
  constructor() {
    this.done = false;
    this.is_error = false;
    this.result = null;
    this.promise = new Promise((resolve, reject) => {
      this.resolve = (arg) => {
        this.done = true;
        this.result = arg;
        this.is_error = false;
        resolve(arg);
      }
      this.reject = (err) => {
        this.done = true;
        this.result = err;
        this.is_error = true;
        reject(err);
      }
    })
  }
}

describe('IOSemaphore', function() {
  describe('basically', function() {
    let sem;
    it('saves should be sequential', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('save');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('save');
      assert.ok(r2.isPending(), "Should be waiting");

      sem.release('save');
      assert.ok(r2.isResolved(), "Should now acquire second one");
    })

    it('save + io should be sequential', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('save');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('io');
      assert.ok(r2.isPending(), "Should be waiting");

      sem.release('save');
      assert.ok(r2.isResolved(), "Should now acquire second one");
    })

    it('io + save should be sequential', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('io');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('save');
      assert.ok(r2.isPending(), "Should be waiting");

      sem.release('io');
      assert.ok(r2.isResolved(), "Should now acquire second one");
    })

    it('io + io should be parallel', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('io');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('io');
      assert.ok(r2.isResolved(), "Should have acquired immediately");
    })

    it('io + io + save + io should be parallel then sequential', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('io');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('io');
      assert.ok(r2.isResolved(), "Should have acquired immediately");

      let r3 = sem.acquire('save');
      assert.ok(r3.isPending(), "Should wait");

      let r4 = sem.acquire('io');
      assert.ok(r4.isPending(), "Should wait");

      sem.release('io')
      assert.ok(r3.isResolved(), 'waiting save should be acquired');
      assert.ok(r4.isPending(), "next io should still wait")
    })

    // it('save + io should be sequential', () => {
    //   sem = new IOSemaphore({'save': 'single', 'io': 'multiple'});
    //   let called = [];
    //   let p1 = new Deferred();
    //   let r1 = sem.run('save', () => {
    //     called.push('run1');
    //     return p1.promise;
    //   });
    //   assert.ok(_.includes(called, 'run1'), "should immediately run first");

    //   sem.run('io', () => {
    //     called.push('run2');
    //   })
    //   assert.ok(!_.includes(called, 'run2'), "Should not have run second save yet");
    //   p1.resolve('run1 done')
    //   return r1.then(() => {
    //     assert.ok(_.includes(called, 'run2'), "Should run2 after run1 is done");  
    //   })
    // })<commit_msg>Fix test<commit_after>const Promise = require('bluebird');
const assert = require('assert');
const _ = require('lodash');
const {IOSemaphore} = require('../src/locks.js');

class Deferred {
  constructor() {
    this.done = false;
    this.is_error = false;
    this.result = null;
    this.promise = new Promise((resolve, reject) => {
      this.resolve = (arg) => {
        this.done = true;
        this.result = arg;
        this.is_error = false;
        resolve(arg);
      }
      this.reject = (err) => {
        this.done = true;
        this.result = err;
        this.is_error = true;
        reject(err);
      }
    })
  }
}

describe('IOSemaphore', function() {
  describe('basically', function() {
    let sem;
    it('saves should be sequential', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('save');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('save');
      assert.ok(r2.isPending(), "Should be waiting");

      sem.release('save');
      assert.ok(r2.isResolved(), "Should now acquire second one");
    })

    it('save + io should be sequential', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('save');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('io');
      assert.ok(r2.isPending(), "Should be waiting");

      sem.release('save');
      assert.ok(r2.isResolved(), "Should now acquire second one");
    })

    it('io + save should be sequential', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('io');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('save');
      assert.ok(r2.isPending(), "Should be waiting");

      sem.release('io');
      assert.ok(r2.isResolved(), "Should now acquire second one");
    })

    it('io + io should be parallel', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('io');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('io');
      assert.ok(r2.isResolved(), "Should have acquired immediately");
    })

    it('io + io + save + io should be parallel then sequential', () => {
      sem = new IOSemaphore({save: 'single', io: 'multiple'});
      let called = [];
      let r1 = sem.acquire('io');
      assert.ok(r1.isResolved(), "Should have acquired immediately");

      let r2 = sem.acquire('io');
      assert.ok(r2.isResolved(), "Should have acquired immediately");

      let r3 = sem.acquire('save');
      assert.ok(r3.isPending(), "Should wait");

      let r4 = sem.acquire('io');
      assert.ok(r4.isPending(), "Should wait");

      sem.release('io')
      sem.release('io')
      assert.ok(r3.isResolved(), 'waiting save should be acquired');
      assert.ok(r4.isPending(), "next io should still wait")
    })

    // it('save + io should be sequential', () => {
    //   sem = new IOSemaphore({'save': 'single', 'io': 'multiple'});
    //   let called = [];
    //   let p1 = new Deferred();
    //   let r1 = sem.run('save', () => {
    //     called.push('run1');
    //     return p1.promise;
    //   });
    //   assert.ok(_.includes(called, 'run1'), "should immediately run first");

    //   sem.run('io', () => {
    //     called.push('run2');
    //   })
    //   assert.ok(!_.includes(called, 'run2'), "Should not have run second save yet");
    //   p1.resolve('run1 done')
    //   return r1.then(() => {
    //     assert.ok(_.includes(called, 'run2'), "Should run2 after run1 is done");  
    //   })
    // })
{'fraction_non_alphanumeric': 0.1860434496379197, 'fraction_numerical': 0.011059907834101382, 'mean_word_length': 2.503690036900369, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '7417630', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2445, 'n_words': 584}