test: created tests for LobbyManager and LobbyRunner

This commit is contained in:
Yannik Bretschneider 2021-06-06 21:35:36 +02:00
parent 5f89e4e90a
commit 3ab8d57829
2 changed files with 31 additions and 11 deletions

View File

@ -11,8 +11,11 @@ import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum;
import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage;
import uulm.teamname.marvelous.server.Server;
import uulm.teamname.marvelous.server.lobby.Lobby;
import uulm.teamname.marvelous.server.netconnector.UserManager;
import java.lang.reflect.InvocationTargetException;
import java.util.function.BiConsumer;
import static org.mockito.Mockito.*;
@ -39,9 +42,10 @@ class LobbyManagerTest {
LobbyRunner lobbyRunner;
MockedStatic<LobbyRunner> lobbyRunnerMockedStatic;
MockedStatic<Server> serverMockedStatic;
@BeforeEach
void beforeEach() {
void beforeEach() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
onMessageCallback = mock(BiConsumer.class);
onErrorCallback = mock(BiConsumer.class);
@ -75,9 +79,14 @@ class LobbyManagerTest {
ParticipantType.PlayerOne,
"AwesomestAwesomePlayer");
lobbyRunner = spy(LobbyRunner.getInstance());
var constructor = LobbyRunner.class.getDeclaredConstructor();
constructor.setAccessible(true);
lobbyRunner = spy(constructor.newInstance());
doNothing().when(lobbyRunner).startLobby(any(LobbyConnection.class));
serverMockedStatic = Mockito.mockStatic(Server.class);
serverMockedStatic.when(Server::getMaxLobbies).thenReturn(2);
lobbyRunnerMockedStatic = Mockito.mockStatic(LobbyRunner.class);
lobbyRunnerMockedStatic.when(LobbyRunner::getInstance).thenReturn(lobbyRunner);
}
@ -85,6 +94,7 @@ class LobbyManagerTest {
@AfterEach
void afterEach() {
lobbyRunnerMockedStatic.close();
serverMockedStatic.close();
}
@Test
@ -273,13 +283,14 @@ class LobbyManagerTest {
Participant spectator1 = new Participant(spectator, ParticipantType.Spectator, "spectator1");
manager.assignLobbyToConnection(player1, "playerOne", playerReady);
verify(lobbyRunner, never()).startLobby(any(LobbyConnection.class));
manager.assignLobbyToConnection(player2, "playerTwo", playerReady);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class));
when(lobbyRunner.isStarted(any(LobbyConnection.class))).thenReturn(true);
manager.assignLobbyToConnection(spectator, "spectator1", spectatorReady);
verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class));
// manager.assignLobbyToConnection(spectator, "spectator2", spectatorReady);
assertThat(manager.getResourceDescriptorToLobby()).hasSize(1);
@ -302,7 +313,5 @@ class LobbyManagerTest {
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID))
.isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player2Participant).gameID))
.isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(spectator1).gameID));
verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class));
}
}

View File

@ -8,6 +8,7 @@ import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.server.Server;
import java.lang.reflect.InvocationTargetException;
@ -47,13 +48,14 @@ class LobbyRunnerTest {
// It seems very impractical indeed to properly to check if another thread executed this, so I won't
@Test
void lobbyGetsStartedTest() {
void lobbyGetsStartedTest() throws InterruptedException {
lobbyRunner.startLobby(connection);
Thread.sleep(10);
verify(connection).run();
}
@Test
void canAddLobbyTest() {
void canAddLobbyTest() throws InterruptedException {
assertThat(lobbyRunner.canAddLobby()).isTrue();
lobbyRunner.startLobby(connection);
assertThat(lobbyRunner.canAddLobby()).isFalse();
@ -66,11 +68,20 @@ class LobbyRunnerTest {
@Test
void lobbyIsStartedTest() {
connection = spy(new LobbyConnection("SomeGameID", null, null));
doNothing().when(connection).run();
assertThat(lobbyRunner.isStarted(connection)).isFalse();
lobbyRunner.startLobby(connection);
assertThat(lobbyRunner.isStarted(connection)).isTrue();
connection.addSpectator(new Participant(null, ParticipantType.Spectator, "Hi"));
connection.addSpectator(new Participant(null, ParticipantType.Spectator, "Hi2"));
connection.removeParticipant(new Participant(null, ParticipantType.Spectator, "Hi"));
assertThat(lobbyRunner.isStarted(connection)).isTrue();
lobbyRunner.removeLobby(connection);
assertThat(lobbyRunner.isStarted(connection)).isFalse();
}
}