Skip to content

Commit

Permalink
fixed unit tests and fixed a bug in rejection of quarantined commands…
Browse files Browse the repository at this point in the history
…, hopefully fixes #14 and/or #15
  • Loading branch information
T1mmos committed Jul 8, 2020
1 parent 1b7e4b7 commit b7e17f1
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ public int getSize()

private int indexOf(UUID id, boolean forwards)
{
int startIdx = forwards ? 0 : getSize() - 1;
int stopIdx = forwards ? getLastIndex() + 1 : -1;
int incr = forwards ? 1 : -1;
for (int i = getCurrentIndex(); i != stopIdx; i += incr)
for (int i = startIdx; i != stopIdx; i += incr)
{
CommandExecution cmdExecution = execLine.get(i);

Expand All @@ -114,9 +115,7 @@ private int indexOf(UUID id, boolean forwards)
}
}

int min = Math.min(stopIdx, getCurrentIndex());
int max = Math.max(stopIdx, getCurrentIndex());
String msg = String.format("No CommandExecution found in the history between [{0} - {1}]", min, max);
String msg = String.format("No CommandExecution found in the command history for id=%s", id);
throw new IllegalArgumentException(msg);
}

Expand Down Expand Up @@ -371,6 +370,10 @@ private List<CommandExecution> redo(List<CommandExecution> list, State state, bo
throw new IllegalStateException("Cannot execute the command " + command.getName()
+ " because of an error in state, reason: " + resp.reason);
}
if (resp.execState == ExecutionState.No)
{
Logger.trace("Cannot execute command because: %s", resp.reason);
}

if (execState == CommandExecutionState.Unexecuted)
{
Expand Down Expand Up @@ -553,19 +556,31 @@ public List<CommandExecution> reject(UUID commandId, State state)
{
throw new IllegalStateException("Cannot remove!");
}

CommandExecution cmdExec = getCommandExecution(commandId);

// a quarantined command is unexecuted so can be safely removed from the
// command history, as if it never existed
if (cmdExec.getExecutionState() == CommandExecutionState.Quarantined)
{
execLine.remove(cmdExec);
return new ArrayList<>(); // there are no failures as no commands need to be redone
}
else
{
// undo all the commands [idx(commandId) <---- currentIdx]
undo(commandId, state);

// undo all the commands [idx(commandId) <---- currentIdx]
undo(commandId, state);

// enlist all unexecuted commands except the one to erase
List<CommandExecution> list = new ArrayList<>(execLine.subList(getCurrentIndex() + 2, getLastIndex() + 1));
// enlist all unexecuted commands except the one to erase
List<CommandExecution> list = new ArrayList<>(execLine.subList(getCurrentIndex() + 2, getLastIndex() + 1));

// remove the rejected command
execLine.remove(getCurrentIndex() + 1);
// remove the rejected command
execLine.remove(getCurrentIndex() + 1);

// redo all the unexecuted commands, failures are allowed
List<CommandExecution> fails = redo(list, state, false);
return fails;
// redo all the unexecuted commands, failures are allowed
List<CommandExecution> fails = redo(list, state, false);
return fails;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package gent.timdemey.cards.test.common;

import java.util.List;
import java.util.UUID;

import org.junit.BeforeClass;

import gent.timdemey.cards.App;
import gent.timdemey.cards.ICardPlugin;
import gent.timdemey.cards.MockCardPlugin;
import gent.timdemey.cards.Services;
import gent.timdemey.cards.logging.ILogManager;
import gent.timdemey.cards.model.entities.cards.Card;
import gent.timdemey.cards.model.entities.cards.PlayerConfiguration;
import gent.timdemey.cards.readonlymodel.ReadOnlyCard;
import gent.timdemey.cards.readonlymodel.ReadOnlyCardStack;
import gent.timdemey.cards.readonlymodel.ReadOnlyList;
import gent.timdemey.cards.services.context.ContextService;
import gent.timdemey.cards.services.context.ContextType;
import gent.timdemey.cards.services.interfaces.ICardGameService;
import gent.timdemey.cards.services.interfaces.IContextService;
import gent.timdemey.cards.services.interfaces.INetworkService;
import gent.timdemey.cards.test.mock.MockLogManager;
import gent.timdemey.cards.test.mock.MockNetworkService;

public class TestBase
Expand All @@ -34,6 +45,31 @@ public boolean isUiThread()
ctxtService.initialize(ContextType.UI);
App.getServices().install(IContextService.class, ctxtService);
App.getServices().install(INetworkService.class, new MockNetworkService());
App.getServices().install(ILogManager.class, new MockLogManager());
App.getServices().install(ICardGameService.class, new ICardGameService()
{

@Override
public int getScore(ReadOnlyCardStack srcCardStack, ReadOnlyCardStack dstCardStack, ReadOnlyList<ReadOnlyCard> transferedCards)
{
// TODO Auto-generated method stub
return 0;
}

@Override
public List<List<Card>> getCards()
{
// TODO Auto-generated method stub
return null;
}

@Override
public List<PlayerConfiguration> createStacks(List<UUID> playerIds, List<List<Card>> playerCards)
{
// TODO Auto-generated method stub
return null;
}
});
}

public static void installMockCardPlugin()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gent.timdemey.cards.test.mock;

import gent.timdemey.cards.logging.ILogManager;
import gent.timdemey.cards.logging.LogLevel;

public class MockLogManager implements ILogManager
{

@Override
public void log(LogLevel lvl, Object msg)
{
log(lvl, msg.toString());
}

@Override
public void log(LogLevel lvl, String msg, Object... params)
{
String msgstr = String.format(msg, params);
log(lvl, msgstr);
}

@Override
public void log(Throwable ex)
{
ex.printStackTrace(System.err);
}

@Override
public void log(String msg, Throwable ex)
{
log(LogLevel.ERROR, msg);
log(ex);
}

@Override
public void setLogLevel(LogLevel lvl)
{
// we log everything in the mocks
}

private void log(LogLevel lvl, String msg)
{
String format = "MockLogManager :: %10s :: %s";
String msgstr = String.format(format, lvl, msg);
System.out.println(msgstr);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import gent.timdemey.cards.model.entities.commands.CommandExecution;
import gent.timdemey.cards.model.entities.commands.CommandExecutionState;
import gent.timdemey.cards.model.entities.commands.CommandHistory;
import gent.timdemey.cards.model.entities.game.GameState;
import gent.timdemey.cards.model.entities.game.Player;
import gent.timdemey.cards.model.entities.game.UDPServer;
import gent.timdemey.cards.model.state.State;
Expand Down Expand Up @@ -71,6 +72,7 @@ public void resetGame()
// reset card game
cardGame = SolShowCardGameHelper.createFixedSolShowCardGame(player1, player2);
state.setCardGame(cardGame);
state.setGameState(GameState.Started);
cs_p1depot = cardGame.getCardStack(SolShowTestIds.P1_DEPOT);
cs_p1turnover = cardGame.getCardStack(SolShowTestIds.P1_TURNOVER);
cs_p1special = cardGame.getCardStack(SolShowTestIds.P1_SPECIAL);
Expand Down Expand Up @@ -143,7 +145,7 @@ public void awaitAcceptAwaitAwaitAcceptAccept()
assertEquals(3, cmdHistory.getSize());

// server accepts cmd3
cmdHistory.accept(cmd2.id, state);
cmdHistory.accept(cmd3.id, state);
// test state
assertEquals(2, cmdHistory.getAcceptedIndex());
assertEquals(2, cmdHistory.getCurrentIndex());
Expand Down

0 comments on commit b7e17f1

Please sign in to comment.