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.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum; import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum;
import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage; 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.lobby.Lobby;
import uulm.teamname.marvelous.server.netconnector.UserManager;
import java.lang.reflect.InvocationTargetException;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -39,9 +42,10 @@ class LobbyManagerTest {
LobbyRunner lobbyRunner; LobbyRunner lobbyRunner;
MockedStatic<LobbyRunner> lobbyRunnerMockedStatic; MockedStatic<LobbyRunner> lobbyRunnerMockedStatic;
MockedStatic<Server> serverMockedStatic;
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
onMessageCallback = mock(BiConsumer.class); onMessageCallback = mock(BiConsumer.class);
onErrorCallback = mock(BiConsumer.class); onErrorCallback = mock(BiConsumer.class);
@ -75,9 +79,14 @@ class LobbyManagerTest {
ParticipantType.PlayerOne, ParticipantType.PlayerOne,
"AwesomestAwesomePlayer"); "AwesomestAwesomePlayer");
lobbyRunner = spy(LobbyRunner.getInstance()); var constructor = LobbyRunner.class.getDeclaredConstructor();
constructor.setAccessible(true);
lobbyRunner = spy(constructor.newInstance());
doNothing().when(lobbyRunner).startLobby(any(LobbyConnection.class)); doNothing().when(lobbyRunner).startLobby(any(LobbyConnection.class));
serverMockedStatic = Mockito.mockStatic(Server.class);
serverMockedStatic.when(Server::getMaxLobbies).thenReturn(2);
lobbyRunnerMockedStatic = Mockito.mockStatic(LobbyRunner.class); lobbyRunnerMockedStatic = Mockito.mockStatic(LobbyRunner.class);
lobbyRunnerMockedStatic.when(LobbyRunner::getInstance).thenReturn(lobbyRunner); lobbyRunnerMockedStatic.when(LobbyRunner::getInstance).thenReturn(lobbyRunner);
} }
@ -85,6 +94,7 @@ class LobbyManagerTest {
@AfterEach @AfterEach
void afterEach() { void afterEach() {
lobbyRunnerMockedStatic.close(); lobbyRunnerMockedStatic.close();
serverMockedStatic.close();
} }
@Test @Test
@ -273,13 +283,14 @@ class LobbyManagerTest {
Participant spectator1 = new Participant(spectator, ParticipantType.Spectator, "spectator1"); Participant spectator1 = new Participant(spectator, ParticipantType.Spectator, "spectator1");
manager.assignLobbyToConnection(player1, "playerOne", playerReady); manager.assignLobbyToConnection(player1, "playerOne", playerReady);
verify(lobbyRunner, never()).startLobby(any(LobbyConnection.class));
manager.assignLobbyToConnection(player2, "playerTwo", playerReady); manager.assignLobbyToConnection(player2, "playerTwo", playerReady);
try { verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class));
Thread.sleep(100); when(lobbyRunner.isStarted(any(LobbyConnection.class))).thenReturn(true);
} catch (InterruptedException e) {
e.printStackTrace();
}
manager.assignLobbyToConnection(spectator, "spectator1", spectatorReady); manager.assignLobbyToConnection(spectator, "spectator1", spectatorReady);
verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class));
// manager.assignLobbyToConnection(spectator, "spectator2", spectatorReady); // manager.assignLobbyToConnection(spectator, "spectator2", spectatorReady);
assertThat(manager.getResourceDescriptorToLobby()).hasSize(1); assertThat(manager.getResourceDescriptorToLobby()).hasSize(1);
@ -302,7 +313,5 @@ class LobbyManagerTest {
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID)) 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(player2Participant).gameID))
.isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(spectator1).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.Mockito;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.server.Server; import uulm.teamname.marvelous.server.Server;
import java.lang.reflect.InvocationTargetException; 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 // It seems very impractical indeed to properly to check if another thread executed this, so I won't
@Test @Test
void lobbyGetsStartedTest() { void lobbyGetsStartedTest() throws InterruptedException {
lobbyRunner.startLobby(connection); lobbyRunner.startLobby(connection);
Thread.sleep(10);
verify(connection).run(); verify(connection).run();
} }
@Test @Test
void canAddLobbyTest() { void canAddLobbyTest() throws InterruptedException {
assertThat(lobbyRunner.canAddLobby()).isTrue(); assertThat(lobbyRunner.canAddLobby()).isTrue();
lobbyRunner.startLobby(connection); lobbyRunner.startLobby(connection);
assertThat(lobbyRunner.canAddLobby()).isFalse(); assertThat(lobbyRunner.canAddLobby()).isFalse();
@ -66,11 +68,20 @@ class LobbyRunnerTest {
@Test @Test
void lobbyIsStartedTest() { void lobbyIsStartedTest() {
connection = spy(new LobbyConnection("SomeGameID", null, null));
doNothing().when(connection).run();
assertThat(lobbyRunner.isStarted(connection)).isFalse(); assertThat(lobbyRunner.isStarted(connection)).isFalse();
lobbyRunner.startLobby(connection); lobbyRunner.startLobby(connection);
assertThat(lobbyRunner.isStarted(connection)).isTrue(); 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();
} }
} }