<commit_before>'use strict';

/* global before, after */

var partUtils = require('../utils'),
  Changes = require('../../../../../scripts/partitioner/sql/changes');

describe('offset', function () {

  var args = partUtils.init(this, beforeEach, afterEach, null, before, after);
  var testUtils = args.utils;

  var queueAndProcess = function (changes) {
    // Force quorum=true. We don't need to consider quorum when getting changes as only changes
    // recorded by quorum are added to LATEST and server downloads all changes regardless of quorum
    // status.
    return testUtils.queueAndProcess(args.db, changes, true);
  };

  it('should get changes by offset', function () {
    var changes = null,
      changes1 = [],
      changes2 = [],
      changes3 = [],
      up = (new Date()).toISOString();

    for (var i = 1; i <= 11; i++) {
      if (i > 10) {
        changes = changes3;
      } else if (i > 5) {
        changes = changes2;
      } else {
        changes = changes1;
      }
      changes.push({
        col: 'task',
        id: i + '',
        name: 'prority',
        val: '"low"',
        up: up
      });
    }

    return queueAndProcess(changes1).then(function () {
      return testUtils.timeout(10); // sleep to guarantee order of changes
    }).then(function () {
      return queueAndProcess(changes2);
    }).then(function () {
      return testUtils.timeout(10); // sleep to guarantee order of changes
    }).then(function () {
      return args.db.changes(null, null, 5, 0);
    }).then(function (changes) {
      changes1.push('more'); // indicates more pages
      testUtils.changesShouldEql(changes1, changes);
    }).then(function () {
      return queueAndProcess(changes3); // simulate change between reads
    }).then(function () {
      return args.db.changes(null, null, 5, 5);
    }).then(function (changes) {
      changes2.push('more'); // indicates more pages
      testUtils.changesShouldEql(changes2, changes);
    }).then(function () {
      return args.db.changes(null, null, 5, 10);
    }).then(function (changes) {
      testUtils.changesShouldEql(changes3, changes);
    });
  });

  it('should throw error if limit too large', function () {
    return testUtils.shouldThrow(function () {
      return args.db.changes(null, null, Changes._MAX_LIMIT + 1);
    }, new Error('limit (' + (Changes._MAX_LIMIT + 1) + ') cannot be greater than ' +
      Changes._MAX_LIMIT));
  });
<commit_msg>fix(offset): sleep after each queue<commit_after>'use strict';

/* global before, after */

var Promise = require('bluebird'), partUtils = require('../utils'),
  Changes = require('../../../../../scripts/partitioner/sql/changes');

describe('offset', function () {

  var args = partUtils.init(this, beforeEach, afterEach, null, before, after);
  var testUtils = args.utils;

  var queueAndProcess = function (changes) {
    // Force quorum=true. We don't need to consider quorum when getting changes as only changes
    // recorded by quorum are added to LATEST and server downloads all changes regardless of quorum
    // status.
    return testUtils.queueAndProcess(args.db, changes, true);
  };

  it('should get changes by offset', function () {
    var changes = null,
      changes1 = [],
      changes2 = [],
      changes3 = [],
      up = (new Date()).toISOString();

    for (var i = 1; i <= 11; i++) {
      if (i > 10) {
        changes = changes3;
      } else if (i > 5) {
        changes = changes2;
      } else {
        changes = changes1;
      }
      changes.push({
        col: 'task',
        id: i + '',
        name: 'prority',
        val: '"low"',
        up: up
      });
    }

    // Note: to reliably ensure that changes are stored in particular order, we need to
    // queueAndProcess() and then sleep after adding each change
    var queueAndProcessAndSleep = function (changes) {
      var chain = Promise.resolve();
      changes.forEach(function (change) {
        chain = chain.then(function () {
          return queueAndProcess([change]).then(function () {
            return testUtils.timeout(1);
          });
        });
      });
      return chain;
    };

    return queueAndProcessAndSleep(changes1).then(function () {
    }).then(function () {
      return queueAndProcessAndSleep(changes2);
    }).then(function () {
      return args.db.changes(null, null, 5, 0);
    }).then(function (changes) {
      changes1.push('more'); // indicates more pages
      testUtils.changesShouldEql(changes1, changes);
    }).then(function () {
      return queueAndProcessAndSleep(changes3); // simulate change between reads
    }).then(function () {
      return args.db.changes(null, null, 5, 5);
    }).then(function (changes) {
      changes2.push('more'); // indicates more pages
      testUtils.changesShouldEql(changes2, changes);
    }).then(function () {
      return args.db.changes(null, null, 5, 10);
    }).then(function (changes) {
      testUtils.changesShouldEql(changes3, changes);
    });
  });

  it('should throw error if limit too large', function () {
    return testUtils.shouldThrow(function () {
      return args.db.changes(null, null, Changes._MAX_LIMIT + 1);
    }, new Error('limit (' + (Changes._MAX_LIMIT + 1) + ') cannot be greater than ' +
      Changes._MAX_LIMIT));
  });

{'fraction_non_alphanumeric': 0.14688090737240075, 'fraction_numerical': 0.011909262759924386, 'mean_word_length': 2.6339285714285716, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1359107', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1507, 'n_words': 428}