<commit_before>/*jshint expr: true */
/*eslint no-unused-expressions: false */
/*global describe, it, beforeEach, afterEach */
'use strict';
const internal = require('internal');
const tasks = require('@arangodb/tasks');
const expect = require('chai').expect;

const isServer = typeof arango === 'undefined';
const query = 'FOR x IN 1..5 LET y = SLEEP(1) RETURN x';
const taskInfo = {
  offset: 0,
  command: function () {
    var taskQuery = 'FOR x IN 1..5 LET y = SLEEP(1) RETURN x';
    require('internal').db._query(taskQuery);
  }
};

function filterQueries(q) {
  return (q.query === query);
}

function sendQuery(count, async) {
  count = count || 1;
  for (let i = 0; i < count; ++i) {
    if (async === false) {
      internal.db._query(query);
    } else {
      tasks.register(taskInfo);
    }
  }
  if (async === true) {
    internal.wait(1);
  }
}

function restoreDefaults(testee) {
  testee.properties({
    enabled: true,
    trackSlowQueries: true,
    maxSlowQueries: 64,
    slowQueryThreshold: 10,
    maxQueryStringLength: 8192
  });
}

describe('AQL query analyzer', function() {
  let testee;

  beforeEach(function() {
    testee = require('@arangodb/aql/queries');
    restoreDefaults(testee);
  });

  afterEach(function() {
    restoreDefaults(testee);
  });

  it('should be able to activate the tracking', function() {
    testee.properties({
      enabled: true
    });
    expect(testee.properties().enabled).to.be.ok;
  });

  it('should be able to deactivate the tracking', function() {
    testee.properties({
      enabled: false
    });
    expect(testee.properties().enabled).not.to.be.ok;
  });

  it('should be able to set tracking properties', function() {
    testee.properties({
      enabled: true,
      trackSlowQueries: false,
      maxSlowQueries: 42,
      slowQueryThreshold: 2.5,
      maxQueryStringLength: 117
    });
    expect(testee.properties().enabled).to.be.ok;
    expect(testee.properties().trackSlowQueries).not.to.be.ok;
    expect(testee.properties().maxSlowQueries).to.equal(42);
    expect(testee.properties().slowQueryThreshold).to.equal(2.5);
    expect(testee.properties().maxQueryStringLength).to.equal(117);
  });

  describe('with active tracking', function() {

    beforeEach(function() {
      if (internal.debugCanUseFailAt()) {
        internal.debugClearFailAt();
      }
      testee.properties({
        enabled: true,
        slowQueryThreshold: 20
      });
      testee.clearSlow();
    });

    afterEach(function() {
      if (internal.debugCanUseFailAt()) {
        internal.debugClearFailAt();
      }
      const list = testee.current().filter(filterQueries);
      for (let item of list) {
        try {
          testee.kill(item.id);
        } catch (e) {
          // noop
        }
      }
    });

    if (isServer && internal.debugCanUseFailAt()) {
      it('should not crash when inserting a query into the current list fails', function() {
        internal.debugSetFailAt('QueryList::insert');

        // inserting the query will fail
        sendQuery(1, true);<commit_msg>fixed test in failure mode<commit_after>/*jshint expr: true */
/*eslint no-unused-expressions: false */
/*global describe, it, beforeEach, afterEach */
'use strict';
const internal = require('internal');
const tasks = require('@arangodb/tasks');
const expect = require('chai').expect;

const isServer = typeof arango === 'undefined';
const query = 'FOR x IN 1..5 LET y = SLEEP(1) RETURN x';
const taskInfo = {
  offset: 0,
  command: function () {
    var taskQuery = 'FOR x IN 1..5 LET y = SLEEP(1) RETURN x';
    require('internal').db._query(taskQuery);
  }
};

function filterQueries(q) {
  return (q.query === query);
}

function sendQuery(count, async) {
  count = count || 1;
  for (let i = 0; i < count; ++i) {
    if (async === false) {
      internal.db._query(query);
    } else {
      tasks.register(taskInfo);
    }
  }
  if (async === true) {
    internal.wait(1);
  }
}

function restoreDefaults(testee) {
  testee.properties({
    enabled: true,
    trackSlowQueries: true,
    maxSlowQueries: 64,
    slowQueryThreshold: 10,
    maxQueryStringLength: 8192
  });
}

describe('AQL query analyzer', function() {
  let testee;

  beforeEach(function() {
    testee = require('@arangodb/aql/queries');
    restoreDefaults(testee);
  });

  afterEach(function() {
    restoreDefaults(testee);
  });

  it('should be able to activate the tracking', function() {
    testee.properties({
      enabled: true
    });
    expect(testee.properties().enabled).to.be.ok;
  });

  it('should be able to deactivate the tracking', function() {
    testee.properties({
      enabled: false
    });
    expect(testee.properties().enabled).not.to.be.ok;
  });

  it('should be able to set tracking properties', function() {
    testee.properties({
      enabled: true,
      trackSlowQueries: false,
      maxSlowQueries: 42,
      slowQueryThreshold: 2.5,
      maxQueryStringLength: 117
    });
    expect(testee.properties().enabled).to.be.ok;
    expect(testee.properties().trackSlowQueries).not.to.be.ok;
    expect(testee.properties().maxSlowQueries).to.equal(42);
    expect(testee.properties().slowQueryThreshold).to.equal(2.5);
    expect(testee.properties().maxQueryStringLength).to.equal(117);
  });

  describe('with active tracking', function() {

    beforeEach(function() {
      if (isServer && internal.debugCanUseFailAt()) {
        internal.debugClearFailAt();
      }
      testee.properties({
        enabled: true,
        slowQueryThreshold: 20
      });
      testee.clearSlow();
    });

    afterEach(function() {
      if (isServer && internal.debugCanUseFailAt()) {
        internal.debugClearFailAt();
      }
      const list = testee.current().filter(filterQueries);
      for (let item of list) {
        try {
          testee.kill(item.id);
        } catch (e) {
          // noop
        }
      }
    });

    if (isServer && internal.debugCanUseFailAt()) {
      it('should not crash when inserting a query into the current list fails', function() {
        internal.debugSetFailAt('QueryList::insert');

        // inserting the query will fail
        sendQuery(1, true);
{'fraction_non_alphanumeric': 0.14694934455413497, 'fraction_numerical': 0.011328693963424502, 'mean_word_length': 2.9590006406149905, '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': '3741779', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1906, 'n_words': 429}