<commit_before>/*
 * @fileOverview Main testing helper lib.
 */
var axios = require('axios');
var Promise = require('bluebird');

var KafkaAvro = require('../..');

var schemaFix = require('../fixtures/schema.fix');

var testLib = module.exports = {};

testLib.KAFKA_SCHEMA_REGISTRY_URL = 'http://localhost:8081';
testLib.KAFKA_BROKER_URL = 'localhost:9092';

testLib.topic = schemaFix.name;
testLib.topicTwo = schemaFix.name + 'Two';

var testBoot = false;

/**
 * Require from all test scripts, prepares kafka for testing.
 *
 */
testLib.init = function() {
  beforeEach(function() {
    if (testBoot) {
      return;
    }
    testBoot = true;

    return Promise.all([
      testLib.registerSchema(testLib.topic, schemaFix),
      testLib.registerSchema(testLib.topicTwo, schemaFix),
    ]);
  });

  beforeEach(function() {
    this.kafkaAvro = new KafkaAvro({
      kafkaBroker: testLib.KAFKA_BROKER_URL,
      schemaRegistry: testLib.KAFKA_SCHEMA_REGISTRY_URL,
    });

    return this.kafkaAvro.init();
  });

};

/**
 * Register a schema on SR.
 *
 * @param {string} The topic.
 * @param {schema} Object The schema to register.
 * @return {Promise} A Promise.
 */
testLib.registerSchema = Promise.method(function(topic, schema) {
  var schemaCreateUrl = testLib.KAFKA_SCHEMA_REGISTRY_URL +
    '/subjects/' + topic + '-value/versions';

  var data = {
    schema: JSON.stringify(schema),
  };

  console.log('TEST :: Registering schema:', topic, 'on SR:', schemaCreateUrl);

  return axios({
    url: schemaCreateUrl,
    method: 'post',
    headers: {
      'Content-Type': 'application/vnd.schemaregistry.v1+json',
    },
    data: data,
  })
    .catch(function(err) {
      console.error('Axios SR creation failed:', err);
      throw err;
    });
});

/** @type {Object} simple logger */
testLib.log = {
  info: function() {
    let args = Array.prototype.splice.call(arguments, 0);
    console.log('INFO:', args.join(' '));
  },
  error: function() {
    let args = Array.prototype.splice.call(arguments, 0);
    console.log('ERROR:', args.join(' '));
  },
};

/**
 * Have a Cooldown period between tests.
 *
 * @param {number} seconds cooldown in seconds.
 * @return {Function} use is beforeEach().
 */
testLib.cooldown = function(seconds) {<commit_msg>wait for SR to come up before starting tests<commit_after>/*
 * @fileOverview Main testing helper lib.
 */
var axios = require('axios');
var Promise = require('bluebird');

var KafkaAvro = require('../..');

var schemaFix = require('../fixtures/schema.fix');

var testLib = module.exports = {};

testLib.KAFKA_SCHEMA_REGISTRY_URL = 'http://localhost:8081';
testLib.KAFKA_BROKER_URL = 'localhost:9092';

testLib.topic = schemaFix.name;
testLib.topicTwo = schemaFix.name + 'Two';

var testBoot = false;

/**
 * Require from all test scripts, prepares kafka for testing.
 *
 */
testLib.init = function() {
  beforeEach(function() {
    if (testBoot) {
      return;
    }
    testBoot = true;

    this.timeout(180000); // wait up to 3' for the SR to come up

    return Promise.all([
      testLib.registerSchema(testLib.topic, schemaFix),
      testLib.registerSchema(testLib.topicTwo, schemaFix),
    ]);
  });

  beforeEach(function() {
    this.kafkaAvro = new KafkaAvro({
      kafkaBroker: testLib.KAFKA_BROKER_URL,
      schemaRegistry: testLib.KAFKA_SCHEMA_REGISTRY_URL,
    });

    return this.kafkaAvro.init();
  });

};

/**
 * Register a schema on SR.
 *
 * @param {string} The topic.
 * @param {schema} Object The schema to register.
 * @param {number=} retries how many times has retried.
 * @return {Promise} A Promise.
 */
testLib.registerSchema = Promise.method(function(topic, schema, retries) {
  var schemaCreateUrl = testLib.KAFKA_SCHEMA_REGISTRY_URL +
    '/subjects/' + topic + '-value/versions';

  var data = {
    schema: JSON.stringify(schema),
  };

  retries = retries || 0;

  console.log('TEST :: Registering schema:', topic, 'on SR:', schemaCreateUrl);

  return axios({
    url: schemaCreateUrl,
    method: 'post',
    headers: {
      'Content-Type': 'application/vnd.schemaregistry.v1+json',
    },
    data: data,
  })
    .catch(function(err) {
      console.error('Axios SR creation failed:', retries, err.message);
      retries++;
      return testLib.registerSchema(topic, schema, retries);
    });
});

/** @type {Object} simple logger */
testLib.log = {
  info: function() {
    let args = Array.prototype.splice.call(arguments, 0);
    console.log('INFO:', args.join(' '));
  },
  error: function() {
    let args = Array.prototype.splice.call(arguments, 0);
    console.log('ERROR:', args.join(' '));
  },
};

/**
 * Have a Cooldown period between tests.
 *
 * @param {number} seconds cooldown in seconds.
 * @return {Function} use is beforeEach().
 */
testLib.cooldown = function(seconds) {
{'fraction_non_alphanumeric': 0.15525494276795004, 'fraction_numerical': 0.006243496357960458, 'mean_word_length': 3.4541241890639482, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4472038', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1600, 'n_words': 365}