From 84acb8a217dac8aa476582f5a4be6284821364f6 Mon Sep 17 00:00:00 2001 From: Michelle Bu Date: Sat, 19 Oct 2013 00:14:51 -0700 Subject: [PATCH] handleTransmission tests --- lib/server.js | 2 +- test/server.js | 101 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/lib/server.js b/lib/server.js index 0e5d087..412ce87 100644 --- a/lib/server.js +++ b/lib/server.js @@ -385,7 +385,7 @@ PeerServer.prototype._handleTransmission = function(key, message) { } else { // Wait for this client to connect/reconnect (XHR) for important // messages. - if (type !== 'LEAVE' && type !== 'EXPIRE' && !!dst) { + if (type !== 'LEAVE' && type !== 'EXPIRE' && dst) { var self = this; if (!this._outstanding[key][dst]) { this._outstanding[key][dst] = []; diff --git a/test/server.js b/test/server.js index 8a04bc5..e40d858 100644 --- a/test/server.js +++ b/test/server.js @@ -92,11 +92,110 @@ describe('PeerServer', function() { }); describe('#_removePeer', function() { + var p; + before(function() { + PeerServer.prototype._initializeHTTP = sinon.stub(); + p = new PeerServer({ port: 8000 }); + var fake = {ip: '0.0.0.0'}; + p._ips[fake.ip] = 1; + p._clients['peerjs'] = {}; + p._clients['peerjs']['test'] = fake; + }); + + it('should decrement the number of ips being used and remove the connection', function() { + expect(p._ips['0.0.0.0']).to.be(1); + p._removePeer('peerjs', 'test'); + expect(p._ips['0.0.0.0']).to.be(0); + expect(p._clients['peerjs']['test']).to.be(undefined); + }); }); describe('#_handleTransmission', function() { - // TODO: this is probably the most important method to test. + var p; + var KEY = 'peerjs'; + var ID = 'test'; + before(function() { + PeerServer.prototype._initializeHTTP = sinon.stub(); + p = new PeerServer({ port: 8000 }); + p._clients[KEY] = {}; + }); + + it('should send to the socket when appropriate', function() { + var send = sinon.spy(); + var write = sinon.spy(); + var message = {dst: ID}; + p._clients[KEY][ID] = { + socket: { + send: send + }, + res: { + write: write + } + } + p._handleTransmission(KEY, message); + expect(send.calledWith(JSON.stringify(message))).to.be(true); + expect(write.calledWith(JSON.stringify(message))).to.be(false); + }); + + it('should write to the response with a newline when appropriate', function() { + var write = sinon.spy(); + var message = {dst: ID}; + p._clients[KEY][ID] = { + res: { + write: write + } + } + p._handleTransmission(KEY, message); + expect(write.calledWith(JSON.stringify(message) + '\n')).to.be(true); + }); + + // no destination. + it('should push to outstanding messages if the destination is not found', function() { + var message = {dst: ID}; + p._outstanding[KEY] = {}; + p._clients[KEY] = {}; + p._handleTransmission(KEY, message); + expect(p._outstanding[KEY][ID][0]).to.be(message); + }); + + it('should not push to outstanding messages if the message is a LEAVE or EXPIRE', function() { + var message = {dst: ID, type: 'LEAVE'}; + p._outstanding[KEY] = {}; + p._clients[KEY] = {}; + p._handleTransmission(KEY, message); + expect(p._outstanding[KEY][ID]).to.be(undefined); + + message = {dst: ID, type: 'EXPIRE'}; + p._handleTransmission(KEY, message); + expect(p._outstanding[KEY][ID]).to.be(undefined); + }); + + it('should remove the peer if there is no dst in the message', function() { + var message = {type: 'LEAVE'}; + p._removePeer = sinon.spy(); + p._outstanding[KEY] = {}; + p._handleTransmission(KEY, message); + expect(p._removePeer.calledWith(KEY, undefined)).to.be(true); + }); + + it('should remove the peer and send a LEAVE message if the socket appears to be closed', function() { + var send = sinon.stub().throws(); + var message = {dst: ID}; + var leaveMessage = {type: 'LEAVE', dst: undefined, src: ID}; + var oldHandleTransmission = p._handleTransmission; + p._removePeer = function() { + // Hacks! + p._handleTransmission = sinon.spy(); + }; + p._clients[KEY][ID] = { + socket: { + send: send + } + } + p._handleTransmission(KEY, message); + expect(p._handleTransmission.calledWith(KEY, leaveMessage)).to.be(true); + }); }); describe('#_generateClientId', function() {