Skip to content

Commit

Permalink
Added tests for pubsub contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Feb 10, 2014
1 parent 240efc7 commit 44e0d70
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ module.exports = {
User.message(user, {greeting: 'hello'}, req);
res.send(200);
});
},

subscribe: function(req, res) {
User.subscribe(req, {id:req.param('id')}, req.param('context'));
res.send(200);
}


};
14 changes: 12 additions & 2 deletions test/integration/helpers/appHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ module.exports.buildAndLift = function(appName, options, callback) {
});
};

module.exports.buildAndLiftWithTwoSockets = function(appName, options, callback) {
module.exports.liftWithTwoSockets = function(options, callback) {
if (typeof options == 'function') {
callback = options;
options = null;
}
module.exports.buildAndLift(appName, options, function(err, sails) {
module.exports.lift(options, function(err, sails) {
if (err) {return callback(err);}
var socket1 = _ioClient.connect('http://localhost:1337',{'force new connection': true});
socket1.on('connect', function() {
Expand All @@ -161,4 +161,14 @@ module.exports.buildAndLiftWithTwoSockets = function(appName, options, callback)
});
});
});
};

module.exports.buildAndLiftWithTwoSockets = function(appName, options, callback) {
if (typeof options == 'function') {
callback = options;
options = null;
}
module.exports.build(appName, function() {
module.exports.liftWithTwoSockets(options, callback);
});
};
10 changes: 2 additions & 8 deletions test/integration/helpers/socketHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ var fs = require('fs');

module.exports = {

// Write routes object to router file
writeRoutes: function(routes) {
fs.writeFileSync('config/routes.js', 'module.exports.routes = ' + JSON.stringify(routes));
},

// Write routes object to blueprint config file
writeBlueprint: function(config) {
config = {blueprints: config};
fs.writeFileSync('config/blueprints.js', 'module.exports = ' + JSON.stringify(config));
writeModelConfig: function(config) {
fs.writeFileSync('config/models.js', 'module.exports.models = {autosubscribe: [], connection: "localDiskDb"}');
},

// Starts sails server, makes request, returns response, kills sails server
Expand Down
193 changes: 193 additions & 0 deletions test/integration/pubsub.modelEvents.context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
* Test dependencies
*/
var assert = require('assert');
var socketHelper = require('./helpers/socketHelper.js');
var appHelper = require('./helpers/appHelper');
var util = require('util');

/**
* Errors
*/
var Err = {
badResponse: function(response) {
return 'Wrong server response! Response :::\n' + util.inspect(response);
}
};


describe('pubsub :: ', function() {

var sailsprocess;
var socket1;
var socket2;
var appName = 'testApp';

describe('Model events (i.e. not the firehose)', function() {


describe('when a model no default autosubscribe contexts ', function() {

before(function(done) {
this.timeout(5000);
appHelper.build(appName, function(err) {
if (err) {throw new Error(err);}
socketHelper.writeModelConfig();
appHelper.liftWithTwoSockets({verbose: false}, function(err, sails, _socket1, _socket2) {
if (err) {throw new Error(err);}
sailsprocess = sails;
socket1 = _socket1;
socket2 = _socket2;

socket2.get('/user/create?name=joe', function(){
socket2.get('/user/create?name=abby', function() {
done();
});
});
});
});
});

after(function() {

socket1.disconnect();
socket2.disconnect();

if (sailsprocess) {
sailsprocess.kill();
}
// console.log('before `chdir ../`' + ', cwd was :: ' + process.cwd());
process.chdir('../');
// console.log('after `chdir ../`' + ', cwd was :: ' + process.cwd());
appHelper.teardown();
});

afterEach(function(done) {
socket1.removeAllListeners();
socket2.removeAllListeners();
done();
});

it('updating an instance via put should not result in any socket messages being received', function(done) {

this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
})
socket1.put('/user/1', {name:'scott'});
setTimeout(done, 1000);

});

describe('after subscribing to the update context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=update', function(){done();});
});
it('updating an instance via put should result in a correct socket messages being received', function(done) {
socket2.on('user', function(message) {
assert(message.id == 1 && message.verb == 'updated' && message.data.name == 'bob', Err.badResponse(message));
done();
})
socket1.put('/user/1', {name:'bob'});
})
});

it('sending a message should not result in any socket messages being received', function(done) {

this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
})
socket1.get('/user/message');
setTimeout(done, 1000);

});

describe('after subscribing to the message context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=message', function(){done();});
});
it('sending a message should result in a correct socket messages being received', function(done) {
socket2.on('user', function(message) {
assert(message.id == 1 && message.verb == 'messaged' && message.data.greeting == 'hello', Err.badResponse(message));
done();
})
socket1.get('/user/message');
})
});

it('adding a pet to the user should not result in any socket messages being received', function(done) {

this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
})
socket1.post('/pet', {name:'rex', owner: 1});
setTimeout(done, 1000);

});

describe('after subscribing to the add:pets context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=add:pets', function(){done();});
});
it('adding a pet should result in a correct socket messages being received', function(done) {
socket2.on('user', function(message) {
assert(message.id == 1 && message.verb == 'addedTo' && message.attribute == 'pets' && message.addedId == 2, Err.badResponse(message));
done();
})
socket1.post('/pet', {name:'alice', owner: 1});
})
});

it('removing a pet from the user should not result in any socket messages being received', function(done) {

this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
})
socket1.delete('/pet', {id: 1});
setTimeout(done, 1000);

});

describe('after subscribing to the remove:pets context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=remove:pets', function(){done();});
});
it('removing a pet should result in a correct socket messages being received', function(done) {
socket2.on('user', function(message) {
assert(message.id == 1 && message.verb == 'removedFrom' && message.attribute == 'pets' && message.removedId == 2, Err.badResponse(message));
done();
})
socket1.delete('/pet', {id: 2});
})
});

it('deleting a user should not result in any socket messages being received', function(done) {

this.slow(3000);
socket2.on('user', function(message) {
assert(false, 'User event received by socket 2 when it should not have been!');
})
socket1.delete('/user', {id: 2});
setTimeout(done, 1000);

});

describe('after subscribing to the destroy context', function() {
before(function(done) {
socket2.get('/user/subscribe?id=1&context=destroy', function(){done();});
});
it('deleting a user should result in a correct socket messages being received', function(done) {
socket2.on('user', function(message) {
assert(message.id == 1 && message.verb == 'destroyed', Err.badResponse(message));
done();
})
socket1.delete('/user', {id: 1});
})
});
});

});
});

0 comments on commit 44e0d70

Please sign in to comment.