Implemented MessageRouter.
Added ConnectionToServerCommandFactory, and JoinSuccessfulCommand, RaceParticipantsFullCOmmand, ServerFullCommand. Added IncomingHeartBeatCommandFactory, and IncomingHeartBeatCommand. Added ConnectionToServerState, which represents the client's connection state to server. Renamed VisualiserInput to VisualiserRaceController. Added ConnectionToServer, which tracks the client's connection to server. Added ConnectionToServerController, which accepts JoinAcceptance messages, turns them into commands, and passes them to ConnectionToServer. Added IncomingHeartBeatService, which tracks the heart beat status of the connection. Added IncomingHeartBeatController, which accepts HeartBeat messages, turns them into commands, and passes them to IncomingHeartBeatService. Refactored ServerConnection a bit. #story[1095]
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package visualiser.network;
|
||||
|
||||
import mock.model.commandFactory.Command;
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.Enums.JoinAcceptanceEnum;
|
||||
import network.Messages.Enums.RequestToJoinEnum;
|
||||
import network.Messages.JoinAcceptance;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import visualiser.Commands.ConnectionToServerCommands.JoinSuccessfulCommand;
|
||||
import visualiser.Commands.ConnectionToServerCommands.RaceParticipantsFullCommand;
|
||||
import visualiser.Commands.ConnectionToServerCommands.ServerFullCommand;
|
||||
import visualiser.enums.ConnectionToServerState;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests the {@link ConnectionToServer} class with a Spectator request, and how it reacts to various commands.
|
||||
*/
|
||||
public class ConnectionToServerSpectatorTest {
|
||||
|
||||
private ConnectionToServer connectionToServer;
|
||||
private Thread connectionToServerThread;
|
||||
|
||||
private BlockingQueue<AC35Data> outgoingMessages;
|
||||
private BlockingQueue<Command> incomingCommands;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
incomingCommands = new LinkedBlockingQueue<>();
|
||||
outgoingMessages = new LinkedBlockingQueue<>();
|
||||
|
||||
connectionToServer = new ConnectionToServer(ConnectionToServerState.UNKNOWN, RequestToJoinEnum.SPECTATOR, incomingCommands, outgoingMessages);
|
||||
connectionToServerThread = new Thread(connectionToServer);
|
||||
connectionToServerThread.start();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* When a connection to server is created, is it expected that it will have sent a request and be in the Request_sent state.
|
||||
* @throws Exception On error.
|
||||
*/
|
||||
@Test
|
||||
public void expectRequestSent() throws Exception {
|
||||
|
||||
//Need to wait for connection thread to execute commands.
|
||||
Thread.sleep(20);
|
||||
|
||||
assertEquals(ConnectionToServerState.REQUEST_SENT, connectionToServer.getConnectionState());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sends a join successful command. Expects that the connection becomes Connected.
|
||||
* @throws Exception On error.
|
||||
*/
|
||||
@Test
|
||||
public void sendJoinSuccessCommand() throws Exception {
|
||||
int sourceID = 0;
|
||||
JoinAcceptance joinAcceptance = new JoinAcceptance(JoinAcceptanceEnum.JOIN_SUCCESSFUL, sourceID);
|
||||
|
||||
Command command = new JoinSuccessfulCommand(joinAcceptance, connectionToServer);
|
||||
|
||||
incomingCommands.put(command);
|
||||
|
||||
//Need to wait for connection thread to execute commands.
|
||||
Thread.sleep(20);
|
||||
|
||||
assertEquals(ConnectionToServerState.CONNECTED, connectionToServer.getConnectionState());
|
||||
assertTrue(connectionToServer.getJoinAcceptance() != null);
|
||||
assertEquals(sourceID, connectionToServer.getJoinAcceptance().getSourceID());
|
||||
assertEquals(JoinAcceptanceEnum.JOIN_SUCCESSFUL, connectionToServer.getJoinAcceptance().getAcceptanceType());
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sends a server full command. Expects that the connection becomes Declined.
|
||||
* @throws Exception On error.
|
||||
*/
|
||||
@Test
|
||||
public void sendServerFullCommand() throws Exception {
|
||||
int sourceID = 0;
|
||||
JoinAcceptance joinAcceptance = new JoinAcceptance(JoinAcceptanceEnum.SERVER_FULL, sourceID);
|
||||
|
||||
Command command = new ServerFullCommand(joinAcceptance, connectionToServer);
|
||||
|
||||
incomingCommands.put(command);
|
||||
|
||||
//Need to wait for connection thread to execute commands.
|
||||
Thread.sleep(20);
|
||||
|
||||
assertEquals(ConnectionToServerState.DECLINED, connectionToServer.getConnectionState());
|
||||
assertTrue(connectionToServer.getJoinAcceptance() != null);
|
||||
assertEquals(sourceID, connectionToServer.getJoinAcceptance().getSourceID());
|
||||
assertEquals(JoinAcceptanceEnum.SERVER_FULL, connectionToServer.getJoinAcceptance().getAcceptanceType());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user