Created FrameRateTracker which can be used to track framerate. Updated RequestToJoinEnum and JoinAcceptanceEnum to match the agreed connection API. Added AssignPlayerBoat message, which is used internally on the client to assign the player a source ID once they have connected. Fixed some race conditions in the MessageRouter. Updated ConnectionAcceptor.CheckClientConnection to wait slightly longer before removing connection (there was a slight race condition before). Race no longer has a reference to LatestMessages. LatestMessages no longer has specific messages types in it. Created RaceState class, which contains the state that is shared between VisualiserRaceState and MockRaceState (currently only used on visualiser). Split VisualiserRace into VisualiserRaceState and VisualiserRaceService. Added the VisualiserRace commands (BoatLocatonCommand, RaceStatusCommand, etc...). Slightly increased the preferred width of race.fxml table columns. issues #27 #37 #35 #story[1095]
109 lines
3.7 KiB
Java
109 lines
3.7 KiB
Java
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.JoinSuccessParticipantCommand;
|
|
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(250);
|
|
|
|
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_PARTICIPANT, sourceID);
|
|
|
|
Command command = new JoinSuccessParticipantCommand(joinAcceptance, connectionToServer);
|
|
|
|
incomingCommands.put(command);
|
|
|
|
//Need to wait for connection thread to execute commands.
|
|
Thread.sleep(250);
|
|
|
|
assertEquals(ConnectionToServerState.CONNECTED, connectionToServer.getConnectionState());
|
|
assertTrue(connectionToServer.getJoinAcceptance() != null);
|
|
assertEquals(sourceID, connectionToServer.getJoinAcceptance().getSourceID());
|
|
assertEquals(JoinAcceptanceEnum.JOIN_SUCCESSFUL_PARTICIPANT, 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(250);
|
|
|
|
assertEquals(ConnectionToServerState.DECLINED, connectionToServer.getConnectionState());
|
|
assertTrue(connectionToServer.getJoinAcceptance() != null);
|
|
assertEquals(sourceID, connectionToServer.getJoinAcceptance().getSourceID());
|
|
assertEquals(JoinAcceptanceEnum.SERVER_FULL, connectionToServer.getJoinAcceptance().getAcceptanceType());
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|