kotlinx-coroutines-test, runTest replaces runBlocking
This commit is contained in:
@@ -2,7 +2,6 @@ package com.shr4pnel.ferretirc
|
||||
|
||||
import com.shr4pnel.ferretirc.net.messages.ServerMessage
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.AfterAll
|
||||
@@ -11,8 +10,6 @@ import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
import java.lang.ProcessBuilder
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import com.shr4pnel.ferretirc.net.messages.ClientMessage as Message
|
||||
|
||||
class IrcClientTest {
|
||||
@@ -22,10 +19,11 @@ class IrcClientTest {
|
||||
lateinit var processes: MutableList<Process>
|
||||
val client = IrcClient("localhost", 6667, enableLogging = true)
|
||||
private val logger = KotlinLogging.logger {}
|
||||
val guestUser = IrcClient("localhost", 6667)
|
||||
|
||||
@JvmStatic
|
||||
@BeforeAll
|
||||
fun setup() =
|
||||
fun setup(): Unit =
|
||||
runBlocking {
|
||||
val logFile = File("src/test/resources/logs/ngircd.log")
|
||||
val ngircdConfigPath = javaClass.classLoader.getResource("ngircd.conf")!!.path
|
||||
@@ -59,38 +57,20 @@ class IrcClientTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sample() {
|
||||
client.scope.launch {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun pingGetsPong() =
|
||||
runBlocking {
|
||||
runTest {
|
||||
val token = "ACK"
|
||||
logger.info { "Sending PING $token" }
|
||||
client.queueMessage(Message.Ping(token))
|
||||
|
||||
logger.info { "Waiting to receive PONG" }
|
||||
val pong =
|
||||
withTimeoutOrNull(1.seconds) {
|
||||
client.waitForNext<ServerMessage.Pong> { it.token.equals(token) }
|
||||
}
|
||||
assertNotNull(pong, "Reached timeout while waiting for PONG")
|
||||
val msg = Message.Ping(token)
|
||||
val pong = client.queueAndWaitForNext<ServerMessage.Pong>(msg)
|
||||
assertEquals(token, pong.token, "Token in PING did not match PONG")
|
||||
logger.info { "Received PING" }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun oper() =
|
||||
runBlocking {
|
||||
runTest {
|
||||
client.queueMessage(Message.Oper("shr4p", "password"))
|
||||
val mode =
|
||||
withTimeoutOrNull(1.seconds) {
|
||||
client.waitForNext<ServerMessage.Mode>()
|
||||
}
|
||||
assertNotNull(mode, "Timed out waiting for OPER MODE response")
|
||||
val mode = client.waitForNext<ServerMessage.Mode>()
|
||||
assertEquals("shr4p", mode.operatorName, "Received incorrect operator name in MODE")
|
||||
assertEquals("+o", mode.pMask, "Received unexpected mask in MODE")
|
||||
logger.info { "Received MODE with mask ${mode.pMask}" }
|
||||
@@ -98,7 +78,7 @@ class IrcClientTest {
|
||||
|
||||
@Test
|
||||
fun listChannels() =
|
||||
runBlocking {
|
||||
runTest {
|
||||
val channels = client.server.fetchChannels()
|
||||
assert(channels.isNotEmpty())
|
||||
client.queueMessage(Message.Join(channels.first()))
|
||||
@@ -114,6 +94,5 @@ class IrcClientTest {
|
||||
logger.debug { "Message: $message, Received: $receivedMessage" }
|
||||
assertEquals(message.message, receivedMessage.message)
|
||||
assertEquals(message.target.chanName, receivedMessage.targets.first())
|
||||
guestUser.close()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user