<commit_before>'use strict';

require('../test_helper');

var async = require('async'),
    DnsResolver = require('../../lib/config_reloader/dns_resolver').DnsResolver,
    ipaddr = require('ipaddr.js'),
    moment = require('moment');

describe('config reloader', function() {
  describe('dns resolver', function() {
    before(function(done) {
      async.parallel([
        function(callback) {
          redisClient.set('router_active_ip:once-valid.blah', '192.168.3.11', callback);
        },
        function(callback) {
          redisClient.set('router_active_ip:google.com', '10.0.0.1', callback);
        },
        function(callback) {
          var key = 'router_seen_ips:google.com:' + moment().subtract(3, 'hours').format(DnsResolver.RECENT_IP_BUCKET_TIME_FORMAT);
          redisClient.sadd(key, ['10.0.0.1'], callback);
        },
        function(callback) {
          redisClient.set('router_active_ip:yahoo.com', '10.0.0.1', callback);
        },
        function(callback) {
          var key = 'router_seen_ips:yahoo.com:' + moment().subtract(4, 'hours').format(DnsResolver.RECENT_IP_BUCKET_TIME_FORMAT);
          redisClient.sadd(key, ['10.0.0.1'], callback);
        },
      ], done);
    });
<commit_msg>Stub DNS lookups in the tests.<commit_after>'use strict';

require('../test_helper');

var async = require('async'),
    dns = require('native-dns'),
    DnsResolver = require('../../lib/config_reloader/dns_resolver').DnsResolver,
    ipaddr = require('ipaddr.js'),
    moment = require('moment'),
    sinon = require('sinon');

describe('config reloader', function() {
  describe('dns resolver', function() {
    // Stub the DNS lookups since these can be unpredictably slow depending on
    // your DNS provider and network connection.
    before(function() {
      // Stub the dns.Request send call.
      this.dnsRequestSend = sinon.stub(dns.Request.prototype, 'send', function() {
        var message = {
          answer: [],
        };

        if(this.question.type !== 1) {
          console.error('Unexpected question type for DNS request stub:', this.question.type);
          process.exit(1);
        }

        switch(this.question.name) {
          case 'google.com':
            message.answer = [
              { name: 'google.com',
                type: 1,
                class: 1,
                ttl: 174,
                address: '172.16.17.32' },
              { name: 'google.com',
                type: 1,
                class: 1,
                ttl: 174,
                address: '192.168.3.11' },
              { name: 'google.com',
                type: 1,
                class: 1,
                ttl: 174,
                address: '192.168.127.12' },
            ];

            break;
          case 'example.com':
            message.answer = [
              { name: 'example.com',
                type: 1,
                class: 1,
                ttl: 40138,
                address: '172.16.17.32' },
            ];

            break;
          case 'yahoo.com':
            message.answer = [
              { name: 'yahoo.com',
                type: 1,
                class: 1,
                ttl: 765,
                address: '172.16.17.32' },
              { name: 'yahoo.com',
                type: 1,
                class: 1,
                ttl: 765,
                address: '172.16.58.3' },
              { name: 'yahoo.com',
                type: 1,
                class: 1,
                ttl: 765,
                address: '172.16.17.32' }
            ];

            break;
          case 'localhost':
          case 'once-valid.blah':
          case 'foo.blah':
            message.answer = [];
            break;
          default:
            console.error('Unexpected host for DNS request stub:', this.question.name);
            process.exit(1);
        }

        process.nextTick(function() {
          this.handle(null, message, false);
        }.bind(this));
      });

      // Stub the fallback dns.lookup call.
      this.dnsLookupStub = sinon.stub(dns, 'lookup', function(host, callback) {
        switch(host) {
          case 'localhost':
            callback(null, '127.0.0.1');
            break;
          case 'once-valid.blah':
          case 'foo.blah':
            callback('Not found');
            break;
          default:
            console.error('Unexpected host for DNS lookup stub:', host);
            process.exit(1);
        }
      });
    });

    after(function() {
      this.dnsRequestSend.restore();
      this.dnsLookupStub.restore();
    });

    before(function(done) {
      async.parallel([
        function(callback) {
          redisClient.set('router_active_ip:once-valid.blah', '1.2.3.4', callback);
        },
        function(callback) {
          redisClient.set('router_active_ip:google.com', '10.0.0.1', callback);
        },
        function(callback) {
          var key = 'router_seen_ips:google.com:' + moment().subtract(3, 'hours').format(DnsResolver.RECENT_IP_BUCKET_TIME_FORMAT);
          redisClient.sadd(key, ['10.0.0.1'], callback);
        },
        function(callback) {
          redisClient.set('router_active_ip:yahoo.com', '10.0.0.1', callback);
        },
        function(callback) {
          var key = 'router_seen_ips:yahoo.com:' + moment().subtract(4, 'hours').format(DnsResolver.RECENT_IP_BUCKET_TIME_FORMAT);
          redisClient.sadd(key, ['10.0.0.1'], callback);
        },
      ], done);
    });

{'fraction_non_alphanumeric': 0.14803293687099725, 'fraction_numerical': 0.030741079597438242, 'mean_word_length': 1.6820412168792935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 18, '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': '4914611', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1622, 'n_words': 293}