Add pingGetsPong Test

This commit is contained in:
2026-08-12 13:28:59 +01:00
parent b35bb5b679
commit 4fd934c7c9
@@ -1,52 +1,74 @@
package com.shr4pnel.ferretirc package com.shr4pnel.ferretirc
import com.shr4pnel.ferretirc.net.messages.ServerMessage
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import com.shr4pnel.ferretirc.net.ClientMessage as Msg 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.AfterAll
import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import java.io.File import java.io.File
import java.lang.ProcessBuilder import java.lang.ProcessBuilder
import java.net.URI 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 { class IrcClientTest {
companion object { companion object {
lateinit var proc: Process lateinit var processes: MutableList<Process>
val client = IrcClient("localhost", 6667)
private val logger = KotlinLogging.logger("IrcClientTest")
@JvmStatic @JvmStatic
@BeforeAll @BeforeAll
fun setup() { fun setup() {
val logFile = File("src/test/resources/logs/ngircd.log") val logFile = File("src/test/resources/logs/ngircd.log")
logFile.createNewFile() logFile.createNewFile()
proc = ProcessBuilder("ngircd", "-nd") processes = ProcessBuilder.startPipeline(
.redirectOutput(logFile) listOf<ProcessBuilder>(
.redirectErrorStream(true) ProcessBuilder("ngircd", "-dn").redirectErrorStream(true),
.start() ProcessBuilder("ts", "(%H:%M:%S)").redirectErrorStream(true),
ProcessBuilder("sed", "-ue", "s/\\[[^][]*\\]//g").redirectOutput(logFile)
)
)
client.scope.launch {
client.connect()
client.register("shr4p", "password")
}
runBlocking {
delay(0.5.seconds)
}
} }
@JvmStatic @JvmStatic
@AfterAll @AfterAll
fun shutdown() { fun shutdown() = processes.forEach { it.destroy() }
proc.destroy()
}
} }
@Test @Test
fun sample() { fun sample() {
val client = IrcClient("shrpnl", URI("http://localhost:6667").toURL())
client.scope.launch { client.scope.launch {
client.connect()
client.queueMessage(Msg.Cap.LS(302)) }
delay(1000) }
client.scope.launch {
for (msg in client.messages) @Test
println(msg) fun pingGetsPong() = runBlocking {
} val token = "ACK"
delay(1000) logger.info { "Sending PING $token" }
client.close() client.queueMessage(Message.Ping(token))
}
runBlocking { delay(1500) } 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")
} }
} }