Permalink
Browse files

fixes and refine tests

  • Loading branch information...
1 parent c401df9 commit 30764a2182a67d18cdb2568c4e97fce432668308 @c4milo c4milo committed Feb 5, 2012
Showing with 66 additions and 48 deletions.
  1. +1 −1 participation/Makefile
  2. +30 −16 participation/lib/connection.js
  3. +35 −31 participation/specs/swarm.spec.js
View
@@ -1,4 +1,4 @@
-SPECS = specs/*.js
+SPECS = specs/*.spec.js
REPORTER = list
JSLINT := /usr/local/bin/gjslint
FIX_STYLE := /usr/local/bin/fixjsstyle
@@ -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];
}
@@ -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 = '';
}
@@ -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() {
@@ -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 ' +
@@ -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);
@@ -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);
});
@@ -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();
});
@@ -133,6 +137,7 @@ describe('Swarm participation API', function() {
message.public.should.be.eql(true);
consumer.disconnect();
producer.disconnect();
+ consumer.removeAllListeners('message');
done();
});
@@ -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();
@@ -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() {
@@ -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();
+ }
}
});
@@ -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,
@@ -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);
}
}
@@ -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();
}
@@ -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 = {
@@ -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 = {
@@ -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);
}
});
@@ -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.