diff --git a/src/main/kotlin/org/rooftop/netx/api/Orchestrator.kt b/src/main/kotlin/org/rooftop/netx/api/Orchestrator.kt index 41f6dd3..045ecec 100644 --- a/src/main/kotlin/org/rooftop/netx/api/Orchestrator.kt +++ b/src/main/kotlin/org/rooftop/netx/api/Orchestrator.kt @@ -8,7 +8,15 @@ interface Orchestrator { fun transaction(timeoutMillis: Long, request: T): Mono> + fun transaction(request: T, context: MutableMap): Mono> + + fun transaction(timeoutMillis: Long, request: T, context: MutableMap): Mono> + fun transactionSync(request: T): Result fun transactionSync(timeoutMillis: Long, request: T): Result + + fun transactionSync(request: T, context: MutableMap): Result + + fun transactionSync(timeoutMillis: Long, request: T, context: MutableMap): Result } diff --git a/src/main/kotlin/org/rooftop/netx/engine/OrchestratorManager.kt b/src/main/kotlin/org/rooftop/netx/engine/OrchestratorManager.kt index e018e43..d7483af 100644 --- a/src/main/kotlin/org/rooftop/netx/engine/OrchestratorManager.kt +++ b/src/main/kotlin/org/rooftop/netx/engine/OrchestratorManager.kt @@ -23,11 +23,37 @@ class OrchestratorManager internal constructor( ?: throw TransactionException("Cannot start transaction \"$request\"") } + override fun transactionSync(request: T, context: MutableMap): Result { + return transaction(request, context).block() + ?: throw TransactionException("Cannot start transaction \"$request\"") + } + + override fun transactionSync( + timeoutMillis: Long, + request: T, + context: MutableMap + ): Result { + return transaction(timeoutMillis, request, context).block() + ?: throw TransactionException("Cannot start transaction \"$request\"") + } + override fun transaction(request: T): Mono> { - return transaction(TEN_SECONDS_TO_TIME_OUT, request) + return transaction(TEN_SECONDS_TO_TIME_OUT, request, mutableMapOf()) } override fun transaction(timeoutMillis: Long, request: T): Mono> { + return transaction(timeoutMillis, request, mutableMapOf()) + } + + override fun transaction(request: T, context: MutableMap): Mono> { + return transaction(TEN_SECONDS_TO_TIME_OUT, request, context) + } + + override fun transaction( + timeoutMillis: Long, + request: T, + context: MutableMap + ): Mono> { return Mono.just(request) .doOnNext { _ -> orchestrateListener.setCastableType(request::class) @@ -37,7 +63,7 @@ class OrchestratorManager internal constructor( OrchestrateEvent( orchestratorId = orchestratorId, clientEvent = codec.encode(request), - context = codec.encode(mutableMapOf()) + context = codec.encode(context.mapValues { codec.encode(it.value) }) ) } .flatMap { transactionManager.start(UNDO, it) } diff --git a/src/test/kotlin/org/rooftop/netx/engine/OrchestratorConfigurer.kt b/src/test/kotlin/org/rooftop/netx/engine/OrchestratorConfigurer.kt index 9081483..a5544d7 100644 --- a/src/test/kotlin/org/rooftop/netx/engine/OrchestratorConfigurer.kt +++ b/src/test/kotlin/org/rooftop/netx/engine/OrchestratorConfigurer.kt @@ -197,6 +197,17 @@ class OrchestratorConfigurer( }) } + @Bean(name = ["startWithContextOrchestrator"]) + fun startWithContextOrchestrator(): Orchestrator { + return orchestratorFactory.create("startWithContextOrchestrator") + .startWithContext({ context, _ -> + context.decodeContext("key", String::class) + }) + .commitWithContext({ context, _ -> + context.decodeContext("key", String::class) + }) + } + object PairOrchestrate : Orchestrate, Pair> { override fun orchestrate(request: Pair): Pair { diff --git a/src/test/kotlin/org/rooftop/netx/engine/OrchestratorTest.kt b/src/test/kotlin/org/rooftop/netx/engine/OrchestratorTest.kt index ff3733c..ecade0b 100644 --- a/src/test/kotlin/org/rooftop/netx/engine/OrchestratorTest.kt +++ b/src/test/kotlin/org/rooftop/netx/engine/OrchestratorTest.kt @@ -34,6 +34,7 @@ class OrchestratorTest( @Qualifier("monoRollbackOrchestrator") private val monoRollbackOrchestrator: Orchestrator, @Qualifier("contextOrchestrator") private val contextOrchestrator: Orchestrator, @Qualifier("pairOrchestrator") private val pairOrchestrator: Orchestrator>, + @Qualifier("startWithContextOrchestrator") private val startWithContextOrchestrator: Orchestrator, ) : DescribeSpec({ describe("numberOrchestrator 구현채는") { @@ -173,6 +174,19 @@ class OrchestratorTest( } } } + + describe("startWithContextOrchestrator 구현채는") { + context("context와 함께 transaction 메소드가 호출되면,") { + it("key에 해당하는 context를 반환한다.") { + val result = startWithContextOrchestrator.transactionSync( + "ignored request", + mutableMapOf("key" to "hello") + ) + + result.decodeResultOrThrow(String::class) shouldBeEqual "hello" + } + } + } }) { data class Home( val address: String,