added method to import errors into client interface KiloClient.Builder#addErrors
This commit is contained in:
parent
ae624ee051
commit
38d800c7ac
@ -112,6 +112,10 @@ class KiloClient<S>(
|
||||
interfaceBuilder = f
|
||||
}
|
||||
|
||||
fun addErrors(from: LocalInterface<*>) {
|
||||
errorProviders += from
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new session object, otherwise Unit session will be used
|
||||
*/
|
||||
@ -123,8 +127,11 @@ class KiloClient<S>(
|
||||
connectionBuilder = f
|
||||
}
|
||||
|
||||
val errorProviders = mutableListOf<LocalInterface<*>>()
|
||||
|
||||
internal fun build(): KiloClient<S> {
|
||||
val i = KiloInterface<S>()
|
||||
for(ep in errorProviders) i.addErrorProvider(ep)
|
||||
interfaceBuilder?.let { i.it() }
|
||||
val connector = connectionBuilder ?: throw IllegalArgumentException("connect handler was not set")
|
||||
return KiloClient(i,secretIdKey) {
|
||||
|
@ -11,7 +11,7 @@ private typealias RawCommandHandler<C> = suspend (C, UByteArray) -> UByteArray
|
||||
|
||||
private val idCounter = AtomicCounter()
|
||||
|
||||
open class LocalInterface<S>: Loggable by LogTag("LocalInterface${idCounter.incrementAndGet()}") {
|
||||
open class LocalInterface<S> : Loggable by LogTag("LocalInterface${idCounter.incrementAndGet()}") {
|
||||
|
||||
private val commands = mutableMapOf<String, RawCommandHandler<S>>()
|
||||
|
||||
|
@ -13,10 +13,13 @@ import net.sergeych.mp_logger.Log
|
||||
import java.net.InetAddress
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertIs
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ClientTest {
|
||||
|
||||
class TestException : Exception("test1")
|
||||
|
||||
@Test
|
||||
fun testAddresses() {
|
||||
println(InetAddress.getLocalHost())
|
||||
@ -34,16 +37,30 @@ class ClientTest {
|
||||
|
||||
val cmdSave by command<String,Unit>()
|
||||
val cmdLoad by command<Unit,String>()
|
||||
val cmdDrop by command<Unit,Unit>()
|
||||
val cmdException by command<Unit,Unit>()
|
||||
|
||||
val cli = KiloInterface<Session>().apply {
|
||||
registerError { TestException() }
|
||||
onConnected { session.data = "start" }
|
||||
on(cmdSave) { session.data = it }
|
||||
on(cmdLoad) {
|
||||
println("load!")
|
||||
session.data }
|
||||
session.data
|
||||
}
|
||||
on(cmdException) {
|
||||
throw TestException()
|
||||
}
|
||||
on(cmdDrop) {
|
||||
throw RemoteInterface.ClosedException()
|
||||
}
|
||||
}
|
||||
val server = KiloServer(cli, acceptTcpDevice(17101)) { Session("unknown")}
|
||||
val client = KiloClient<Unit> {
|
||||
val server = KiloServer(cli, acceptTcpDevice(17101)) {
|
||||
Session("unknown")
|
||||
}
|
||||
|
||||
val client = KiloClient<Unit>() {
|
||||
addErrors(cli)
|
||||
connect { connectTcpDevice("localhost:17101") }
|
||||
}
|
||||
println(client.call(cmdLoad))
|
||||
@ -51,6 +68,13 @@ class ClientTest {
|
||||
assertEquals("start", client.call(cmdLoad))
|
||||
client.call(cmdSave, "foobar")
|
||||
assertEquals("foobar", client.call(cmdLoad))
|
||||
|
||||
// client.call(cmdException)
|
||||
val res = kotlin.runCatching { client.call(cmdException) }
|
||||
println(res.exceptionOrNull())
|
||||
assertIs<TestException>(res.exceptionOrNull())
|
||||
assertEquals("foobar", client.call(cmdLoad))
|
||||
|
||||
server.close()
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user