Skip to content

Commit

Permalink
fixes and refine tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c4milo committed Feb 5, 2012
1 parent c401df9 commit 30764a2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 48 deletions.
2 changes: 1 addition & 1 deletion participation/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SPECS = specs/*.js
SPECS = specs/*.spec.js
REPORTER = list
JSLINT := /usr/local/bin/gjslint
FIX_STYLE := /usr/local/bin/fixjsstyle
Expand Down
46 changes: 30 additions & 16 deletions participation/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ util.inherits(Connection, EventEmitter);

var swarms = options.swarms;

if(Array.isArray(swarms)) {
if (Array.isArray(swarms)) {
for(var j in swarms) {
path += '&swarm_id=' + swarms[j];
}
Expand All @@ -51,12 +51,11 @@ util.inherits(Connection, EventEmitter);
if (!chunk.match(/\r\n$/)) {
return;
}

try {
self.emit('message', JSON.parse(buffer));
} catch(e) {
console.log(e.stack);
console.log('Malformed message, not a valid JSON ' +
'structure: ---->' + buffer + '<----');
} finally {
buffer = '';
}
Expand All @@ -65,19 +64,26 @@ util.inherits(Connection, EventEmitter);

this.req.on('socket', function(socket) {
socket.on('connect', function() {
/**
* GOTCHA!
* There is an issue here
* between the socket and the
* state of http.ClientRequest.
* ClientRequest is in a bad state when socket
* connects causing a hang up
* when users try to send data inmediately
* after 'connect' is emitted.
* We need to make our users aware
* of attempting to send data
* upon presence arrival instead.
**/
self.connected = true;
self.emit('connect');
});

socket.on('error', function(err) {
console.log('socket connection error: ');
console.log(err.stack);
//self.emit('error', err);

console.log('initiating reconnection algorithm...');
//TODO Reconnections http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html
//TODO use circular queue to reduce the amount
//packets being lost when disconnected.
socket.on('end', function(e) {
self.connected = false;
self.emit('disconnect');
});

socket.on('close', function() {
Expand All @@ -86,12 +92,22 @@ util.inherits(Connection, EventEmitter);
});
});

this.req.on('error', function(err) {
throw err;
//self.connected = false;
//self.emit('disconnect');
//console.log('initiating reconnection algorithm...');
//TODO Reconnections http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html
//TODO use circular queue to reduce the amount
//packets being lost when disconnected.
});

//initiates connection
this.req.write('\n');
};

this.send = function(message) {
if(!this.connected) {
if (!this.connected) {
this.emit('error', {errors:
[ { code:'000',
description: 'There is not an open connection with ' +
Expand All @@ -111,10 +127,8 @@ util.inherits(Connection, EventEmitter);
};

this.disconnect = function() {
try {
if (this.connected) {
this.req.end();
} catch(e) {
console.log(e.stack);
}
};
}).call(Connection.prototype);
Expand Down
66 changes: 35 additions & 31 deletions participation/specs/swarm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('Swarm participation API', function() {
var producer = new Swarm(options);
producer.on('message', function(message) {
//we need to fail if this callback gets called.
console.log('message -> ' + JSON.stringify(message));
true.should.be.eql(false);
});

Expand All @@ -77,14 +78,17 @@ describe('Swarm participation API', function() {
});

producer.on('connect', function(err) {
producer.disconnect();
//producer.disconnect();
});

producer.on('presence', function(presence) {
//console.log('presence -> ' + JSON.stringify(presence));
presence.from.resource.should.be.eql(resourceId);
this.disconnect();
});

producer.on('disconnect', function() {
//console.log('disconnected');
done();
});

Expand Down Expand Up @@ -133,6 +137,7 @@ describe('Swarm participation API', function() {
message.public.should.be.eql(true);
consumer.disconnect();
producer.disconnect();
consumer.removeAllListeners('message');
done();
});

Expand All @@ -143,13 +148,11 @@ describe('Swarm participation API', function() {
});

consumer.on('presence', function(presence) {
//console.log(i + ') consumer received -> ' + JSON.stringify(presence));
consumer.send('I should not be able to send this message');
});

consumer.on('connect', function() {
consumer.send('I should not be able to send this message');
//console.log('consumer ' + _resource.id + ' connected!');
});
consumer.on('disconnect', function() {});
consumer.on('connect', function() {});

consumer.connect();

Expand All @@ -159,20 +162,19 @@ describe('Swarm participation API', function() {
});

producer.on('presence', function(presence) {
if (presence.from.resource &&
!presence.type && //available
if (presence.from.resource &&
(!presence.type || presence.type == 'available') && //available
presence.from.resource == consumerOptions.resource) {
//lets send a message to the consumer once it shows up in
//Sends a message to the consumer once it shows up in
//the swarm.
producer.send('yo producer 1');
//console.log('producer received -> ' + JSON.stringify(presence));
}
//console.log('producer received -> ' + JSON.stringify(presence));
});

producer.on('error', function(err) {
//we need to fail if this callback gets called.
true.should.be.eql(false);
producer.disconnect();
});

producer.on('connect', function() {
Expand Down Expand Up @@ -217,13 +219,15 @@ describe('Swarm participation API', function() {
var count = 0;
var prosumer = new Swarm(options);
prosumer.on('message', function(message) {
message.from.swarm.should.be.eql(swarmId);
message.from.resource.should.be.eql(_resource.id);
message.payload.should.be.eql('yo!');
count++;
if(count == 3) {
prosumer.disconnect();
done();
if (message.from.swarm &&
message.from.swarm == swarmId &&
message.from.resource == _resource.id) {
message.payload.should.be.eql('yo!');
count++;
if(count == 3) {
prosumer.disconnect();
done();
}
}
});

Expand Down Expand Up @@ -266,7 +270,7 @@ describe('Swarm participation API', function() {
var consumer = new Swarm(options2);

prosumer.on('presence', function(presence) {
if(presence.from.resource == consumerId) {
if (presence.from.resource == consumerId) {
for(var i = 0; i < 4; i++) {
prosumer.send({ message: 'yo in private!',
swarms: swarmId,
Expand Down Expand Up @@ -336,12 +340,12 @@ describe('Swarm participation API', function() {

var count = 0;
prosumer.on('presence', function(presence) {
presence.from.resource.should.be.eql(prosumerId);

if (presence.from.swarm == swarm1.id ||
presence.from.swarm == swarm2.id) {
presence.from.resource.should.be.eql(prosumerId);

count++;
if(count == 2) {
if (count == 2) {
prosumer.send(count);
}
}
Expand All @@ -352,7 +356,7 @@ describe('Swarm participation API', function() {
count2++;
message.from.resource.should.be.eql(prosumerId);
message.payload.should.be.eql(2);
if(count2 == 2) {
if (count2 == 2) {
done();
prosumer.disconnect();
}
Expand All @@ -366,8 +370,8 @@ describe('Swarm participation API', function() {
});
});

it('should allow unregistered consumers if swarm is public', function(done) {
/*var swarmService = new SwarmService(cfgKey);
it('should allow unregistered consumers if swarm is public')/*, function(done) {
var swarmService = new SwarmService(cfgKey);
//create a public swarm
var myswarm = {
Expand Down Expand Up @@ -405,9 +409,9 @@ describe('Swarm participation API', function() {
});
consumer.connect();
});*/
});
done();
});
});*/

it('should support sending payloads bigger than 1500 bytes', function(done) {
var options = {
Expand All @@ -422,12 +426,12 @@ describe('Swarm participation API', function() {
});

var message = '';
for(var i = 0; i < 3000; i++) {
for (var i = 0; i < 3000; i++) {
message += i + ',';
}

prosumer.on('presence', function(presence) {
if(presence.from.swarm == swarmId) {
if (presence.from.swarm == swarmId) {
prosumer.send(message);
}
});
Expand All @@ -441,7 +445,7 @@ describe('Swarm participation API', function() {
prosumer.connect();
});

it('should not miss messages if connection goes down')
it('should not miss messages if connection goes down');

it('should re-connect if connection goes down')
it('should re-connect if connection goes down');
});

0 comments on commit 30764a2

Please sign in to comment.