<commit_before>exports['Should correctly timeout mongos socket operation and then correctly re-execute'] = {
  metadata: {
    requires: {
      generators: true,
      topology: "single"
    }
  },

  test: function(configuration, test) {
    var Mongos = configuration.require.Mongos,
      ObjectId = configuration.require.BSON.ObjectId,
      co = require('co'),
      mockupdb = require('../../../mock');

    // Contain mock server
    var server = null;
    var running = true;
    // Current index for the ismaster
    var currentStep = 0;
    // Primary stop responding
    var stopRespondingPrimary = false;

    // Extend the object
    var extend = function(template, fields) {
      for(var name in template) fields[name] = template[name];
      return fields;
    }

    // Default message fields
    var defaultFields = {
      "ismaster" : true,
      "msg" : "isdbgrid",
      "maxBsonObjectSize" : 16777216,
      "maxMessageSizeBytes" : 48000000,
      "maxWriteBatchSize" : 1000,
      "localTime" : new Date(),
      "maxWireVersion" : 3,
      "minWireVersion" : 0,
      "ok" : 1
    }

    // Primary server states
    var serverIsMaster = [extend(defaultFields, {})];
    var timeoutPromise = function(timeout) {
      return new Promise(function(resolve, reject) {
        setTimeout(function() {
          resolve();
        }, timeout);
      });
    }

    // Boot the mock
    co(function*() {
      server = yield mockupdb.createServer(52000, 'localhost');

      // Primary state machine
      co(function*() {
        while(running) {
          var request = yield server.receive();

          // Get the document
          var doc = request.document;
          if(doc.ismaster && currentStep == 0) {
            request.reply(serverIsMaster[0]);
            currentStep += 1;
          } else if(doc.insert && currentStep == 1) {
            // Stop responding to any calls (emulate dropping packets on the floor)
            if(stopRespondingPrimary) {
              currentStep += 1;
              stopRespondingPrimary = false;
              // Timeout after 1500 ms
              yield timeoutPromise(1500);
              continue;
            }
          } else if(doc.ismaster && currentStep == 2) {
            request.reply(serverIsMaster[0]);
          } else if(doc.insert && currentStep == 2) {
            request.reply({ok:1, n:doc.documents, lastOp: new Date()});
          }
        }
      });

      // Start dropping the packets
      setTimeout(function() {
        stopRespondingPrimary = true;
        currentIsMasterState = 1;
      }, 500);
    });

    // Attempt to connect
    var _server = new Mongos([
        { host: 'localhost', port: 52000 },
      ], {
      connectionTimeout: 3000,
      socketTimeout: 1000,
      haInterval: 500,
      size: 1
    });

    // Are we done
    var done = false;

    // Add event listeners
    _server.once('connect', function() {
      // Run an interval
      var intervalId = setInterval(function() {
        _server.insert('test.test', [{created:new Date()}], function(err, r) {
          if(r && !done) {
            done = true;
            clearInterval(intervalId);
            test.equal(52000, r.connection.port);
            running = false;
            server.destroy();
            test.done();
          }
        });
      }, 500);
    });

    _server.on('error', function(){});
    _server.connect();
  }
}
<commit_msg>more small fixes to proxy tests<commit_after>exports['Should correctly timeout mongos socket operation and then correctly re-execute'] = {
  metadata: {
    requires: {
      generators: true,
      topology: "single"
    }
  },

  test: function(configuration, test) {
    var Mongos = configuration.require.Mongos,
      ObjectId = configuration.require.BSON.ObjectId,
      co = require('co'),
      mockupdb = require('../../../mock');

    // Contain mock server
    var server = null;
    var running = true;
    // Current index for the ismaster
    var currentStep = 0;
    // Primary stop responding
    var stopRespondingPrimary = false;

    // Extend the object
    var extend = function(template, fields) {
      for(var name in template) fields[name] = template[name];
      return fields;
    }

    // Default message fields
    var defaultFields = {
      "ismaster" : true,
      "msg" : "isdbgrid",
      "maxBsonObjectSize" : 16777216,
      "maxMessageSizeBytes" : 48000000,
      "maxWriteBatchSize" : 1000,
      "localTime" : new Date(),
      "maxWireVersion" : 3,
      "minWireVersion" : 0,
      "ok" : 1
    }

    // Primary server states
    var serverIsMaster = [extend(defaultFields, {})];
    var timeoutPromise = function(timeout) {
      return new Promise(function(resolve, reject) {
        setTimeout(function() {
          resolve();
        }, timeout);
      });
    }

    // Boot the mock
    co(function*() {
      server = yield mockupdb.createServer(52000, 'localhost');

      // Primary state machine
      co(function*() {
        while(running) {
          var request = yield server.receive();

          // Get the document
          var doc = request.document;
          if(doc.ismaster && currentStep == 0) {
            request.reply(serverIsMaster[0]);
            currentStep += 1;
          } else if(doc.insert && currentStep == 1) {
            // Stop responding to any calls (emulate dropping packets on the floor)
            if(stopRespondingPrimary) {
              currentStep += 1;
              stopRespondingPrimary = false;
              // Timeout after 1500 ms
              yield timeoutPromise(1500);
              request.connection.destroy();
            }
          } else if(doc.ismaster && currentStep == 2) {
            request.reply(serverIsMaster[0]);
          } else if(doc.insert && currentStep == 2) {
            request.reply({ok:1, n:doc.documents, lastOp: new Date()});
          }
        }
      });

      // Start dropping the packets
      setTimeout(function() {
        stopRespondingPrimary = true;
        currentIsMasterState = 1;
      }, 500);
    });

    // Attempt to connect
    var _server = new Mongos([
        { host: 'localhost', port: 52000 },
      ], {
      connectionTimeout: 3000,
      socketTimeout: 1000,
      haInterval: 500,
      size: 1
    });

    // Are we done
    var done = false;

    // Add event listeners
    _server.once('connect', function() {
      // Run an interval
      var intervalId = setInterval(function() {
        _server.insert('test.test', [{created:new Date()}], function(err, r) {
          if(r && !done) {
            done = true;
            clearInterval(intervalId);
            test.equal(52000, r.connection.port);
            running = false;
            server.destroy();
            test.done();
          }
        });
      }, 500);
    });

    _server.on('error', function(){});
    _server.connect();
  }
}

{'fraction_non_alphanumeric': 0.12438530517789992, 'fraction_numerical': 0.021695111368238356, 'mean_word_length': 1.9201858108108107, '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': '1560365', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1891, 'n_words': 489}