Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Orchestrator start with context #111

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/kotlin/org/rooftop/netx/api/Orchestrator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ interface Orchestrator<T : Any, V : Any> {

fun transaction(timeoutMillis: Long, request: T): Mono<Result<V>>

fun transaction(request: T, context: MutableMap<String, Any>): Mono<Result<V>>

fun transaction(timeoutMillis: Long, request: T, context: MutableMap<String, Any>): Mono<Result<V>>

fun transactionSync(request: T): Result<V>

fun transactionSync(timeoutMillis: Long, request: T): Result<V>

fun transactionSync(request: T, context: MutableMap<String, Any>): Result<V>

fun transactionSync(timeoutMillis: Long, request: T, context: MutableMap<String, Any>): Result<V>
}
30 changes: 28 additions & 2 deletions src/main/kotlin/org/rooftop/netx/engine/OrchestratorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,37 @@ class OrchestratorManager<T : Any, V : Any> internal constructor(
?: throw TransactionException("Cannot start transaction \"$request\"")
}

override fun transactionSync(request: T, context: MutableMap<String, Any>): Result<V> {
return transaction(request, context).block()
?: throw TransactionException("Cannot start transaction \"$request\"")
}

override fun transactionSync(
timeoutMillis: Long,
request: T,
context: MutableMap<String, Any>
): Result<V> {
return transaction(timeoutMillis, request, context).block()
?: throw TransactionException("Cannot start transaction \"$request\"")
}

override fun transaction(request: T): Mono<Result<V>> {
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<Result<V>> {
return transaction(timeoutMillis, request, mutableMapOf())
}

override fun transaction(request: T, context: MutableMap<String, Any>): Mono<Result<V>> {
return transaction(TEN_SECONDS_TO_TIME_OUT, request, context)
}

override fun transaction(
timeoutMillis: Long,
request: T,
context: MutableMap<String, Any>
): Mono<Result<V>> {
return Mono.just(request)
.doOnNext { _ ->
orchestrateListener.setCastableType(request::class)
Expand All @@ -37,7 +63,7 @@ class OrchestratorManager<T : Any, V : Any> internal constructor(
OrchestrateEvent(
orchestratorId = orchestratorId,
clientEvent = codec.encode(request),
context = codec.encode(mutableMapOf<String, String>())
context = codec.encode(context.mapValues { codec.encode(it.value) })
)
}
.flatMap { transactionManager.start(UNDO, it) }
Expand Down
11 changes: 11 additions & 0 deletions src/test/kotlin/org/rooftop/netx/engine/OrchestratorConfigurer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ class OrchestratorConfigurer(
})
}

@Bean(name = ["startWithContextOrchestrator"])
fun startWithContextOrchestrator(): Orchestrator<String, String> {
return orchestratorFactory.create<String>("startWithContextOrchestrator")
.startWithContext({ context, _ ->
context.decodeContext("key", String::class)
})
.commitWithContext({ context, _ ->
context.decodeContext("key", String::class)
})
}

object PairOrchestrate :
Orchestrate<Pair<OrchestratorTest.Foo, OrchestratorTest.Foo>, Pair<OrchestratorTest.Foo, OrchestratorTest.Foo>> {
override fun orchestrate(request: Pair<OrchestratorTest.Foo, OrchestratorTest.Foo>): Pair<OrchestratorTest.Foo, OrchestratorTest.Foo> {
Expand Down
14 changes: 14 additions & 0 deletions src/test/kotlin/org/rooftop/netx/engine/OrchestratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class OrchestratorTest(
@Qualifier("monoRollbackOrchestrator") private val monoRollbackOrchestrator: Orchestrator<String, String>,
@Qualifier("contextOrchestrator") private val contextOrchestrator: Orchestrator<String, String>,
@Qualifier("pairOrchestrator") private val pairOrchestrator: Orchestrator<String, Pair<Foo, Foo>>,
@Qualifier("startWithContextOrchestrator") private val startWithContextOrchestrator: Orchestrator<String, String>,
) : DescribeSpec({

describe("numberOrchestrator 구현채는") {
Expand Down Expand Up @@ -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,
Expand Down
Loading