proto-socket/kotlin/src/test/kotlin/com/tokilabs/toki_socket/TestHelpers.kt

33 lines
785 B
Kotlin

package com.tokilabs.toki_socket
import com.tokilabs.toki_socket.packets.TestData
import java.net.ServerSocket
import kotlin.test.fail
fun testParserMap(): ParserMap =
mapOf(
typeNameOf<TestData>() to { TestData.parseFrom(it) },
)
fun testData(): TestData =
TestData.newBuilder()
.setIndex(1)
.setMessage("ping")
.build()
fun freePort(): Int =
ServerSocket(0).use { it.localPort }
suspend fun waitForCondition(
timeoutMs: Long = 2_000L,
intervalMs: Long = 10L,
message: String,
predicate: () -> Boolean,
) {
val deadline = System.nanoTime() + timeoutMs * 1_000_000L
while (System.nanoTime() < deadline) {
if (predicate()) return
kotlinx.coroutines.delay(intervalMs)
}
fail(message)
}