From 06beb60a9316078122cc8e3e9d9e88a10653a544 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Thu, 28 Nov 2024 07:56:05 +0300 Subject: [PATCH 1/4] Add Computation and Data Transformers for testing --- .../computation/ComputationTransformer.kt | 20 +++++++++++++++++++ .../ComputationWithArgsTransformer.kt | 20 +++++++++++++++++++ .../transmissiontest/data/DataTransformer.kt | 20 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 transmission-test/src/main/java/com/trendyol/transmissiontest/computation/ComputationTransformer.kt create mode 100644 transmission-test/src/main/java/com/trendyol/transmissiontest/computation/ComputationWithArgsTransformer.kt create mode 100644 transmission-test/src/main/java/com/trendyol/transmissiontest/data/DataTransformer.kt diff --git a/transmission-test/src/main/java/com/trendyol/transmissiontest/computation/ComputationTransformer.kt b/transmission-test/src/main/java/com/trendyol/transmissiontest/computation/ComputationTransformer.kt new file mode 100644 index 0000000..eedcce5 --- /dev/null +++ b/transmission-test/src/main/java/com/trendyol/transmissiontest/computation/ComputationTransformer.kt @@ -0,0 +1,20 @@ +package com.trendyol.transmissiontest.computation + +import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.computation.Computations +import com.trendyol.transmission.transformer.request.computation.createComputations +import com.trendyol.transmission.transformer.request.computation.register +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.UnconfinedTestDispatcher + +@OptIn(ExperimentalCoroutinesApi::class) +internal class ComputationTransformer, D: Any?>( + contract: C, data: () -> D? +): Transformer(dispatcher = UnconfinedTestDispatcher()) { + override val computations: Computations = createComputations { + register(contract) { + data() + } + } +} diff --git a/transmission-test/src/main/java/com/trendyol/transmissiontest/computation/ComputationWithArgsTransformer.kt b/transmission-test/src/main/java/com/trendyol/transmissiontest/computation/ComputationWithArgsTransformer.kt new file mode 100644 index 0000000..971b130 --- /dev/null +++ b/transmission-test/src/main/java/com/trendyol/transmissiontest/computation/ComputationWithArgsTransformer.kt @@ -0,0 +1,20 @@ +package com.trendyol.transmissiontest.computation + +import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmission.transformer.request.computation.Computations +import com.trendyol.transmission.transformer.request.computation.createComputations +import com.trendyol.transmission.transformer.request.computation.register +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.UnconfinedTestDispatcher + +@OptIn(ExperimentalCoroutinesApi::class) +internal class ComputationWithArgsTransformer, D: Any?, A: Any>( + contract: C, data: () -> D? +): Transformer(dispatcher = UnconfinedTestDispatcher()) { + override val computations: Computations = createComputations { + register(contract) { + data() + } + } +} diff --git a/transmission-test/src/main/java/com/trendyol/transmissiontest/data/DataTransformer.kt b/transmission-test/src/main/java/com/trendyol/transmissiontest/data/DataTransformer.kt new file mode 100644 index 0000000..207d1fe --- /dev/null +++ b/transmission-test/src/main/java/com/trendyol/transmissiontest/data/DataTransformer.kt @@ -0,0 +1,20 @@ +package com.trendyol.transmissiontest.data + +import com.trendyol.transmission.Transmission +import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.dataholder.dataHolder +import com.trendyol.transmission.transformer.request.Contract +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.UnconfinedTestDispatcher + +@OptIn(ExperimentalCoroutinesApi::class) +class DataTransformer( + contract: Contract.DataHolder, data: () -> D +) : Transformer(dispatcher = UnconfinedTestDispatcher()) { + + private val dataHolder = dataHolder( + initialValue = data(), + contract = contract, + publishUpdates = false + ) +} From db7e0920690cb7e7723450335500a785e81e66ea Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Thu, 28 Nov 2024 07:56:27 +0300 Subject: [PATCH 2/4] update computatio request parameters --- .../trendyol/transmission/transformer/request/Contract.kt | 4 ++-- .../transmission/transformer/request/ContractExt.kt | 4 ++-- .../transformer/request/computation/ComputationExt.kt | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/Contract.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/Contract.kt index bbddd8b..2813e7b 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/Contract.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/Contract.kt @@ -10,12 +10,12 @@ sealed interface Contract { internal val key: String, ) : Contract - class Computation internal constructor( + class Computation internal constructor( internal val key: String, internal val useCache: Boolean = false ) : Contract - class ComputationWithArgs internal constructor( + class ComputationWithArgs internal constructor( internal val key: String, internal val useCache: Boolean = false ) : Contract diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/ContractExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/ContractExt.kt index 594a09c..d0696c6 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/ContractExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/ContractExt.kt @@ -14,7 +14,7 @@ fun Contracts.dataHolder(): Contract.DataHolder { return Contract.DataHolder(key = IdentifierGenerator.generateIdentifier()) } -fun Contracts.computation( +fun Contracts.computation( useCache: Boolean = false ): Contract.Computation { return Contract.Computation( @@ -23,7 +23,7 @@ fun Contracts.computation( ) } -fun Contracts.computationWithArgs( +fun Contracts.computationWithArgs( useCache: Boolean = false ): Contract.ComputationWithArgs { return Contract.ComputationWithArgs( diff --git a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt index 3d9e97a..c462907 100644 --- a/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt +++ b/transmission/src/main/java/com/trendyol/transmission/transformer/request/computation/ComputationExt.kt @@ -13,9 +13,9 @@ import com.trendyol.transmission.transformer.request.RequestHandler * Can be queried using [RequestHandler.compute] * @param computation Computation to get the result [Transmission.Data] */ -fun , T : Any> ComputationScope.register( +fun , T : Any?> ComputationScope.register( contract: C, - computation: suspend RequestHandler.() -> T?, + computation: suspend RequestHandler.() -> T, ) { this.computationRegistry.buildWith(contract.key, contract.useCache, computation) } @@ -28,9 +28,9 @@ fun , T : Any> ComputationScope.register( * Can be queried using [RequestHandler.compute] * @param computation Computation to get the result [Transmission.Data] */ -fun , A : Any, T : Any> ComputationScope.register( +fun , A : Any, T : Any?> ComputationScope.register( contract: C, - computation: suspend RequestHandler.(args: A) -> T?, + computation: suspend RequestHandler.(args: A) -> T, ) { this.computationRegistry.buildWith(contract.key, contract.useCache, computation) } From 32d9c85626b81aba8e86c1057a13f96ab7711ae3 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Thu, 28 Nov 2024 07:57:31 +0300 Subject: [PATCH 3/4] Delete old testing code and bind query processing to actual one --- .../trendyol/transmissiontest/TestSuite.kt | 40 +++++++++---- .../transmission/router/RegistryScope.kt | 30 ---------- .../transmission/router/RequestDelegate.kt | 57 +------------------ .../transmission/router/TransmissionRouter.kt | 2 - .../TransmissionRouterBuilderInternal.kt | 12 +--- .../TransmissionTestingRouterBuilder.kt | 18 ------ .../TransmissionTestingRouterBuilderScope.kt | 7 --- 7 files changed, 33 insertions(+), 133 deletions(-) delete mode 100644 transmission/src/main/java/com/trendyol/transmission/router/RegistryScope.kt delete mode 100644 transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilder.kt delete mode 100644 transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilderScope.kt diff --git a/transmission-test/src/main/java/com/trendyol/transmissiontest/TestSuite.kt b/transmission-test/src/main/java/com/trendyol/transmissiontest/TestSuite.kt index 463561d..a905d9e 100644 --- a/transmission-test/src/main/java/com/trendyol/transmissiontest/TestSuite.kt +++ b/transmission-test/src/main/java/com/trendyol/transmissiontest/TestSuite.kt @@ -2,9 +2,12 @@ package com.trendyol.transmissiontest import com.trendyol.transmission.Transmission import com.trendyol.transmission.router.TransmissionRouter -import com.trendyol.transmission.router.RegistryScope -import com.trendyol.transmission.router.builder.TransmissionTestingRouterBuilder +import com.trendyol.transmission.router.builder.TransmissionRouterBuilder import com.trendyol.transmission.transformer.Transformer +import com.trendyol.transmission.transformer.request.Contract +import com.trendyol.transmissiontest.computation.ComputationTransformer +import com.trendyol.transmissiontest.computation.ComputationWithArgsTransformer +import com.trendyol.transmissiontest.data.DataTransformer import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch @@ -15,23 +18,39 @@ import kotlinx.coroutines.test.runTest @OptIn(ExperimentalCoroutinesApi::class) class TestSuite { - private var orderedInitialProcessing: List = emptyList() + private var orderedInitialProcessing: MutableList = mutableListOf() private var transformer: Transformer? = null - private var registryScope: RegistryScope.() -> Unit = {} private lateinit var router: TransmissionRouter + private val supplementaryTransformerSet: MutableList = mutableListOf() fun initialize(transformer: Transformer): TestSuite { this.transformer = transformer return this } - fun register(registry: RegistryScope.() -> Unit = {}): TestSuite { - this.registryScope = registry + fun registerData( + contract: Contract.DataHolder, data: () -> D + ): TestSuite { + supplementaryTransformerSet += DataTransformer(contract, data) + return this + } + + fun , D : Any> registerComputation( + contract: C, data: () -> D? + ): TestSuite { + supplementaryTransformerSet += ComputationTransformer(contract, data) + return this + } + + fun , D : Any, A: Any> registerComputation( + contract: C, data: () -> D? + ): TestSuite { + supplementaryTransformerSet += ComputationWithArgsTransformer(contract, data) return this } fun processBeforeTesting(vararg transmissions: Transmission): TestSuite { - orderedInitialProcessing = transmissions.toList() + orderedInitialProcessing += transmissions.toList() return this } @@ -41,10 +60,11 @@ class TestSuite { transmission: Transmission, scope: suspend TransformerTestScope.(scope: TestScope) -> Unit ) { - router = TransmissionTestingRouterBuilder.build { + router = TransmissionRouterBuilder.build { addDispatcher(UnconfinedTestDispatcher()) - this@TestSuite.transformer?.let { addTransformerSet(setOf(it)) } - testing(this@TestSuite.registryScope) + this@TestSuite.transformer?.let { + addTransformerSet((listOf(it) + supplementaryTransformerSet).toSet()) + } } runTest { diff --git a/transmission/src/main/java/com/trendyol/transmission/router/RegistryScope.kt b/transmission/src/main/java/com/trendyol/transmission/router/RegistryScope.kt deleted file mode 100644 index 76dfbbe..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/router/RegistryScope.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.trendyol.transmission.router - -import com.trendyol.transmission.Transmission - -interface RegistryScope { - - fun addQueryData( - data: D, - key: String, - ) - - fun addComputation( - data: D, - key: String, - ) -} - -internal class RegistryScopeImpl : RegistryScope { - - val dataMap: MutableMap = mutableMapOf() - val computationMap: MutableMap = mutableMapOf() - - override fun addQueryData(data: D, key: String) { - dataMap[key] = data - } - - override fun addComputation(data: D, key: String) { - computationMap[key] = data - } -} diff --git a/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt index 161e5ab..7aa36bc 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/RequestDelegate.kt @@ -22,7 +22,6 @@ import kotlinx.coroutines.launch internal class RequestDelegate( private val queryScope: CoroutineScope, private val routerRef: TransmissionRouter, - private val registry: RegistryScopeImpl? = null, ) : RequestHandler { private val routerQueryResultChannel: MutableSharedFlow = MutableSharedFlow() @@ -36,11 +35,7 @@ internal class RequestDelegate( init { queryScope.launch { - if (registry != null) { - outGoingQuery.consumeAsFlow().collect { testQuery(it) } - } else { - outGoingQuery.consumeAsFlow().collect { processQuery(it) } - } + outGoingQuery.consumeAsFlow().collect { processQuery(it) } } } @@ -164,56 +159,6 @@ internal class RequestDelegate( // endregion - // region test queries - - private fun testQuery(query: Query) = queryScope.launch { - when (query) { - is Query.Computation -> testComputationQuery(query) - is Query.Data -> testDataQuery(query) - is Query.ComputationWithArgs<*> -> testComputationQueryWithArgs(query) - is Query.Execution -> {} - is Query.ExecutionWithArgs<*> -> {} - } - } - - private fun testDataQuery( - query: Query.Data, - ) = queryScope.launch { - val dataToSend = QueryResult.Data( - owner = query.sender, - data = registry?.dataMap?.get(query.key), - key = query.key, - resultIdentifier = query.queryIdentifier - ) - queryScope.launch { - queryResultChannel.trySend(dataToSend) - } - } - - private fun testComputationQuery( - query: Query.Computation, - ) = queryScope.launch { - val computationToSend = QueryResult.Computation( - owner = query.sender, - data = registry?.computationMap?.get(query.key), - key = query.key, - resultIdentifier = query.queryIdentifier - ) - queryResultChannel.trySend(computationToSend) - } - - private fun testComputationQueryWithArgs( - query: Query.ComputationWithArgs, - ) = queryScope.launch { - val computationToSend = QueryResult.Computation( - owner = query.sender, - data = registry?.computationMap?.get(query.key), - key = query.key, - resultIdentifier = query.queryIdentifier - ) - queryResultChannel.trySend(computationToSend) - } - // region Request Handler override suspend fun , D : Transmission.Data> getData(contract: C): D? { diff --git a/transmission/src/main/java/com/trendyol/transmission/router/TransmissionRouter.kt b/transmission/src/main/java/com/trendyol/transmission/router/TransmissionRouter.kt index 3b824c5..a04d0c4 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/TransmissionRouter.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/TransmissionRouter.kt @@ -28,7 +28,6 @@ class TransmissionRouter internal constructor( internal val transformerSetLoader: TransformerSetLoader? = null, internal val autoInitialization: Boolean = true, dispatcher: CoroutineDispatcher = Dispatchers.Default, - registryScope: RegistryScopeImpl? = null ) { private val exceptionHandler = CoroutineExceptionHandler { _, _ -> } @@ -52,7 +51,6 @@ class TransmissionRouter internal constructor( private val _requestDelegate = RequestDelegate( queryScope = routerScope, routerRef = this@TransmissionRouter, - registry = registryScope ) val requestHelper: RequestHandler = _requestDelegate diff --git a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderInternal.kt b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderInternal.kt index 6309f30..acb1f3b 100644 --- a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderInternal.kt +++ b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionRouterBuilderInternal.kt @@ -1,30 +1,22 @@ package com.trendyol.transmission.router.builder -import com.trendyol.transmission.router.RegistryScope -import com.trendyol.transmission.router.RegistryScopeImpl import com.trendyol.transmission.router.loader.TransformerSetLoader import com.trendyol.transmission.transformer.Transformer import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers internal class TransmissionRouterBuilderInternal internal constructor( - scope: TransmissionTestingRouterBuilderScope.() -> Unit + scope: TransmissionRouterBuilderScope.() -> Unit ) { internal var dispatcher: CoroutineDispatcher = Dispatchers.Default - internal var registryScope: RegistryScopeImpl? = null internal lateinit var transformerSetLoader: TransformerSetLoader internal var autoInitialization: Boolean = true - private val scopeImpl = object : TransmissionTestingRouterBuilderScope { - - override fun testing(scope: RegistryScope.() -> Unit) { - this@TransmissionRouterBuilderInternal.registryScope = RegistryScopeImpl().apply(scope) - } + private val scopeImpl = object : TransmissionRouterBuilderScope { override fun addDispatcher(dispatcher: CoroutineDispatcher) { this@TransmissionRouterBuilderInternal.dispatcher = dispatcher - } override fun addTransformerSet(transformerSet: Set) { diff --git a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilder.kt b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilder.kt deleted file mode 100644 index a643cdd..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilder.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.trendyol.transmission.router.builder - -import com.trendyol.transmission.router.TransmissionRouter -import com.trendyol.transmission.transformer.request.Contracts -import com.trendyol.transmission.transformer.request.identity - -object TransmissionTestingRouterBuilder { - - fun build(scope: TransmissionTestingRouterBuilderScope.() -> Unit): TransmissionRouter { - val builder = TransmissionRouterBuilderInternal(scope) - return TransmissionRouter( - identity = Contracts.identity(), - transformerSetLoader = builder.transformerSetLoader, - dispatcher = builder.dispatcher, - registryScope = builder.registryScope - ) - } -} diff --git a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilderScope.kt b/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilderScope.kt deleted file mode 100644 index 8f450df..0000000 --- a/transmission/src/main/java/com/trendyol/transmission/router/builder/TransmissionTestingRouterBuilderScope.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.trendyol.transmission.router.builder - -import com.trendyol.transmission.router.RegistryScope - -interface TransmissionTestingRouterBuilderScope : TransmissionRouterBuilderScope { - fun testing(scope: RegistryScope.() -> Unit = {}) -} From 4f25a84c5201a54f339dac0a2353d46c8787a392 Mon Sep 17 00:00:00 2001 From: Yigit Ozgumus Date: Thu, 28 Nov 2024 07:57:39 +0300 Subject: [PATCH 4/4] Update sample --- .../features/colorpicker/ColorPickerTransformerTest.kt | 9 +++++---- .../features/output/OutputTransformerTest.kt | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/sample/src/test/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformerTest.kt b/sample/src/test/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformerTest.kt index e505fcb..dfaced1 100644 --- a/sample/src/test/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformerTest.kt +++ b/sample/src/test/java/com/trendyol/transmission/features/colorpicker/ColorPickerTransformerTest.kt @@ -1,11 +1,11 @@ package com.trendyol.transmission.features.colorpicker import androidx.compose.ui.graphics.Color +import com.trendyol.transmission.components.features.ColorPickerUiState import com.trendyol.transmission.components.features.colorpicker.ColorPickerEffect import com.trendyol.transmission.components.features.colorpicker.ColorPickerSignal import com.trendyol.transmission.components.features.colorpicker.ColorPickerTransformer import com.trendyol.transmission.transformer.util.TestCoroutineRule -import com.trendyol.transmission.components.features.ColorPickerUiState import com.trendyol.transmissiontest.attachToRouter import com.trendyol.transmissiontest.test import org.junit.Before @@ -70,8 +70,9 @@ class ColorPickerTransformerTest { @Test fun `GIVEN inputTransformer, WHEN SelectColor signal is sent, THEN SelectedColorUpdate is sent to MultiOutputTransformer`() { - sut.attachToRouter().test(signal = ColorPickerSignal.SelectColor(3, Color.Blue)) { - assertTrue { effectStream.last() is ColorPickerEffect.SelectedColorUpdate } - } + sut.attachToRouter() + .test(signal = ColorPickerSignal.SelectColor(3, Color.Blue)) { + assertTrue { effectStream.last() is ColorPickerEffect.SelectedColorUpdate } + } } } diff --git a/sample/src/test/java/com/trendyol/transmission/features/output/OutputTransformerTest.kt b/sample/src/test/java/com/trendyol/transmission/features/output/OutputTransformerTest.kt index a031545..2ef9011 100644 --- a/sample/src/test/java/com/trendyol/transmission/features/output/OutputTransformerTest.kt +++ b/sample/src/test/java/com/trendyol/transmission/features/output/OutputTransformerTest.kt @@ -6,6 +6,7 @@ import com.trendyol.transmission.components.features.output.OutputTransformer import com.trendyol.transmission.transformer.util.TestCoroutineRule import com.trendyol.transmission.components.features.ColorPickerUiState import com.trendyol.transmission.components.features.OutputUiState +import com.trendyol.transmission.components.features.colorpicker.ColorPickerTransformer import com.trendyol.transmissiontest.attachToRouter import com.trendyol.transmissiontest.test import org.junit.Before @@ -45,8 +46,8 @@ class OutputTransformerTest { @Test fun `GIVEN sut, WHEN inputUpdate effect comes and ColorPickerUIState exists, THEN RouterPayloadEffect should be published`() { sut.attachToRouter() - .register { - addQueryData(ColorPickerUiState(), key = "ColorPickerUiState") + .registerData(ColorPickerTransformer.holderContract) { + ColorPickerUiState() } .test(effect = InputEffect.InputUpdate("test")) { assertEquals(OutputUiState(outputText = "test"), dataStream[1])