Tests: added ai vs ai load testing

This commit is contained in:
Oleg Agafonov 2018-01-28 15:12:00 +04:00
parent c2d661eca7
commit b114eaa828
3 changed files with 81 additions and 19 deletions

View file

@ -4,6 +4,7 @@ import mage.constants.PhaseStep;
import mage.constants.PlayerAction;
import mage.interfaces.callback.CallbackClient;
import mage.interfaces.callback.ClientCallback;
import mage.player.ai.ComputerPlayer;
import mage.remote.Session;
import mage.util.RandomUtil;
import mage.utils.CompressUtil;
@ -34,22 +35,11 @@ public class LoadCallbackClient implements CallbackClient {
@Override
public void processCallback(ClientCallback callback) {
//TODO
controlCount = 0;
callback.setData(CompressUtil.decompress(callback.getData()));
/*
// random sleep can help with freezes (server concurrent access problem?!)
try {
Thread.sleep(RandomUtil.nextInt(1000));
}catch (InterruptedException e) {
log.error("thread error", e);
}
*/
log.info(callback.getMethod());
log.info(getLogStartInfo() + "callback: " + callback.getMethod());
switch (callback.getMethod()) {
case START_GAME: {
@ -223,4 +213,8 @@ public class LoadCallbackClient implements CallbackClient {
public String getLastGameResult() {
return this.gameResult;
}
public GameView getLastGameView() {
return this.gameView;
}
}

View file

@ -10,6 +10,7 @@ import mage.cards.repository.CardRepository;
import mage.constants.*;
import mage.game.match.MatchOptions;
import mage.player.ai.ComputerPlayer;
import mage.players.Player;
import mage.players.PlayerType;
import mage.remote.Connection;
import mage.remote.MageRemoteException;
@ -119,7 +120,6 @@ public class LoadTest {
@Test
@Ignore
public void test_TwoUsersPlayGameUntilEnd() {
// simple connection to server test
// monitor other players
LoadPlayer monitor = new LoadPlayer("monitor");
@ -130,7 +130,7 @@ public class LoadTest {
// game by user 1
GameTypeView gameType = player1.session.getGameTypes().get(0);
MatchOptions gameOptions = createSimpleGameOptions(gameType, player1.session);
MatchOptions gameOptions = createSimpleGameOptionsForBots(gameType, player1.session);
TableView game = player1.session.createTable(player1.roomID, gameOptions);
UUID tableId = game.getTableId();
Assert.assertEquals(player1.userName, game.getControllerName());
@ -181,6 +181,61 @@ public class LoadTest {
}
}
@Test
@Ignore
public void test_TwoAIPlayGameUntilEnd() {
// monitor and game source
LoadPlayer monitor = new LoadPlayer("monitor");
// game by monitor
GameTypeView gameType = monitor.session.getGameTypes().get(0);
MatchOptions gameOptions = createSimpleGameOptionsForAI(gameType, monitor.session);
TableView game = monitor.session.createTable(monitor.roomID, gameOptions);
UUID tableId = game.getTableId();
DeckCardLists deckList = createSimpleDeck("GR", false);
Optional<TableView> checkGame;
// join AI
Assert.assertTrue(monitor.session.joinTable(monitor.roomID, tableId, "ai_1", PlayerType.COMPUTER_MAD, 5, deckList, ""));
Assert.assertTrue(monitor.session.joinTable(monitor.roomID, tableId, "ai_2", PlayerType.COMPUTER_MAD, 5, deckList, ""));
// match start
Assert.assertTrue(monitor.session.startMatch(monitor.roomID, tableId));
// playing until game over
boolean startToWatching = false;
while(true) {
checkGame = monitor.getTable(tableId);
TableState state = checkGame.get().getTableState();
logger.warn(state);
if (state == TableState.FINISHED) {
break;
}
if (!startToWatching && state == TableState.DUELING) {
Assert.assertTrue(monitor.session.watchGame(checkGame.get().getGames().iterator().next()));
startToWatching = true;
}
GameView gameView = monitor.client.getLastGameView();
if (gameView != null) {
for (PlayerView p : gameView.getPlayers()) {
logger.info(p.getName() + " - Life=" + p.getLife() + "; Lib=" + p.getLibraryCount());
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e){
logger.error(e.getMessage(), e);
}
}
}
@Test
@Ignore
public void test_GameThread() {
@ -348,11 +403,11 @@ public class LoadTest {
return con;
}
private MatchOptions createSimpleGameOptions(GameTypeView gameTypeView, Session session) {
MatchOptions options = new MatchOptions("Test game", gameTypeView.getName(), false, 2);
private MatchOptions createSimpleGameOptions(String gameName, GameTypeView gameTypeView, Session session, PlayerType playersType) {
MatchOptions options = new MatchOptions(gameName, gameTypeView.getName(), false, 2);
options.getPlayerTypes().add(PlayerType.HUMAN);
options.getPlayerTypes().add(PlayerType.HUMAN);
options.getPlayerTypes().add(playersType);
options.getPlayerTypes().add(playersType);
options.setDeckType(session.getDeckTypes()[0]);
options.setLimited(false);
@ -363,6 +418,14 @@ public class LoadTest {
return options;
}
private MatchOptions createSimpleGameOptionsForBots(GameTypeView gameTypeView, Session session) {
return createSimpleGameOptions("Bots test game", gameTypeView, session, PlayerType.HUMAN);
}
private MatchOptions createSimpleGameOptionsForAI(GameTypeView gameTypeView, Session session) {
return createSimpleGameOptions("AI test game", gameTypeView, session, PlayerType.COMPUTER_MAD);
}
private Deck generateRandomDeck(String colors, boolean onlyBasicLands) {
logger.info("Building " + (onlyBasicLands ? "only lands" : "random") + " deck with colors: " + colors);
@ -438,7 +501,7 @@ public class LoadTest {
public UUID createNewTable() {
GameTypeView gameType = this.session.getGameTypes().get(0);
MatchOptions gameOptions = createSimpleGameOptions(gameType, this.session);
MatchOptions gameOptions = createSimpleGameOptionsForBots(gameType, this.session);
TableView game = this.session.createTable(this.roomID, gameOptions);
this.createdTableID = game.getTableId();
Assert.assertEquals(this.userName, game.getControllerName());

View file

@ -6,6 +6,7 @@ import mage.interfaces.callback.CallbackClient;
import mage.interfaces.callback.ClientCallback;
import mage.remote.Session;
import mage.utils.MageVersion;
import mage.view.GameView;
import org.apache.log4j.Logger;
/**
@ -76,4 +77,8 @@ public class SimpleMageClient implements MageClient {
public String getLastGameResult() {
return this.callbackClient.getLastGameResult();
}
public GameView getLastGameView() {
return this.callbackClient.getLastGameView();
}
}