diff --git a/mccoroutine-minestom-core/src/test/java/helper/MockedMinestomServer.kt b/mccoroutine-minestom-core/src/test/java/helper/MockedMinestomServer.kt deleted file mode 100644 index 8dfd84dc..00000000 --- a/mccoroutine-minestom-core/src/test/java/helper/MockedMinestomServer.kt +++ /dev/null @@ -1,41 +0,0 @@ -package helper - -import net.minestom.server.MinecraftServer -import net.minestom.server.extensions.Extension -import org.slf4j.Logger - -class MockedMinestomServer { - fun boot(mlogger: Logger? = null): Extension { - val extension = MockedExtension(mlogger) - MinecraftServer.init() - Thread { - while (true) { - MinecraftServer.getSchedulerManager().processTick() - Thread.sleep(50) - } - }.start() - return extension - } - - class MockedExtension(private val logger: Logger?) : Extension() { - override fun initialize() { - } - - override fun terminate() { - } - - - /** - * Gets the logger for the extension - * - * @return The logger for the extension - */ - override fun getLogger(): Logger { - if (logger != null) { - return logger - } - - return super.getLogger() - } - } -} diff --git a/mccoroutine-minestom-core/src/test/java/integrationtest/MinestomCommandTest.kt b/mccoroutine-minestom-core/src/test/java/integrationtest/MinestomCommandTest.kt deleted file mode 100644 index 9385fec8..00000000 --- a/mccoroutine-minestom-core/src/test/java/integrationtest/MinestomCommandTest.kt +++ /dev/null @@ -1,62 +0,0 @@ -package integrationtest - -import com.github.shynixn.mccoroutine.minestom.setSuspendingDefaultExecutor -import helper.MockedMinestomServer -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withContext -import net.minestom.server.MinecraftServer -import net.minestom.server.command.builder.Command -import net.minestom.server.extensions.Extension -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Test - -class MinestomCommandTest { - /** - * Given - * a call of a simple suspending command - * When - * executeServerCommand is called from any context - * Then - * the command should be called on the correct threads. - */ - @Test - fun dispatchCommand_SimpleSuspendingCommandExecutor_ShouldCallOnCorrectThreads() { - val server = MockedMinestomServer() - val extension = server.boot() - var unitTestThreadId: Long - val testCommandExecutor = TestCommandExecutor(extension) - - runBlocking { - unitTestThreadId = Thread.currentThread().id - MinecraftServer.getCommandManager().register(testCommandExecutor) - MinecraftServer.getCommandManager().executeServerCommand("unittest") - } - - Thread.sleep(2000) - - Assertions.assertNotEquals(unitTestThreadId, testCommandExecutor.callThreadId) - Assertions.assertNotEquals(unitTestThreadId, testCommandExecutor.asyncThreadId) - Assertions.assertNotEquals(unitTestThreadId, testCommandExecutor.leaveThreadId) - } - - private class TestCommandExecutor(private val extension: Extension) : Command("unittest") { - var callThreadId = 0L - var asyncThreadId = 0L - var leaveThreadId = 0L - - init { - this.setSuspendingDefaultExecutor(extension) { sender, context -> - callThreadId = Thread.currentThread().id - - withContext(Dispatchers.IO) { - asyncThreadId = Thread.currentThread().id - Thread.sleep(50) - } - - leaveThreadId = Thread.currentThread().id - } - - } - } -} diff --git a/mccoroutine-minestom-core/src/test/java/integrationtest/MinestomEventTest.kt b/mccoroutine-minestom-core/src/test/java/integrationtest/MinestomEventTest.kt deleted file mode 100644 index 1301d7cc..00000000 --- a/mccoroutine-minestom-core/src/test/java/integrationtest/MinestomEventTest.kt +++ /dev/null @@ -1,100 +0,0 @@ -package integrationtest - -import com.github.shynixn.mccoroutine.minestom.addSuspendingListener -import helper.MockedMinestomServer -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withContext -import net.minestom.server.MinecraftServer -import net.minestom.server.entity.Player -import net.minestom.server.event.player.PlayerDisconnectEvent -import net.minestom.server.event.player.PlayerLoginEvent -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Test -import org.mockito.Mockito - -class MinestomEventTest { - /** - * Given a test listener - * When the test listener is register and join event is called - * then the join event should be called on the correct thread. - */ - @Test - fun registerSuspendListener_PlayerJoinEvent_ShouldCallEventWithCorrectThread() { - // Arrange - val server = MockedMinestomServer() - val extension = server.boot() - val testListener = TestListener() - var unitTestThreadId: Long - - // Act - runBlocking { - unitTestThreadId = Thread.currentThread().id - MinecraftServer.getGlobalEventHandler() - .addSuspendingListener(extension, PlayerLoginEvent::class.java) { e -> - testListener.onPlayerJoinEvent(e) - } - MinecraftServer.getGlobalEventHandler().call(PlayerLoginEvent(Mockito.mock(Player::class.java))) - } - - Thread.sleep(500) - - // Assert - Assertions.assertNotEquals(unitTestThreadId, testListener.joinEventCalledId) - Assertions.assertNotEquals(unitTestThreadId, testListener.asyncChatEventCalledId) - Assertions.assertNotEquals(unitTestThreadId, testListener.leaveThreadId) - Assertions.assertNotEquals(testListener.asyncChatEventCalledId, testListener.leaveThreadId) - } - - /** - * Given a test listener - * When the test listener is register and quit event is called - * then the quit event should be called on the correct thread. - */ - @Test - fun registerSuspendListener_PlayerQuitEvent_ShouldCallEventWithCorrectThread() { - // Arrange - val server = MockedMinestomServer() - val extension = server.boot() - val testListener = TestListener() - var unitTestThreadId: Long - - // Act - runBlocking { - unitTestThreadId = Thread.currentThread().id - MinecraftServer.getGlobalEventHandler() - .addSuspendingListener(extension, PlayerDisconnectEvent::class.java) { e -> - testListener.onPlayerQuitEvent(e) - } - MinecraftServer.getGlobalEventHandler().call(PlayerLoginEvent(Mockito.mock(Player::class.java))) - } - - Thread.sleep(500) - - // Assert - Assertions.assertNotEquals(unitTestThreadId, testListener.quitEventCalledId) - } - - class TestListener( - var joinEventCalledId: Long = 0L, - var quitEventCalledId: Long = 0L, - var asyncChatEventCalledId: Long = 0L, - var leaveThreadId: Long = 0L - ) { - - suspend fun onPlayerJoinEvent(event: PlayerLoginEvent) { - joinEventCalledId = Thread.currentThread().id - - withContext(Dispatchers.IO) { - Thread.sleep(100) - asyncChatEventCalledId = Thread.currentThread().id - } - - leaveThreadId = Thread.currentThread().id - } - - fun onPlayerQuitEvent(event: PlayerDisconnectEvent) { - quitEventCalledId = Thread.currentThread().id - } - } -} diff --git a/mccoroutine-minestom-core/src/test/java/integrationtest/MinestomExceptionTest.kt b/mccoroutine-minestom-core/src/test/java/integrationtest/MinestomExceptionTest.kt deleted file mode 100644 index 31b70c78..00000000 --- a/mccoroutine-minestom-core/src/test/java/integrationtest/MinestomExceptionTest.kt +++ /dev/null @@ -1,58 +0,0 @@ -package integrationtest - -import com.github.shynixn.mccoroutine.minestom.launch -import helper.MockedMinestomServer -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.runBlocking -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Test -import org.mockito.Mockito -import org.slf4j.Logger - -class MinestomExceptionTest { - /** - * Given - * multiple extension.launch() operations - * When - * 2 launches fail and one is successful - * The - * extension scope should not fail and the 2 failures be logged. - */ - @Test - fun extensionLaunch_MultipleFailingCoroutineScopes_ShouldBeCaughtInRootScopeAndKeepExtensionScopeRunning() { - // Arrange - val testServer = MockedMinestomServer() - val logger = Mockito.mock(Logger::class.java) - var logMessageCounter = 0 - Mockito.`when`( - logger.error(Mockito.anyString(), Mockito.any()) - ).thenAnswer { - logMessageCounter++ - } - val extension = testServer.boot(logger) - var actualThreadId = 0L - var ioThreadId: Long - - // Act - runBlocking(Dispatchers.IO) { - ioThreadId = Thread.currentThread().id - - extension.launch { - throw IllegalArgumentException("UnitTestFailure!") - } - - extension.launch { - throw IllegalArgumentException("Another UnitTestFailure!") - } - - extension.launch { - actualThreadId = Thread.currentThread().id - } - } - Thread.sleep(2000) - - // Assert - Assertions.assertNotEquals(ioThreadId, actualThreadId) - Assertions.assertEquals(2, logMessageCounter) - } -}