Skip to content

Commit

Permalink
More work on refactoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Mar 5, 2013
1 parent 97cd6b4 commit 41075c9
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 86 deletions.
72 changes: 72 additions & 0 deletions new_tests/repl_set/reconnect_tests.js
@@ -0,0 +1,72 @@
exports['Should retrieve correct count after primary killed'] = function(configuration, test) {
var db = configuration.db;

// Drop collection on replicaset
db.dropCollection('testsets', function(err, r) {

db.createCollection('testsets', function(err, collection) {
test.equal(null, err);
test.ok(collection != null);

// Insert a dummy document
collection.insert({a:20}, {safe: {w:2, wtimeout: 10000}}, function(err, r) {
test.equal(null, err);

// Execute a count
collection.count(function(err, c) {
test.equal(null, err);
test.equal(1, c);

// Close starting connection
db.close();

// Ensure replication happened in time
setTimeout(function() {
// Kill the primary
configuration.killPrimary(function(node) {
db.collection('testsets', function(err, collection) {
test.equal(null, err);

collection.insert({a:30}, {w:1}, function(err, r) {
test.equal(null, err);

collection.insert({a:40}, {w:1}, function(err, r) {
test.equal(null, err);

// Execute count
collection.count(function(err, c) {
test.equal(null, err);
test.equal(3, c);
test.done();
});
});
});
});
});
}, 2000);
})
})
});
});
}

exports['Should correctly throw timeout for replication to servers on inserts'] = function(configuration, test) {
var db = configuration.db;

// Drop collection on replicaset
db.dropCollection('shouldCorrectlyThrowTimeoutForReplicationToServersOnInserts', function(err, r) {

// Recreate collection on replicaset
db.createCollection('shouldCorrectlyThrowTimeoutForReplicationToServersOnInserts', function(err, collection) {
test.equal(null, err);

// Insert a dummy document
collection.insert({a:20}, {safe: {w:7, wtimeout: 10000}}, function(err, r) {
test.equal('timeout', err.err);
test.equal(true, err.wtimeout);
test.done();
});
});
});
}

106 changes: 84 additions & 22 deletions new_tests/runner.js
Expand Up @@ -3,20 +3,25 @@ var Configuration = require('integra').Configuration
, mongodb = require('../')
, Db = mongodb.Db
, Server = mongodb.Server
, ServerManager = require('../test/tools/server_manager').ServerManager;
, ReplSet = mongodb.ReplSet
, ServerManager = require('../test/tools/server_manager').ServerManager
, ReplicaSetManager = require('../test/tools/replica_set_manager').ReplicaSetManager;

// Server manager
var serverManager = new ServerManager();
var replicasetManager = new ReplicaSetManager({retries:120, secondary_count:2, passive_count:0, arbiter_count:0});

// Create Simple Server configuration
//
// Configurations
//
var configurations = Configuration

// Single server configuration
.add('single_server', function() {
var db = new Db('integration_tests', new Server("127.0.0.1", 27017,
{auto_reconnect: false, poolSize: 4}), {w:0, native_parser: false});

//
// Basic functions
//
// Test suite start
this.start = function(callback) {
serverManager.start(true, {purgedirectories:true}, function(err) {
if(err) throw err;
Expand All @@ -28,36 +33,80 @@ var configurations = Configuration
});
}

this.setup = function(callback) {
callback();
}

this.teardown = function(callback) {
callback();
// Test suite stop
this.stop = function(callback) {
serverManager.stop(9, function(err) {
callback();
});
};

// Pr test functions
this.setup = function(callback) { callback(); }
this.teardown = function(callback) { callback(); };

// Used in tests
this.db_name = "integration_tests";
this.db = db;
})

// Simple Replicaset Configuration
.add('replica_set', function() {
var self = this;

// Test suite start
this.start = function(callback) {
replicasetManager.startSet(true, function(err, result) {
if(err) throw err;

// Set up the replicaset
var replSet = new ReplSet( [
new Server( replicasetManager.host, replicasetManager.ports[1]),
new Server( replicasetManager.host, replicasetManager.ports[0]),
new Server( replicasetManager.host, replicasetManager.ports[2])
],
{rs_name:replicasetManager.name}
);

self.db = new Db('integration_tests', replSet, {w:0, native_parser: false});
self.db.open(function(err, result) {
if(err) throw err;
callback();
})
});
}

// Test suite stop
this.stop = function(callback) {
serverManager.stop(9, function(err) {
replicasetManager.killAll(function(err) {
callback();
});
};

//
// Custom functions tests can use to manage a test suite
//
this.killServer = function(callback) {
callback();
// Allow us to kill the primary
this.killPrimary = function(callback) {
replicasetManager.killPrimary(function() {
callback();
})
}

this.restartServer = function(callback) {
callback();
// Pr test functions
this.setup = function(callback) {
callback();
}

this.teardown = function(callback) {
replicasetManager.restartKilledNodes(function() {
callback();
});
};

// Used in tests
this.db_name = "integration_tests";
this.db = db;
})

//
// Runners
//
// Configure a Run of tests
var functional_tests_runner = Runner
// Add configurations to the test runner
Expand All @@ -69,8 +118,21 @@ var functional_tests_runner = Runner
['/new_tests/functional/insert_tests.js']
);

// Run the tests against configuration 'single_server'
functional_tests_runner.run("single_server");
// Configure a Run of tests
var repl_set_tests_runner = Runner
// Add configurations to the test runner
.configurations(configurations)
.exeuteSerially(true)
// First parameter is test suite name
// Second parameter is the configuration used
// Third parameter is the list of files to execute
.add("replica_set",
['/new_tests/repl_set/reconnect_tests.js']
);

// // Run the tests against configuration 'single_server'
// functional_tests_runner.run("single_server");
repl_set_tests_runner.run("replica_set");



Expand Down
File renamed without changes.
128 changes: 64 additions & 64 deletions test/replicaset/insert_test.js
Expand Up @@ -60,34 +60,34 @@ exports.tearDown = function(callback) {
}
}

exports.shouldCorrectlyWaitForReplicationToServersOnInserts = function(test) {
// debug("=========================================== shouldWorkCorrectlyWithInserts")
// Replica configuration
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[2], { auto_reconnect: true } )
],
{rs_name:RS.name}
);

// Insert some data
var db = new Db('integration_test_', replSet, {w:0, numberOfRetries:20, retryMiliSeconds:5000});
db.open(function(err, p_db) {
// Drop collection on replicaset
p_db.dropCollection('shouldCorrectlyWaitForReplicationToServersOnInserts', function(err, r) {
// Recreate collection on replicaset
p_db.createCollection('shouldCorrectlyWaitForReplicationToServersOnInserts', function(err, collection) {
// Insert a dummy document
collection.insert({a:20}, {safe: {w:2, wtimeout: 10000}}, function(err, r) {
test.equal(null, err);
test.done();
p_db.close();
});
});
});
});
}
// exports.shouldCorrectlyWaitForReplicationToServersOnInserts = function(test) {
// // debug("=========================================== shouldWorkCorrectlyWithInserts")
// // Replica configuration
// var replSet = new ReplSetServers( [
// new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
// new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
// new Server( RS.host, RS.ports[2], { auto_reconnect: true } )
// ],
// {rs_name:RS.name}
// );

// // Insert some data
// var db = new Db('integration_test_', replSet, {w:0, numberOfRetries:20, retryMiliSeconds:5000});
// db.open(function(err, p_db) {
// // Drop collection on replicaset
// p_db.dropCollection('shouldCorrectlyWaitForReplicationToServersOnInserts', function(err, r) {
// // Recreate collection on replicaset
// p_db.createCollection('shouldCorrectlyWaitForReplicationToServersOnInserts', function(err, collection) {
// // Insert a dummy document
// collection.insert({a:20}, {safe: {w:2, wtimeout: 10000}}, function(err, r) {
// test.equal(null, err);
// test.done();
// p_db.close();
// });
// });
// });
// });
// }

exports.shouldCorrectlyThrowTimeoutForReplicationToServersOnInserts = function(test) {
// debug("=========================================== shouldWorkCorrectlyWithInserts")
Expand Down Expand Up @@ -124,42 +124,42 @@ exports.shouldCorrectlyThrowTimeoutForReplicationToServersOnInserts = function(t
});
}

exports.shouldCorrectlyExecuteSafeFindAndModify = function(test) {
// Replica configuration
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[2], { auto_reconnect: true } )
],
{rs_name:RS.name}
);

// Insert some data
var db = new Db('integration_test_', replSet, {w:0, numberOfRetries:20, retryMiliSeconds:5000});
db.open(function(err, p_db) {
// Check if we got an error
if(err != null) debug("shouldWorkCorrectlyWithInserts :: " + inspect(err));

// Drop collection on replicaset
p_db.dropCollection('shouldCorrectlyExecuteSafeFindAndModify', function(err, r) {
if(err != null) debug("shouldWorkCorrectlyWithInserts :: " + inspect(err));
// Recreate collection on replicaset
p_db.createCollection('shouldCorrectlyExecuteSafeFindAndModify', function(err, collection) {
if(err != null) debug("shouldWorkCorrectlyWithInserts :: " + inspect(err));
// Insert a dummy document
collection.insert({a:20}, {safe: {w:2, wtimeout: 10000}}, function(err, r) {
// Execute a safe insert with replication to two servers
collection.findAndModify({'a':20}, [['a', 1]], {'$set':{'b':3}}, {new:true, safe: {w:2, wtimeout: 10000}}, function(err, result) {
test.equal(20, result.a);
test.equal(3, result.b);
test.done();
p_db.close();
})
});
});
});
});
}
// exports.shouldCorrectlyExecuteSafeFindAndModify = function(test) {
// // Replica configuration
// var replSet = new ReplSetServers( [
// new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
// new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
// new Server( RS.host, RS.ports[2], { auto_reconnect: true } )
// ],
// {rs_name:RS.name}
// );

// // Insert some data
// var db = new Db('integration_test_', replSet, {w:0, numberOfRetries:20, retryMiliSeconds:5000});
// db.open(function(err, p_db) {
// // Check if we got an error
// if(err != null) debug("shouldWorkCorrectlyWithInserts :: " + inspect(err));

// // Drop collection on replicaset
// p_db.dropCollection('shouldCorrectlyExecuteSafeFindAndModify', function(err, r) {
// if(err != null) debug("shouldWorkCorrectlyWithInserts :: " + inspect(err));
// // Recreate collection on replicaset
// p_db.createCollection('shouldCorrectlyExecuteSafeFindAndModify', function(err, collection) {
// if(err != null) debug("shouldWorkCorrectlyWithInserts :: " + inspect(err));
// // Insert a dummy document
// collection.insert({a:20}, {safe: {w:2, wtimeout: 10000}}, function(err, r) {
// // Execute a safe insert with replication to two servers
// collection.findAndModify({'a':20}, [['a', 1]], {'$set':{'b':3}}, {new:true, safe: {w:2, wtimeout: 10000}}, function(err, result) {
// test.equal(20, result.a);
// test.equal(3, result.b);
// test.done();
// p_db.close();
// })
// });
// });
// });
// });
// }

exports.shouldCorrectlyInsertAfterPrimaryComesBackUp = function(test) {
// Replica configuration
Expand Down

0 comments on commit 41075c9

Please sign in to comment.