Adding new test case to run a ranking game.

This commit is contained in:
lotodore
2010-11-14 01:10:24 +00:00
parent e69135f1e9
commit 7f91cd091d
6 changed files with 271 additions and 15 deletions
+1 -1
View File
@@ -39,7 +39,7 @@ public class CreateGameTest extends TestBase {
l.add(600);
l.add(1000);
NetGameInfo gameInfo = createGameInfo(8, EndRaiseModeEnumType.EnumType.raiseByEndValue, 1000, 100, GuestUser + " create test game", l, 10, 0, 7, 2000);
sendMessage(createJoinGameRequestMsg(
sendMessage(createGameRequestMsg(
gameInfo,
NetGameTypeEnumType.EnumType.normalGame,
20,
@@ -37,7 +37,7 @@ public class CreateRankingGameTest extends TestBase {
counter++;
Collection<Integer> l = new ArrayList<Integer>();
NetGameInfo gameInfo = createGameInfo(8, EndRaiseModeEnumType.EnumType.doubleBlinds, 0, 50, GuestUser + " create ranking game " + counter, l, 10, 0, 11, 10000);
sendMessage(createJoinGameRequestMsg(
sendMessage(createGameRequestMsg(
gameInfo,
NetGameTypeEnumType.EnumType.rankingGame,
20,
+18 -1
View File
@@ -1,3 +1,20 @@
/* PokerTH automated tests.
Copyright (C) 2010 Lothar May
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package pokerth_test;
import static org.junit.Assert.fail;
@@ -26,7 +43,7 @@ public class RunNormalGameTest extends TestBase {
Collection<Integer> l = new ArrayList<Integer>();
NetGameInfo gameInfo = createGameInfo(5, EndRaiseModeEnumType.EnumType.doubleBlinds, 0, 100, GuestUser + " run normal game", l, 10, 0, 2, 2000);
sendMessage(createJoinGameRequestMsg(
sendMessage(createGameRequestMsg(
gameInfo,
NetGameTypeEnumType.EnumType.normalGame,
10,
@@ -0,0 +1,216 @@
/* PokerTH automated tests.
Copyright (C) 2010 Lothar May
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package pokerth_test;
import java.net.InetAddress;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import pokerth_protocol.*;
import pokerth_protocol.MyActionRequestMessage.MyActionRequestMessageSequenceType;
import pokerth_protocol.NetGameInfo.EndRaiseModeEnumType;
import pokerth_protocol.NetGameInfo.NetGameTypeEnumType;
import pokerth_protocol.StartEventAckMessage.StartEventAckMessageSequenceType;
import pokerth_protocol.StartEventMessage.StartEventMessageSequenceType;
public class RunRankingGameTest extends TestBase {
Connection dbConn;
@Before
public void dbInit() throws Exception {
final String userName = "username";
final String password = "password";
final String url = "jdbc:mysql://localhost:3306/database";
Class.forName("com.mysql.jdbc.Driver").newInstance ();
dbConn = DriverManager.getConnection(url, userName, password);
}
@Test
public void testRunRankingGame() throws Exception {
long firstPlayerId = userInit();
Collection<Integer> l = new ArrayList<Integer>();
NetGameInfo gameInfo = createGameInfo(5, EndRaiseModeEnumType.EnumType.doubleBlinds, 0, 50, GuestUser + " run ranking game", l, 10, 0, 11, 10000);
sendMessage(createGameRequestMsg(
gameInfo,
NetGameTypeEnumType.EnumType.rankingGame,
5,
7,
""));
PokerTHMessage msg;
// Game list update (new game)
msg = receiveMessage();
if (!msg.isGameListMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
// Join game ack.
msg = receiveMessage();
if (msg.isJoinGameReplyMessageSelected()) {
if (!msg.getJoinGameReplyMessage().getValue().getJoinGameResult().isJoinGameAckSelected()) {
fail("Could not create game!");
}
}
else {
failOnErrorMessage(msg);
fail("Invalid message.");
}
long gameId = msg.getJoinGameReplyMessage().getValue().getGameId().getValue();
// Game list update (player joined).
msg = receiveMessage();
if (!msg.isGameListMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
// Let 9 additional clients join.
Socket s[] = new Socket[9];
long playerId[] = new long[9];
for (int i = 0; i < 9; i++) {
s[i] = new Socket("::1", 7234);
String username = "test" + (i+1);
String password = username;
playerId[i] = userInit(s[i], username, password);
sendMessage(joinGameRequestMsg(gameId, ""), s[i]);
do {
msg = receiveMessage(s[i]);
failOnErrorMessage(msg);
} while (!msg.isJoinGameReplyMessageSelected());
if (!msg.getJoinGameReplyMessage().getValue().getJoinGameResult().isJoinGameAckSelected()) {
fail("User " + username + " could not join ranking game.");
}
// The player should have joined the game.
msg = receiveMessage();
if (!msg.isPlayerListMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
msg = receiveMessage();
if (!msg.isGamePlayerMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
msg = receiveMessage();
if (!msg.isGameListMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
}
// Server should automatically send start event.
msg = receiveMessage();
if (!msg.isStartEventMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
for (int i = 0; i < 9; i++) {
do {
msg = receiveMessage(s[i]);
failOnErrorMessage(msg);
} while (!msg.isStartEventMessageSelected());
}
// Acknowledge start event.
StartEventAckMessageSequenceType startType = new StartEventAckMessageSequenceType();
startType.setGameId(new NonZeroId(gameId));
StartEventAckMessage startAck = new StartEventAckMessage();
startAck.setValue(startType);
msg = new PokerTHMessage();
msg.selectStartEventAckMessage(startAck);
sendMessage(msg);
for (int i = 0; i < 9; i++) {
sendMessage(msg, s[i]);
}
// Game list update (game now running).
msg = receiveMessage();
if (!msg.isGameListMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
msg = receiveMessage();
if (!msg.isGameStartMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
long handNum = 0;
do {
msg = receiveMessage();
failOnErrorMessage(msg);
if (msg.isHandStartMessageSelected()) {
handNum++;
}
else if (msg.isPlayersTurnMessageSelected()) {
if (msg.getPlayersTurnMessage().getValue().getPlayerId().getValue() == firstPlayerId) {
NetPlayerAction action = new NetPlayerAction();
action.setValue(NetPlayerAction.EnumType.actionAllIn);
MyActionRequestMessageSequenceType myRequest = new MyActionRequestMessageSequenceType();
myRequest.setGameId(new NonZeroId(gameId));
myRequest.setGameState(msg.getPlayersTurnMessage().getValue().getGameState());
myRequest.setHandNum(new NonZeroId(handNum));
myRequest.setMyAction(action);
myRequest.setMyRelativeBet(0);
MyActionRequestMessage myAction = new MyActionRequestMessage();
myAction.setValue(myRequest);
PokerTHMessage outMsg = new PokerTHMessage();
outMsg.selectMyActionRequestMessage(myAction);
sendMessage(outMsg);
}
}
for (int i = 0; i < 9; i++) {
while (s[i].getInputStream().available() > 0) {
PokerTHMessage inMsg = receiveMessage(s[i]);
failOnErrorMessage(inMsg);
if (inMsg.isPlayersTurnMessageSelected()) {
if (inMsg.getPlayersTurnMessage().getValue().getPlayerId().getValue() == playerId[i]) {
NetPlayerAction action = new NetPlayerAction();
action.setValue(NetPlayerAction.EnumType.actionFold);
MyActionRequestMessageSequenceType myRequest = new MyActionRequestMessageSequenceType();
myRequest.setGameId(new NonZeroId(gameId));
myRequest.setGameState(inMsg.getPlayersTurnMessage().getValue().getGameState());
myRequest.setHandNum(new NonZeroId(handNum));
myRequest.setMyAction(action);
myRequest.setMyRelativeBet(0);
MyActionRequestMessage myAction = new MyActionRequestMessage();
myAction.setValue(myRequest);
PokerTHMessage outMsg = new PokerTHMessage();
outMsg.selectMyActionRequestMessage(myAction);
sendMessage(outMsg, s[i]);
}
}
}
}
} while (!msg.isEndOfGameMessageSelected());
}
}
@@ -37,7 +37,7 @@ public class StartNormalGameTest extends TestBase {
Collection<Integer> l = new ArrayList<Integer>();
NetGameInfo gameInfo = createGameInfo(10, EndRaiseModeEnumType.EnumType.doubleBlinds, 0, 100, GuestUser + " start normal game", l, 10, 0, 11, 20000);
sendMessage(createJoinGameRequestMsg(
sendMessage(createGameRequestMsg(
gameInfo,
NetGameTypeEnumType.EnumType.normalGame,
10,
+34 -11
View File
@@ -50,13 +50,12 @@ public abstract class TestBase {
protected IEncoder<PokerTHMessage> encoder;
protected IDecoder decoder;
private Socket sock;
protected Socket sock;
@Before
public void setUp() throws Exception {
Thread.sleep(2000);
InetAddress localaddr = InetAddress.getLocalHost();
sock = new Socket(localaddr, 7234);
sock = new Socket("::1", 7234);
encoder = CoderFactory.getInstance().newEncoder("BER");
decoder = CoderFactory.getInstance().newDecoder("BER");
}
@@ -120,18 +119,19 @@ public abstract class TestBase {
fail("Invalid message.");
}
// Waiting for player list update.
msg = receiveMessage();
msg = receiveMessage(s);
if (!msg.isPlayerListMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
}
public void userInit() throws Exception {
userInit(sock);
public long userInit() throws Exception {
return userInit(sock, AuthUser, AuthPassword);
}
public void userInit(Socket s) throws Exception {
public long userInit(Socket s, String user, String password) throws Exception {
long playerId = 0;
PokerTHMessage msg = receiveMessage(s);
AnnounceMessage announce = msg.getAnnounceMessage();
assertTrue(announce.getValue().getServerType().getValue() == ServerTypeEnumType.EnumType.serverTypeInternetAuth);
@@ -143,7 +143,7 @@ public abstract class TestBase {
requestedVersion.setMajor(PROTOCOL_VERSION_MAJOR);
requestedVersion.setMinor(PROTOCOL_VERSION_MINOR);
AuthenticatedLogin authLogin = new AuthenticatedLogin();
authLogin.setClientUserData(scramAuth.executeStep1(AuthUser).getBytes());
authLogin.setClientUserData(scramAuth.executeStep1(user).getBytes());
LoginChoiceType loginType = new LoginChoiceType();
loginType.selectAuthenticatedLogin(authLogin);
InitMessageSequenceType msgType = new InitMessageSequenceType();
@@ -162,7 +162,7 @@ public abstract class TestBase {
{
String serverFirstMessage = new String(msg.getAuthMessage().getValue().getAuthServerChallenge().getServerChallenge());
AuthClientResponse authClient = new AuthClientResponse();
authClient.setClientResponse(scramAuth.executeStep2(AuthPassword, serverFirstMessage).getBytes());
authClient.setClientResponse(scramAuth.executeStep2(password, serverFirstMessage).getBytes());
AuthMessageChoiceType authChoice = new AuthMessageChoiceType();
authChoice.selectAuthClientResponse(authClient);
AuthMessage authResponse = new AuthMessage();
@@ -182,20 +182,24 @@ public abstract class TestBase {
InitAckMessage initAck = msg.getInitAckMessage();
assertTrue(initAck.getValue().getYourPlayerId().getValue() != 0L);
assertTrue(!initAck.getValue().isYourAvatarPresent());
playerId = initAck.getValue().getYourPlayerId().getValue();
}
else {
failOnErrorMessage(msg);
fail("Invalid message.");
}
// Waiting for player list update.
msg = receiveMessage();
do {
msg = receiveMessage(s);
} while (msg.isGameListMessageSelected() || msg.isGamePlayerMessageSelected());
if (!msg.isPlayerListMessageSelected()) {
failOnErrorMessage(msg);
fail("Invalid message.");
}
return playerId;
}
public PokerTHMessage createJoinGameRequestMsg(NetGameInfo gameInfo, NetGameTypeEnumType.EnumType type, int playerActionTimeout, int guiSpeed, String password) {
public PokerTHMessage createGameRequestMsg(NetGameInfo gameInfo, NetGameTypeEnumType.EnumType type, int playerActionTimeout, int guiSpeed, String password) {
NetGameTypeEnumType gameType = new NetGameTypeEnumType();
gameType.setValue(type);
gameInfo.setNetGameType(gameType);
@@ -218,6 +222,25 @@ public abstract class TestBase {
return msg;
}
public PokerTHMessage joinGameRequestMsg(long gameId, String password) {
JoinExistingGame joinExisting = new JoinExistingGame();
joinExisting.setGameId(new NonZeroId(gameId));
JoinGameActionChoiceType joinAction = new JoinGameActionChoiceType();
joinAction.selectJoinExistingGame(joinExisting);
JoinGameRequestMessageSequenceType joinType = new JoinGameRequestMessageSequenceType();
joinType.setJoinGameAction(joinAction);
if (!password.isEmpty()) {
joinType.setPassword(password);
}
JoinGameRequestMessage joinRequest = new JoinGameRequestMessage();
joinRequest.setValue(joinType);
PokerTHMessage msg = new PokerTHMessage();
msg.selectJoinGameRequestMessage(joinRequest);
return msg;
}
public NetGameInfo createGameInfo(int delayBetweenHands, EndRaiseModeEnumType.EnumType endMode, int endRaiseValue, int sb, String gameName,
Collection<Integer> manualBlinds, int maxNumPlayers, int raiseEveryMinutes, int raiseEveryHands, int startMoney) {
NetGameInfo gameInfo = new NetGameInfo();