add tests and make several replacements/improvements

This commit is contained in:
InsanusMokrassar 2022-12-06 12:38:24 +06:00
parent b17931e7bd
commit 02dbd493c2
11 changed files with 135 additions and 59 deletions

View File

@ -1,6 +1,7 @@
plugins { plugins {
id "org.jetbrains.kotlin.multiplatform" id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization" id "org.jetbrains.kotlin.plugin.serialization"
id "application"
} }
apply from: "$mppJavaProjectPresetPath" apply from: "$mppJavaProjectPresetPath"
@ -14,3 +15,12 @@ kotlin {
} }
} }
} }
application {
mainClassName = "dev.inmo.micro_utils.startup.launcher.ServerLauncherKt"
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

View File

@ -0,0 +1,13 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.i
import dev.inmo.kslog.common.logger
import dev.inmo.micro_utils.startup.plugin.ServerPlugin
import org.koin.core.Koin
object HelloWorldPlugin : ServerPlugin {
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
logger.i("Hello world")
}
}

View File

@ -0,0 +1,24 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.i
import kotlinx.serialization.json.JsonObject
import org.koin.core.KoinApplication
import org.koin.core.context.GlobalContext
import org.koin.dsl.module
suspend fun start(rawConfig: JsonObject) {
with(StartupLauncherPlugin) {
logger.i("Start initialization")
val koinApp = KoinApplication.init()
koinApp.modules(
module {
setupDI(rawConfig)
}
)
logger.i("Modules loaded")
GlobalContext.startKoin(koinApp)
logger.i("Koin started")
startPlugin(koinApp.koin)
logger.i("App has been setup")
}
}

View File

@ -14,7 +14,7 @@ import org.koin.core.Koin
import org.koin.core.module.Module import org.koin.core.module.Module
import org.koin.dsl.module import org.koin.dsl.module
object StartupLauncher : ServerPlugin { object StartupLauncherPlugin : ServerPlugin {
internal val logger = taggedLogger(this) internal val logger = taggedLogger(this)
override fun Module.setupDI(config: JsonObject) { override fun Module.setupDI(config: JsonObject) {
val pluginsConfig = defaultJson.decodeFromJsonElement(Config.serializer(), config) val pluginsConfig = defaultJson.decodeFromJsonElement(Config.serializer(), config)

View File

@ -0,0 +1,18 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.KSLog
import dev.inmo.kslog.common.i
import kotlinx.serialization.json.jsonObject
import java.io.File
suspend fun main(args: Array<String>) {
KSLog.default = KSLog("ServerLauncher")
val (configPath) = args
val file = File(configPath)
KSLog.i("Start read config from ${file.absolutePath}")
val json = defaultJson.parseToJsonElement(file.readText()).jsonObject
KSLog.i("Config has been read")
start(json)
}

View File

@ -1,36 +0,0 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.KSLog
import dev.inmo.kslog.common.i
import dev.inmo.micro_utils.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.serialization.json.jsonObject
import org.koin.core.KoinApplication
import org.koin.core.context.GlobalContext
import org.koin.dsl.module
import java.io.File
suspend fun main(args: Array<String>) {
KSLog.default = KSLog("ServerLauncher")
val (configPath) = args
val file = File(configPath)
KSLog.i("Start read config from ${file.absolutePath}")
val json = defaultJson.parseToJsonElement(file.readText()).jsonObject
KSLog.i("Config has been read")
with(StartupLauncher) {
logger.i("Start initialization")
val koinApp = KoinApplication.init()
koinApp.modules(
module {
setupDI(json)
}
)
logger.i("Modules loaded")
GlobalContext.startKoin(koinApp)
logger.i("Koin started")
startPlugin(koinApp.koin)
logger.i("Behaviour builder has been setup")
}
}

View File

@ -0,0 +1,39 @@
import dev.inmo.micro_utils.coroutines.launchSynchronously
import dev.inmo.micro_utils.startup.launcher.Config
import dev.inmo.micro_utils.startup.launcher.HelloWorldPlugin
import dev.inmo.micro_utils.startup.launcher.defaultJson
import dev.inmo.micro_utils.startup.launcher.start
import kotlinx.coroutines.launch
import kotlinx.serialization.json.jsonObject
import kotlin.test.Test
class StartupLaunchingTests {
@Test(timeout = 1000L)
fun CheckThatEmptyPluginsListLeadsToEndOfMain() {
val emptyJson = defaultJson.encodeToJsonElement(
Config.serializer(),
Config(emptyList())
).jsonObject
launchSynchronously {
val job = launch {
start(emptyJson)
}
job.join()
}
}
@Test(timeout = 1000L)
fun CheckThatHelloWorldPluginsListLeadsToEndOfMain() {
val emptyJson = defaultJson.encodeToJsonElement(
Config.serializer(),
Config(listOf(HelloWorldPlugin))
).jsonObject
launchSynchronously {
val job = launch {
start(emptyJson)
}
job.join()
}
}
}

View File

@ -12,6 +12,7 @@ kotlin {
api libs.koin api libs.koin
api libs.kt.serialization api libs.kt.serialization
api libs.kslog api libs.kslog
api libs.kt.reflect
api project(":micro_utils.coroutines") api project(":micro_utils.coroutines")
} }
} }

View File

@ -1,34 +1,13 @@
package dev.inmo.micro_utils.startup.plugin package dev.inmo.micro_utils.startup.plugin
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin import org.koin.core.Koin
import org.koin.core.module.Module import org.koin.core.module.Module
@Serializable(ServerPlugin.Companion::class) @Serializable(ServerPluginSerializer::class)
interface ServerPlugin { interface ServerPlugin {
fun Module.setupDI(config: JsonObject) {} fun Module.setupDI(config: JsonObject) {}
suspend fun startPlugin(koin: Koin) {} suspend fun startPlugin(koin: Koin) {}
companion object : KSerializer<ServerPlugin> {
override val descriptor: SerialDescriptor
get() = String.serializer().descriptor
override fun deserialize(decoder: Decoder): ServerPlugin {
val kclass = Class.forName(decoder.decodeString()).kotlin
return (kclass.objectInstance ?: kclass.constructors.first { it.parameters.isEmpty() }.call()) as ServerPlugin
}
override fun serialize(encoder: Encoder, value: ServerPlugin) {
encoder.encodeString(
value::class.java.canonicalName
)
}
}
} }

View File

@ -0,0 +1,5 @@
package dev.inmo.micro_utils.startup.plugin
import kotlinx.serialization.KSerializer
expect object ServerPluginSerializer : KSerializer<ServerPlugin>

View File

@ -0,0 +1,23 @@
package dev.inmo.micro_utils.startup.plugin
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
actual object ServerPluginSerializer : KSerializer<ServerPlugin> {
override val descriptor: SerialDescriptor
get() = String.serializer().descriptor
override fun deserialize(decoder: Decoder): ServerPlugin {
val kclass = Class.forName(decoder.decodeString()).kotlin
return (kclass.objectInstance ?: kclass.constructors.first { it.parameters.isEmpty() }.call()) as ServerPlugin
}
override fun serialize(encoder: Encoder, value: ServerPlugin) {
encoder.encodeString(
value::class.java.canonicalName
)
}
}