Files
ferretirc-client/src/test/kotlin/com/shr4pnel/ferretirc/IrcClientTest.kt
T

79 lines
2.7 KiB
Kotlin

package com.shr4pnel.ferretirc
import com.shr4pnel.ferretirc.net.messages.ServerMessage
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull
import com.shr4pnel.ferretirc.net.messages.ClientMessage as Message
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
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.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
class IrcClientTest {
companion object {
lateinit var processes: MutableList<Process>
val client = IrcClient("localhost", 6667)
private val logger = KotlinLogging.logger("IrcClientTest")
@JvmStatic
@BeforeAll
fun setup() {
val logFile = File("src/test/resources/logs/ngircd.log")
val ngircdConfigPath = javaClass.classLoader.getResource("ngircd.conf")!!.path
logFile.createNewFile()
processes = ProcessBuilder.startPipeline(
listOf<ProcessBuilder>(
// Run IRC server in [-d] debug log mode, with [-n] no daemon, using config [-f] file at path
ProcessBuilder("ngircd", "-dnf", ngircdConfigPath).redirectErrorStream(true),
// Remove unhelpful default ngircd info [PID. relative time, GID?]
ProcessBuilder("sed", "-ue", "s/\\[[^][]*\\]//g"),
// Add relative timestamp
ProcessBuilder("ts", "-s", "%H:%M:%.S").redirectOutput(logFile)
)
)
client.scope.launch {
client.connect()
client.register("shr4p", "password")
}
runBlocking {
delay(0.5.seconds)
}
}
@JvmStatic
@AfterAll
fun shutdown() = processes.forEach { it.destroy() }
}
@Test
fun sample() {
client.scope.launch {
}
}
@Test
fun pingGetsPong() = runBlocking {
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")
assertEquals(token, pong.token, "Token in PING did not match PONG")
}
}