From 38d800c7acfa13e613b7543170385a5854bfd767 Mon Sep 17 00:00:00 2001 From: sergeych Date: Mon, 17 Jun 2024 10:44:12 +0700 Subject: [PATCH] added method to import errors into client interface KiloClient.Builder#addErrors --- .../net/sergeych/kiloparsec/KiloClient.kt | 7 +++++ .../net/sergeych/kiloparsec/LocalInterface.kt | 2 +- .../net/sergeych/kiloparsec/ClientTest.kt | 30 +++++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloClient.kt b/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloClient.kt index 680734f..0b31cad 100644 --- a/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloClient.kt +++ b/src/commonMain/kotlin/net/sergeych/kiloparsec/KiloClient.kt @@ -112,6 +112,10 @@ class KiloClient( 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( connectionBuilder = f } + val errorProviders = mutableListOf>() + internal fun build(): KiloClient { val i = KiloInterface() + 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) { diff --git a/src/commonMain/kotlin/net/sergeych/kiloparsec/LocalInterface.kt b/src/commonMain/kotlin/net/sergeych/kiloparsec/LocalInterface.kt index cc15c8e..3ca9ddf 100644 --- a/src/commonMain/kotlin/net/sergeych/kiloparsec/LocalInterface.kt +++ b/src/commonMain/kotlin/net/sergeych/kiloparsec/LocalInterface.kt @@ -11,7 +11,7 @@ private typealias RawCommandHandler = suspend (C, UByteArray) -> UByteArray private val idCounter = AtomicCounter() -open class LocalInterface: Loggable by LogTag("LocalInterface${idCounter.incrementAndGet()}") { +open class LocalInterface : Loggable by LogTag("LocalInterface${idCounter.incrementAndGet()}") { private val commands = mutableMapOf>() diff --git a/src/jvmTest/kotlin/net/sergeych/kiloparsec/ClientTest.kt b/src/jvmTest/kotlin/net/sergeych/kiloparsec/ClientTest.kt index 0362286..f0cc812 100644 --- a/src/jvmTest/kotlin/net/sergeych/kiloparsec/ClientTest.kt +++ b/src/jvmTest/kotlin/net/sergeych/kiloparsec/ClientTest.kt @@ -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() val cmdLoad by command() + val cmdDrop by command() + val cmdException by command() val cli = KiloInterface().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 { + val server = KiloServer(cli, acceptTcpDevice(17101)) { + Session("unknown") + } + + val client = KiloClient() { + 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(res.exceptionOrNull()) + assertEquals("foobar", client.call(cmdLoad)) + server.close() }