Skip to content

Commit

Permalink
Merge branch 'feature/KAIZEN-112_fix_loop' into 'ver-0.2'
Browse files Browse the repository at this point in the history
KAIZEN-112 Delete max exception for loop

See merge request datana_smart/datana-smart-libs/kotlin-cor!4
  • Loading branch information
Okatov Sergey committed Jan 31, 2022
2 parents 5e50265 + 9fbc6f8 commit 792061a
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 198 deletions.
5 changes: 0 additions & 5 deletions src/commonMain/kotlin/Const.kt

This file was deleted.

45 changes: 2 additions & 43 deletions src/commonMain/kotlin/handlers/Loop.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.crowdproj.kotlin.cor.handlers

import com.crowdproj.kotlin.cor.Const.LOOP_MAX_EXCEPTION
import com.crowdproj.kotlin.cor.CorDslMarker
import com.crowdproj.kotlin.cor.ICorChainDsl
import com.crowdproj.kotlin.cor.ICorExec
import com.crowdproj.kotlin.cor.base.BaseCorChain
import com.crowdproj.kotlin.cor.base.BaseCorChainDsl
import kotlin.math.absoluteValue

@CorDslMarker
fun <T> ICorChainDsl<T>.loopWhile(
Expand All @@ -33,9 +31,7 @@ class CorLoop<T>(
description: String = "",
blockOn: suspend T.() -> Boolean = { true },
blockExcept: suspend T.(Throwable) -> Unit = {},
val blockMaxEx: suspend T.() -> Long = { LOOP_MAX_EXCEPTION },
var blockCheck: suspend T.() -> Boolean = { true },
var blockFailed: suspend T.() -> Unit = {},
) : BaseCorChain<T>(
title = title,
description = description,
Expand All @@ -51,51 +47,31 @@ class CorLoop<T>(
}

private suspend fun loopWhile(context: T) {
var numException = 0L
val maxException = blockMaxEx.invoke(context).takeIf { it >= 0 } ?: blockMaxEx.invoke(context).absoluteValue
while (blockCheck.invoke(context)
&& (numException < maxException
|| (numException == 0L && maxException == 0L))
) {
while (blockCheck.invoke(context)) {
try {
execs.forEach { it.exec(context) }
} catch (e: Throwable) {
except(context, e)
numException++
}
}
if (numException >= maxException) {
blockFailed.invoke(context)
}
}

private suspend fun loopUntil(context: T) {
var numException = 0L
val maxException = blockMaxEx.invoke(context).takeIf { it >= 0 } ?: blockMaxEx.invoke(context).absoluteValue
do {
try {
execs.forEach { it.exec(context) }
} catch (e: Throwable) {
except(context, e)
numException++
}
} while (blockCheck.invoke(context)
&& (numException < maxException
|| (numException == 0L && maxException == 0L))
)
if (numException >= maxException) {
blockFailed.invoke(context)
}
} while (blockCheck.invoke(context))
}

}

@CorDslMarker
class CorLoopDsl<T>(
private val checkBefore: Boolean,
var blockMaxEx: suspend T.() -> Long = { LOOP_MAX_EXCEPTION },
var blockCheck: suspend T.() -> Boolean = { true },
var blockFailed: suspend T.() -> Unit = {},
) : BaseCorChainDsl<T>() {
override fun build(): ICorExec<T> = CorLoop(
checkBefore,
Expand All @@ -104,19 +80,9 @@ class CorLoopDsl<T>(
execs = workers.map { it.build() }.toList(),
blockOn = blockOn,
blockExcept = blockExcept,
blockMaxEx = blockMaxEx,
blockCheck = blockCheck,
blockFailed = blockFailed,
)

/**
* Maximum allowed number of exceptions
* If the value is less than zero, then the number of exceptions is unlimited
*/
fun maxEx(function: suspend T.() -> Long) {
blockMaxEx = function
}

/**
* Cycle repetition condition
* Repeat until the condition is met
Expand All @@ -125,12 +91,5 @@ class CorLoopDsl<T>(
blockCheck = function
}

/**
* Executed when the allowed number of exceptions is exceeded
*/
fun failed(function: suspend T.() -> Unit) {
blockFailed = function
}

}

78 changes: 0 additions & 78 deletions src/commonTest/kotlin/LoopCorBaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class LoopCorBaseTest {
on { true }
check { some < 5 }
except { status = CorStatuses.FAILING }
maxEx { 5L }
worker(title = "Increment some") {
some++
println("=$some")
Expand All @@ -43,7 +42,6 @@ class LoopCorBaseTest {
on { true }
check { some < 5 }
except { status = CorStatuses.FAILING }
maxEx { 5L }
worker(title = "Increment some") {
some++
println("=$some")
Expand All @@ -59,81 +57,5 @@ class LoopCorBaseTest {
}
}.build()

val exceptionLoopWhile = rootChain<TestContext> {
loopWhile {
check { some < 10 } // выполняется пока true
except { status = CorStatuses.FAILING }
maxEx { 5L } // возможное количество исключений
failed { some-- }
worker(title = "Increment some") {
some++
throw RuntimeException("ex loop")
}
}
}.build()

val exceptionLoopUntil = rootChain<TestContext> {
loopUntil {
check { some < 10 }
except { status = CorStatuses.FAILING }
maxEx { 5L }
failed { some-- }
worker(title = "Increment some") {
some++
throw RuntimeException("ex loop")
}
}
}.build()

val zeroExceptionLoopUntil = rootChain<TestContext> {
loopUntil {
check { some < 10 }
except { status = CorStatuses.FAILING }
maxEx { 0L }
worker(title = "Increment some") {
some++
throw RuntimeException("ex loop")
}
}
}.build()

val zeroExceptionLoopWhile = rootChain<TestContext> {
loopWhile {
check { some < 10 }
except { status = CorStatuses.FAILING }
maxEx { 0L }
worker(title = "Increment some") {
some++
throw RuntimeException("ex loop")
}
}
}.build()

val lessThanZeroExceptionLoopUntil = rootChain<TestContext> {
loopUntil {
check { some < 6 }
except { status = CorStatuses.FAILING }
maxEx { -1L }
failed { some++ }
worker(title = "Increment some") {
some++
throw RuntimeException("ex loop")
}
}
}.build()

val lessThanZeroExceptionLoopWhile = rootChain<TestContext> {
loopWhile {
check { some < 6 }
except { status = CorStatuses.FAILING }
maxEx { -1L }
failed { some++ }
worker(title = "Increment some") {
some++
throw RuntimeException("ex loop")
}
}
}.build()

}
}
72 changes: 0 additions & 72 deletions src/jvmTest/kotlin/CorJvmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,76 +43,4 @@ class CorJvmTest {
assertEquals(10, ctx.some)
}

@Test
fun exceptionLoopWhileCorTest() {
val chain = LoopCorBaseTest.exceptionLoopWhile

val ctx = TestContext(some = 0)

runBlocking { chain.exec(ctx) }

assertEquals(CorStatuses.FAILING, ctx.status)
assertEquals(4, ctx.some)
}

@Test
fun exceptionLoopUntilCorTest() {
val chain = LoopCorBaseTest.exceptionLoopUntil

val ctx = TestContext(some = 0)

runBlocking { chain.exec(ctx) }

assertEquals(CorStatuses.FAILING, ctx.status)
assertEquals(4, ctx.some)
}

@Test
fun zeroExceptionLoopUntilCorTest() {
val chain = LoopCorBaseTest.zeroExceptionLoopUntil

val ctx = TestContext(some = 0)

runBlocking { chain.exec(ctx) }

assertEquals(CorStatuses.FAILING, ctx.status)
assertEquals(1, ctx.some)
}

@Test
fun zeroExceptionLoopWhileCorTest() {
val chain = LoopCorBaseTest.zeroExceptionLoopWhile

val ctx = TestContext(some = 0)

runBlocking { chain.exec(ctx) }

assertEquals(CorStatuses.FAILING, ctx.status)
assertEquals(1, ctx.some)
}

@Test
fun lessThanZeroExceptionLoopUntilCorTest() {
val chain = LoopCorBaseTest.lessThanZeroExceptionLoopUntil

val ctx = TestContext(some = 0)

runBlocking { chain.exec(ctx) }

assertEquals(CorStatuses.FAILING, ctx.status)
assertEquals(2, ctx.some)
}

@Test
fun lessThanZeroExceptionLoopWhileCorTest() {
val chain = LoopCorBaseTest.lessThanZeroExceptionLoopWhile

val ctx = TestContext(some = 0)

runBlocking { chain.exec(ctx) }

assertEquals(CorStatuses.FAILING, ctx.status)
assertEquals(2, ctx.some)
}

}

0 comments on commit 792061a

Please sign in to comment.