Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
greggman committed Oct 3, 2014

Verified

This commit was signed with the committer’s verified signature.
alexvanboxel Alex Van Boxel
1 parent 230cfe2 commit 10e24af
Showing 4 changed files with 78 additions and 6 deletions.
12 changes: 12 additions & 0 deletions lib/test/test-controller.js
Original file line number Diff line number Diff line change
@@ -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,13 +66,24 @@ var TestController = function(options) {
assert(connected, "connected");
connected = false;
});
client.addEventListener('testmsg', function(data) {
receivedMessages.push({cmd: 'testmsg', data: data});
});

this.socket = wsclient;

this.close = function() {
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();
};
36 changes: 31 additions & 5 deletions lib/test/test-game.js
Original file line number Diff line number Diff line change
@@ -47,20 +47,32 @@ 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");
assert(connected, "connected");
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();
};
31 changes: 31 additions & 0 deletions test/client/roundtrip-test.js
Original file line number Diff line number Diff line change
@@ -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();
});

5 changes: 4 additions & 1 deletion test/server/server-version-test.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 10e24af

Please sign in to comment.