"use strict";

/**
 * Correctly cause pool to grow
 */
exports['Example of simple parallel insert into db'] = {
  metadata: {
    requires: {
      topology: "single"
    }
  },

  test: function(configuration, test) {
    var Server = configuration.require.Server
      , ReadPreference = configuration.require.ReadPreference;

    // Attempt to connect
    var server = new Server({
        host: configuration.host
      , port: configuration.port
      , reconnect: true
      , reconnectInterval: 50
    })

    // Number of operations done
    var numberOfOpsDone = 0;
    var numberOfPoolConnectionExpansion = 0;

    // LINE var Server = require('mongodb-core').Server,
    // LINE   test = require('assert');
    // LINE var server = new Server({host: 'localhost', port: 27017});
    // REMOVE-LINE test.done();
    // BEGIN
    // Add event listeners
    server.on('connect', function(_server) {
      _server.s.pool.once('connection', function() {
        numberOfPoolConnectionExpansion = numberOfPoolConnectionExpansion + 1;
      });


      // Execute the insert
      _server.insert('integration_tests.inserts_example1', [{a:1}], {
        writeConcern: {w:1}, ordered:true
      }, function(err, results) {
        test.equal(null, err);
        test.equal(1, results.result.n);
        numberOfOpsDone = numberOfOpsDone + 1;

        if(numberOfOpsDone == 2) {
          test.equal(1, numberOfPoolConnectionExpansion);
          _server.destroy();
          test.done();          
        }
      });

      // Execute the insert
      _server.insert('integration_tests.inserts_example1', [{a:1}], {
        writeConcern: {w:1}, ordered:true
      }, function(err, results) {
        test.equal(null, err);
        test.equal(1, results.result.n);
        numberOfOpsDone = numberOfOpsDone + 1;

        if(numberOfOpsDone == 2) {
          test.equal(1, numberOfPoolConnectionExpansion);
          _server.destroy();
          test.done();          
        }
      });
    });

    // Start connection
    server.connect();
    // END
  }
}

@@ -1359,33 +1359,33 @@
 berOfOpsDone == 
-2
+3
 ) %7B%0A          te
@@ -1383,33 +1383,495 @@
        test.
-equal(1, 
+ok(numberOfPoolConnectionExpansion %3E 0);%0A          _server.destroy();%0A          test.done();          %0A        %7D%0A      %7D);%0A%0A      // Execute the insert%0A      _server.insert('integration_tests.inserts_example1', %5B%7Ba:1%7D%5D, %7B%0A        writeConcern: %7Bw:1%7D, ordered:true%0A      %7D, function(err, results) %7B%0A        test.equal(null, err);%0A        test.equal(1, results.result.n);%0A        numberOfOpsDone = numberOfOpsDone + 1;%0A%0A        if(numberOfOpsDone == 3) %7B%0A          test.ok(
 numberOfPool
@@ -1881,32 +1881,36 @@
 nectionExpansion
+ %3E 0
 );%0A          _se
@@ -2303,17 +2303,17 @@
 Done == 
-2
+3
 ) %7B%0A    
@@ -2323,25 +2323,19 @@
    test.
-equal(1, 
+ok(
 numberOf
@@ -2357,16 +2357,20 @@
 xpansion
+ %3E 0
 );%0A     

make parallel test a bit slower to force multiple connections to be created