<commit_before>suite("Exchanges (Publish on Pulse)", function() {
  var assert  = require('assert');
  var base    = require('../');
  var path    = require('path');
  var fs      = require('fs');
  var debug   = require('debug')('base:test:publish-pulse');
  var Promise = require('promise');
  var slugid  = require('slugid');
  var amqplib  = require('amqplib');

  // Load necessary configuration
  var cfg = base.config({
    envs: [
      'influxdb_connectionString',
    ],
    filename:               'taskcluster-base-test'
  });

  if (!cfg.get('influxdb:connectionString') &&
      !cfg.get('pulse:credentials:password')) {
    console.log("Skipping 'pulse publisher', missing config file: " +
                "taskcluster-base-test.conf.json");
    return;
  }

  // ConnectionString for use with amqplib only
  var connectionString = [
    'amqps://',         // Ensure that we're using SSL
    cfg.get('pulse:username'),
    ':',
    cfg.get('pulse:password'),
    '@',
    cfg.get('pulse:hostname') || 'pulse.mozilla.org',
    ':',
    5671                // Port for SSL
  ].join('');

  var influx = null;
  var exchanges = null;
  setup(function() {
    exchanges = new base.Exchanges({
      title:              "Title for my Events",
      description:        "Test exchanges used for testing things only"
    });
    // Check that we can declare an exchange
    exchanges.declare({
      exchange:           'test-exchange',
      name:               'testExchange',
      title:              "Test Exchange",
      description:        "Place we post message for **testing**.",
      routingKey: [
        {
          name:           'testId',
          summary:        "Identifier that we use for testing",
          multipleWords:  false,
          required:       true,
          maxSize:        22
        }, {
          name:           'taskRoutingKey',
          summary:        "Test specific routing-key: `test.key`",
          multipleWords:  true,
          required:       true,
          maxSize:        128
        }, {
          name:           'state',
          summary:        "State of something",
          multipleWords:  false,
          required:       false,
          maxSize:        16
        }, {
          name:           'index',
          summary:        "index of something",
          multipleWords:  false,
          required:       false,
          maxSize:        3
        }, {
          name:           'myConstant',
          summary:        "Some constant to test",
          constant:       "-constant-"
        }
      ],
      schema:             'http://localhost:1203/exchange-test-schema.json#',
      messageBuilder:     function(msg) { return msg; },
      routingKeyBuilder:  function(msg, rk) { return rk; },
      CCBuilder:          function() {return ["something.cced"];}
    });
    // Create validator to validate schema
    var validator = new base.validator.Validator();
    // Load exchange-test-schema.json schema from disk
    var schemaPath = path.join(__dirname, 'schemas', 'exchange-test-schema.json');
    var schema = fs.readFileSync(schemaPath, {encoding: 'utf-8'});
    validator.register(JSON.parse(schema));

    // Create influx db connection for report statistics
    influx = new base.stats.Influx({
      connectionString:   cfg.get('influxdb:connectionString')
    });

    // Set options on exchanges
    exchanges.configure({
      validator:              validator,
      credentials:            cfg.get('pulse')
    });
  });

  // Test that we can connect to AMQP server
  test("connect", function() {
    return exchanges.connect().then(function(publisher) {
      assert(publisher instanceof base.Exchanges.Publisher,
             "Should get an instance of base.Exchanges.Publisher");
    });
  });

  // Test that we can publish messages
  test("publish message", function() {
    return exchanges.connect().then(function(publisher) {
      return publisher.testExchange({someString: "My message"}, {
        testId:           "myid",
        taskRoutingKey:   "some.string.with.dots",
        state:            undefined // Optional
      });
    });
  });

  /*
  // Test that we can publish messages fast<commit_msg>More perf-tests commented out<commit_after>suite("Exchanges (Publish on Pulse)", function() {
  var assert  = require('assert');
  var base    = require('../');
  var path    = require('path');
  var fs      = require('fs');
  var debug   = require('debug')('base:test:publish-pulse');
  var Promise = require('promise');
  var slugid  = require('slugid');
  var amqplib  = require('amqplib');

  // Load necessary configuration
  var cfg = base.config({
    envs: [
      'influxdb_connectionString',
    ],
    filename:               'taskcluster-base-test'
  });

  if (!cfg.get('influxdb:connectionString') &&
      !cfg.get('pulse:credentials:password')) {
    console.log("Skipping 'pulse publisher', missing config file: " +
                "taskcluster-base-test.conf.json");
    return;
  }

  // ConnectionString for use with amqplib only
  var connectionString = [
    'amqps://',         // Ensure that we're using SSL
    cfg.get('pulse:username'),
    ':',
    cfg.get('pulse:password'),
    '@',
    cfg.get('pulse:hostname') || 'pulse.mozilla.org',
    ':',
    5671                // Port for SSL
  ].join('');

  var influx = null;
  var exchanges = null;
  setup(function() {
    exchanges = new base.Exchanges({
      title:              "Title for my Events",
      description:        "Test exchanges used for testing things only"
    });
    // Check that we can declare an exchange
    exchanges.declare({
      exchange:           'test-exchange',
      name:               'testExchange',
      title:              "Test Exchange",
      description:        "Place we post message for **testing**.",
      routingKey: [
        {
          name:           'testId',
          summary:        "Identifier that we use for testing",
          multipleWords:  false,
          required:       true,
          maxSize:        22
        }, {
          name:           'taskRoutingKey',
          summary:        "Test specific routing-key: `test.key`",
          multipleWords:  true,
          required:       true,
          maxSize:        128
        }, {
          name:           'state',
          summary:        "State of something",
          multipleWords:  false,
          required:       false,
          maxSize:        16
        }, {
          name:           'index',
          summary:        "index of something",
          multipleWords:  false,
          required:       false,
          maxSize:        3
        }, {
          name:           'myConstant',
          summary:        "Some constant to test",
          constant:       "-constant-"
        }
      ],
      schema:             'http://localhost:1203/exchange-test-schema.json#',
      messageBuilder:     function(msg) { return msg; },
      routingKeyBuilder:  function(msg, rk) { return rk; },
      CCBuilder:          function() {return ["something.cced"];}
    });
    // Create validator to validate schema
    var validator = new base.validator.Validator();
    // Load exchange-test-schema.json schema from disk
    var schemaPath = path.join(__dirname, 'schemas', 'exchange-test-schema.json');
    var schema = fs.readFileSync(schemaPath, {encoding: 'utf-8'});
    validator.register(JSON.parse(schema));

    // Create influx db connection for report statistics
    influx = new base.stats.Influx({
      connectionString:   cfg.get('influxdb:connectionString')
    });

    // Set options on exchanges
    exchanges.configure({
      validator:              validator,
      credentials:            cfg.get('pulse')
    });
  });

  // Test that we can connect to AMQP server
  test("connect", function() {
    return exchanges.connect().then(function(publisher) {
      assert(publisher instanceof base.Exchanges.Publisher,
             "Should get an instance of base.Exchanges.Publisher");
    });
  });

  // Test that we can publish messages
  test("publish message", function() {
    return exchanges.connect().then(function(publisher) {
      return publisher.testExchange({someString: "My message"}, {
        testId:           "myid",
        taskRoutingKey:   "some.string.with.dots",
        state:            undefined // Optional
      });
    });
  });


  /*
  // Test that we can publish messages fast
  test("publish message 400", function() {
    var promises = [];
    return exchanges.connect().then(function(publisher) {
      for (var i = 0; i < 400; i++) {
        promises.push(new Promise(function(accept) {
          setTimeout(accept, Math.floor(i / 4));
        }).then(function() {
          return publisher.testExchange({someString: "My message" + i}, {
            testId:           "myid",
            taskRoutingKey:   "some.string.with.dots",
            state:            undefined // Optional
          }).then(function() {
            publisher.testExchange({someString: "My message" + i}, {
              testId:           "myid",
              taskRoutingKey:   "some.string.with.dots",
              state:            undefined // Optional
            });
          });
        }));
      }
      return Promise.all(promises);
    });
  }); return; // */

  /*
  // Test that we can publish messages fast
{'fraction_non_alphanumeric': 0.1289807221216317, 'fraction_numerical': 0.004473319842368729, 'mean_word_length': 1.8724380544509025, 'pattern_counts': {'":': 0, '<': 4, '<?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': '4982848', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2529, 'n_words': 639}