75 lines
2.4 KiB
Kotlin
75 lines
2.4 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")
|
|
logFile.createNewFile()
|
|
processes = ProcessBuilder.startPipeline(
|
|
listOf<ProcessBuilder>(
|
|
ProcessBuilder("ngircd", "-dn").redirectErrorStream(true),
|
|
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
|
|
@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")
|
|
}
|
|
}
|