Add pingGetsPong Test
This commit is contained in:
@@ -1,52 +1,74 @@
|
||||
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 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.BeforeAll
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
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 {
|
||||
companion object {
|
||||
lateinit var proc: Process
|
||||
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()
|
||||
proc = ProcessBuilder("ngircd", "-nd")
|
||||
.redirectOutput(logFile)
|
||||
.redirectErrorStream(true)
|
||||
.start()
|
||||
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() {
|
||||
proc.destroy()
|
||||
}
|
||||
fun shutdown() = processes.forEach { it.destroy() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sample() {
|
||||
val client = IrcClient("shrpnl", URI("http://localhost:6667").toURL())
|
||||
client.scope.launch {
|
||||
client.connect()
|
||||
client.queueMessage(Msg.Cap.LS(302))
|
||||
delay(1000)
|
||||
client.scope.launch {
|
||||
for (msg in client.messages)
|
||||
println(msg)
|
||||
}
|
||||
delay(1000)
|
||||
client.close()
|
||||
}
|
||||
runBlocking { delay(1500) }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user