I am trying to write an automation script to test some of my api calls here is a simple example:
async.series([
function(callback){
func.clean_database()
callback(null);
},
function(callback){
func.register_and_auth(users[0])
callback(null);
}
],
function(err, results){
console.log('done');
});
Clean database could look as simple as:
clean_database: function () {
var Projects = mongoose.model("Projects", new mongoose.Schema({}));
Projects.collection.remove(function (err, results) { console.log("Emptied: Projects"); });
return true;
},
The problem is that the register_and_auth(users[0]) runs before the cleaning database has completed.. thus telling me the user has already registered. I was under the impression async.series would run these in series i.e. one after the other.
How can I make it so that the callback(null) is tied to the completion of the function i am running in the first step?
Thanks!
::::UPDATE::::
If anyone cares I had to extend this and call more methods to mimic a user flow here is what it ultimately looks like.. if anyone wants to improve upon this please do
async.series([
func.clean_database, function(one_callback){
func.register_and_auth(users[0], function(callback) {
console.log('-- Starting Create and Upload');
func.auth_and_create_project_and_upload(users[0], data, function(callback) {
console.log('-- Starting Annotations');
func.auth_open_project_add_annotations_to_all_pages(users[0], annotation, function(callback) {
console.log('-- Done with User 0.');
one_callback();
});
});
});
},
function(two_callback) {
func.register_and_auth(users[1], function(callback) {
console.log('-- Starting Create and Upload');
func.auth_and_create_project_and_upload(users[1], data, function(callback) {
console.log('-- Starting Annotations');
func.auth_open_project_add_annotations_to_all_pages(users[1], annotation, function(callback) {
console.log('-- Done with User 1.');
two_callback();
});
});
});
},
],
function(err, results){
console.log('Load test complete for two users.');
});
You need to pass your callbacks in and call them when the tasks are done
async.series([
func.clean_database,
function(callback){
func.register_and_auth(users[0], callback);
}
],
function(err, results){
console.log('done');
});
clean_database: function (callback) {
var Projects = mongoose.model("Projects", new mongoose.Schema({}));
Projects.collection.remove(function (err, results) {
console.log("Emptied: Projects");
callback(err, results);
});
return true;
},