From 10e24afb5ba3cb234a46d02707b7d260efae4641 Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Fri, 3 Oct 2014 15:50:56 -0700 Subject: [PATCH] more tests --- lib/test/test-controller.js | 12 ++++++++++ lib/test/test-game.js | 36 +++++++++++++++++++++++++----- test/client/roundtrip-test.js | 31 +++++++++++++++++++++++++ test/server/server-version-test.js | 5 ++++- 4 files changed, 78 insertions(+), 6 deletions(-) diff --git a/lib/test/test-controller.js b/lib/test/test-controller.js index 104cca61..8d90950c 100644 --- a/lib/test/test-controller.js +++ b/lib/test/test-controller.js @@ -55,6 +55,7 @@ var TestController = function(options) { socket: wsclient, quiet: true, }); + var receivedMessages = []; client.addEventListener('connect', function() { debug("connected"); assert(!connected, "not connected"); @@ -65,6 +66,9 @@ var TestController = function(options) { assert(connected, "connected"); connected = false; }); + client.addEventListener('testmsg', function(data) { + receivedMessages.push({cmd: 'testmsg', data: data}); + }); this.socket = wsclient; @@ -72,6 +76,14 @@ var TestController = function(options) { wsclient.close(); }; + this.sendCmd = function(cmd, data) { + client.sendCmd(cmd, data); + }; + + this.getReceivedMessages = function() { + return receivedMessages.slice(); + }; + options.hftServer.getSocketServer().emit('connection', wsclient.server); wsclient.connect(); }; diff --git a/lib/test/test-game.js b/lib/test/test-game.js index 295588e4..07072f17 100644 --- a/lib/test/test-game.js +++ b/lib/test/test-game.js @@ -47,9 +47,10 @@ requirejs.config({ var GameServer = requirejs('hft/gameserver'); // Not to be confused with TestController (client side) -var TestGamePlayer = function(netPlayer, testGame) { +var TestGamePlayer = function(netPlayer, testGame, data) { var connected = true; var self = this; + var receivedMessages = [{cmd: 'connect', data: data}]; netPlayer.addEventListener('disconnect', function() { debug("TestGamePlayer: disconnect"); @@ -57,10 +58,21 @@ var TestGamePlayer = function(netPlayer, testGame) { connected = false; testGame.removePlayer(self); }); + netPlayer.addEventListener('testmsg', function(data) { + receivedMessages.push({cmd: 'testmsg', data: data}); + }); - this.switchGame = function(id) { + this.switchGame = function(id, data) { debug("TestGamePlayer: switchGame: " + id); - netPlayer.switchGame(id); + netPlayer.switchGame(id, data); + }; + + this.sendCmd = function(data) { + netPlayer.sendCmd('testmsg', data); + }; + + this.getReceivedMessages = function() { + return receivedMessages.slice(); }; }; @@ -93,9 +105,9 @@ var TestGame = function(options) { connected = false; }); - server.addEventListener('playerconnect', function(netPlayer, name) { + server.addEventListener('playerconnect', function(netPlayer, name, data) { debug("player added" + id); - players.push(new TestGamePlayer(netPlayer, this)); + players.push(new TestGamePlayer(netPlayer, this, data)); }.bind(this)); server.addEventListener('testmsg', function(data, id) { @@ -117,6 +129,12 @@ var TestGame = function(options) { players.splice(ndx, 1); }; + this.sendMessageToPlayers = function(data) { + players.forEach(function(player) { + player.sendCmd(data); + }) + }; + this.socket = wsclient; this.getId = function() { @@ -139,6 +157,10 @@ var TestGame = function(options) { return players.slice(); }; + this.broadcastCmd= function(cmd, data) { + server.broadcastCmd(cmd, data); + }; + this.broadcastCmdToGames = function(cmd, data) { server.broadcastCmdToGames(cmd, data); }; @@ -151,6 +173,10 @@ var TestGame = function(options) { return receivedMessages.slice(); }; + this.getPlayers = function() { + return players.slice(); + }; + options.hftServer.getSocketServer().emit('connection', wsclient.server); wsclient.connect(); }; diff --git a/test/client/roundtrip-test.js b/test/client/roundtrip-test.js index 2de83b79..bdde92b9 100644 --- a/test/client/roundtrip-test.js +++ b/test/client/roundtrip-test.js @@ -55,6 +55,37 @@ describe('roundtrip', function() { testGame.getNumPlayers().should.be.eql(1); testCtrl.close(); testGame.getNumPlayers().should.be.eql(0); + testGame.close(); + done(); + }); + + it('should be able to send messages to abd from game to controllers', function(done) { + var testGame = new TestGame({hftServer: hftServer}); + var testCtrl1 = new TestController({hftServer: hftServer}); + var testCtrl2 = new TestController({hftServer: hftServer}); + + testGame.getNumPlayers().should.be.eql(2); + + testGame.sendMessageToPlayers({foo: 123}); + testCtrl1.getReceivedMessages().should.containDeep( + [{cmd: 'testmsg', data: {foo: 123}}]); + testCtrl2.getReceivedMessages().should.containDeep( + [{cmd: 'testmsg', data: {foo: 123}}]); + + testGame.broadcastCmd('testmsg', {bar: 456}); + testCtrl1.getReceivedMessages().should.containDeep( + [{cmd: 'testmsg', data: {bar: 456}}]); + testCtrl2.getReceivedMessages().should.containDeep( + [{cmd: 'testmsg', data: {bar: 456}}]); + + testCtrl1.sendCmd('testmsg', {moo: 789}); + testGame.getPlayers()[0].getReceivedMessages().should.containDeep( + [{cmd: 'testmsg', data: {moo: 789}}]); + + testCtrl1.close(); + testCtrl2.close(); + testGame.getNumPlayers().should.be.eql(0); + testGame.close(); done(); }); diff --git a/test/server/server-version-test.js b/test/server/server-version-test.js index 2c3a3ea1..a9929a10 100644 --- a/test/server/server-version-test.js +++ b/test/server/server-version-test.js @@ -184,10 +184,13 @@ describe('server versions', function() { testGame1.getNumPlayers().should.be.eql(1); testGame2.getNumPlayers().should.be.eql(0); var player = testGame1.getPlayers()[0]; - player.switchGame("bbb"); + player.switchGame("bbb", {foo: "abc"}); testGame1.getNumPlayers().should.be.eql(0); testGame2.getNumPlayers().should.be.eql(1); + testGame2.getPlayers()[0].getReceivedMessages().should.containDeep( + [{cmd: 'connect', data: {foo: "abc"}}]); + testGame1.close(); testGame2.close(); done();