-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Interface structure is refactored
2. subChain handler is added
- Loading branch information
Showing
19 changed files
with
178 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
kotlinVersion=1.7.0 | ||
kotlinVersion=1.7.10 | ||
|
||
coroutinesVersion=1.6.3 | ||
# -native-mt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.crowdproj.kotlin.cor.handlers | ||
|
||
import com.crowdproj.kotlin.cor.CorDslMarker | ||
import com.crowdproj.kotlin.cor.ICorAddExecDsl | ||
import com.crowdproj.kotlin.cor.ICorExec | ||
import com.crowdproj.kotlin.cor.base.BaseCorChain | ||
import com.crowdproj.kotlin.cor.base.BaseCorChainDsl | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.launch | ||
|
||
@CorDslMarker | ||
fun <T, K> ICorAddExecDsl<T>.subChain(function: CorSubChainDsl<T,K>.() -> Unit) { | ||
add(CorSubChainDsl<T,K>().apply(function)) | ||
} | ||
|
||
class CorSubChain<T,K>( | ||
private val execs: List<ICorExec<K>>, | ||
title: String, | ||
description: String = "", | ||
blockOn: suspend T.() -> Boolean = { true }, | ||
private val blockSplit: suspend T.() -> Flow<K>, | ||
private val blockJoin: suspend T.(K) -> Unit, | ||
blockExcept: suspend T.(Throwable) -> Unit = {}, | ||
private val buffer: Int = 1, | ||
) : BaseCorChain<T>( | ||
title = title, | ||
description = description, | ||
blockOn = blockOn, | ||
blockExcept = blockExcept | ||
) { | ||
|
||
override suspend fun handle(context: T): Unit = coroutineScope { | ||
context | ||
.blockSplit() | ||
.onEach { subCtx -> execs.map{ launch { it.exec(subCtx) } }.forEach { it.join() } } | ||
.buffer(buffer) | ||
.collect { context.blockJoin(it) } | ||
} | ||
} | ||
|
||
/** | ||
* DLS is the execution context of multiple chains. | ||
* It can be expanded by other chains. | ||
*/ | ||
@CorDslMarker | ||
class CorSubChainDsl<T,K>( | ||
): BaseCorChainDsl<T,K>() { | ||
private var blockSplit: suspend T.() -> Flow<K> = { emptyFlow() } | ||
private var blockJoin: suspend T.(K) -> Unit = {} | ||
private var bufferSize: Int = 0 | ||
|
||
fun buffer(size: Int) { | ||
bufferSize = size | ||
} | ||
|
||
fun split(funSplit: suspend T.() -> Flow<K>) { | ||
blockSplit = funSplit | ||
} | ||
|
||
fun join(funJoin: suspend T.(K) -> Unit) { | ||
blockJoin = funJoin | ||
} | ||
|
||
override fun build(): ICorExec<T> = CorSubChain( | ||
title = title, | ||
description = description, | ||
execs = workers.map { it.build() }.toList(), | ||
blockOn = blockOn, | ||
blockExcept = blockExcept, | ||
blockSplit = blockSplit, | ||
blockJoin = blockJoin, | ||
buffer = bufferSize | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.crowdproj.kotlin.cor | ||
|
||
import com.crowdproj.kotlin.cor.handlers.subChain | ||
import com.crowdproj.kotlin.cor.handlers.worker | ||
import com.crowdproj.kotlin.cor.helper.CorStatuses | ||
import com.crowdproj.kotlin.cor.helper.TestContext | ||
import com.crowdproj.kotlin.cor.helper.TestSubContext | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.asFlow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class CorSubChainTest { | ||
@Test | ||
fun createCor() = runTest { | ||
val ctx = TestContext() | ||
chain.exec(ctx) | ||
assertEquals(CorStatuses.RUNNING, ctx.status) | ||
assertEquals(65, ctx.some) | ||
} | ||
|
||
companion object { | ||
val chain = rootChain<TestContext> { | ||
worker("status") { status = CorStatuses.RUNNING } | ||
worker("init") { | ||
some = 0 | ||
} | ||
subChain<TestContext, TestSubContext> { | ||
buffer(20) | ||
on { status == CorStatuses.RUNNING } | ||
split { (1..10).asFlow().map { TestSubContext(temp = it) } } | ||
worker { | ||
handle { | ||
temp ++ | ||
} | ||
} | ||
join { sub: TestSubContext -> | ||
some += sub.temp | ||
} | ||
} | ||
}.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.crowdproj.kotlin.cor.helper | ||
|
||
enum class CorStatuses { | ||
NONE, | ||
RUNNING, | ||
FAILING, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.crowdproj.kotlin.cor.helper | ||
|
||
data class TestContext( | ||
var status: CorStatuses = CorStatuses.NONE, | ||
var some: Int = Int.MIN_VALUE, | ||
var text: String = "", | ||
) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.crowdproj.kotlin.cor.helper | ||
|
||
data class TestSubContext( | ||
var temp: Int = Int.MIN_VALUE, | ||
var str: String = "", | ||
) |