Merge pull request #2037 from Slayer95/test

Add regression test for players being unable to join battles
This commit is contained in:
Guangcong Luo 2015-07-07 03:17:40 -05:00
commit f67fd91590
4 changed files with 66 additions and 28 deletions

23
dev-tools/users-utils.js Normal file
View File

@ -0,0 +1,23 @@
function createConnection (ip, workerid, socketid) {
if (!workerid || !socketid) {
workerid = Object.keys(Sockets.workers)[0];
socketid = 1;
while (Users.connections[workerid + '-' + socketid]) {
socketid++;
}
}
var connectionid = workerid + '-' + socketid;
var connection = Users.connections[connectionid] = new Users.Connection(connectionid, Sockets.workers[workerid], socketid, null, ip || '127.0.0.1');
return connection;
}
function createUser (connection) {
if (!connection) connection = createConnection();
var user = new Users.User(connection);
connection.user = user;
user.joinRoom('global', connection);
return user;
}
exports.Connection = createConnection;
exports.User = createUser;

View File

@ -1,6 +1,9 @@
var assert = require('assert');
var room;
var userUtils = require('./../../dev-tools/users-utils.js');
var User = userUtils.User;
describe('Rooms features', function () {
describe('Rooms', function () {
describe('Rooms.get', function () {
@ -22,4 +25,24 @@ describe('Rooms features', function () {
});
});
});
describe('BattleRoom', function () {
var room;
afterEach(function () {
if (room) room.expire();
});
it('should allow two users to join the battle', function () {
var p1 = new User();
var p2 = new User();
var packedTeam = 'Weavile||lifeorb||swordsdance,knockoff,iceshard,iciclecrash|Jolly|,252,,,4,252|||||';
var options = [{rated: false, tour: false}, {rated: false, tour: true}, {rated: true, tour: false}, {rated: true, tour: true}];
options.forEach(function (option) {
room = Rooms.global.startBattle(p1, p2, 'customgame', packedTeam, packedTeam, option);
if (room.active) return assert.ok(room.battle.players.none(null)); // Automatically joined
assert.ok(room.joinBattle(p1, packedTeam));
assert.ok(room.joinBattle(p2, packedTeam));
});
});
});
});

View File

@ -1,24 +1,8 @@
var assert = require('assert');
function createConnection (ip, workerid, socketid) {
if (!workerid || !socketid) {
workerid = Object.keys(Sockets.workers)[0];
socketid = 1;
while (Users.connections[workerid + '-' + socketid]) {
socketid++;
}
}
var connectionid = workerid + '-' + socketid;
var connection = Users.connections[connectionid] = new Users.Connection(connectionid, Sockets.workers[workerid], socketid, null, ip || '127.0.0.1');
return connection;
}
function createUser (connection) {
if (!connection) connection = createConnection();
var user = new Users.User(connection);
connection.user = user;
user.joinRoom('global', connection);
return user;
}
var userUtils = require('./../../dev-tools/users-utils.js');
var Connection = userUtils.Connection;
var User = userUtils.User;
describe('Users features', function () {
describe('Users', function () {
@ -44,9 +28,9 @@ describe('Users features', function () {
describe('#disconnectAll', function () {
[1, 2].forEach(function (totalConnections) {
it('should drop all ' + totalConnections + ' connection(s) and mark as inactive', function () {
var user = createUser();
var user = new User();
var iterations = totalConnections;
while (--iterations) user.mergeConnection(createConnection());
while (--iterations) user.mergeConnection(new Connection());
user.disconnectAll();
assert.strictEqual(user.connections.length, 0);
@ -54,9 +38,9 @@ describe('Users features', function () {
});
it('should unref all ' + totalConnections + ' connection(s)', function () {
var user = createUser();
var user = new User();
var iterations = totalConnections;
while (--iterations) user.mergeConnection(createConnection());
while (--iterations) user.mergeConnection(new Connection());
var connections = user.connections.slice();
@ -67,9 +51,9 @@ describe('Users features', function () {
});
it('should clear `user` property for all ' + totalConnections + ' connection(s)', function () {
var user = createUser();
var user = new User();
var iterations = totalConnections;
while (--iterations) user.mergeConnection(createConnection());
while (--iterations) user.mergeConnection(new Connection());
var connections = user.connections.slice();
user.disconnectAll();
@ -87,20 +71,20 @@ describe('Users features', function () {
});
it('should disconnect every user at that IP', function () {
var users = ['127.0.0.1', '127.0.0.1'].map(function (ip) {return createUser(createConnection(ip));});
var users = ['127.0.0.1', '127.0.0.1'].map(function (ip) {return new User(new Connection(ip));});
users[0].ban();
assert.strictEqual(users[0].connected, false);
assert.strictEqual(users[1].connected, false);
});
it('should not disconnect users at other IPs', function () {
var users = ['127.0.0.1', '127.0.0.2'].map(function (ip) {return createUser(createConnection(ip));});
var users = ['127.0.0.1', '127.0.0.2'].map(function (ip) {return new User(new Connection(ip));});
users[0].ban();
assert.strictEqual(users[1].connected, true);
});
it('should update IP count properly', function () {
var user = createUser();
var user = new User();
user.ban();
for (var ip in user.ips) {
assert.strictEqual(user.ips[ip], 0);

View File

@ -36,6 +36,14 @@ function init (callback) {
BattleEngine.Battle.prototype.send = function () {};
BattleEngine.Battle.prototype.receive = function () {};
var Simulator = global.Simulator;
Simulator.Battle.prototype.send = function () {};
Simulator.Battle.prototype.receive = function () {};
Simulator.SimulatorProcess.processes.forEach(function (process) {
// Don't crash -we don't care of battle child processes.
process.process.on('error', function () {});
});
// Deterministic tests
BattleEngine.Battle.prototype._init = BattleEngine.Battle.prototype.init;
BattleEngine.Battle.prototype.init = function (roomid, formatarg, rated) {