fix repeatTaskUntilSuccess and k3d test
parent
211f66d1ce
commit
3bd9cdb6d9
@ -0,0 +1,17 @@
|
|||||||
|
package org.domaindrivenarchitecture.provs.core
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repeats task until it returns success
|
||||||
|
*/
|
||||||
|
fun Prov.repeatTaskUntilSuccess(times: Int, sleepInSec: Int, func: Prov.() -> ProvResult) = requireLast {
|
||||||
|
require(times > 0)
|
||||||
|
var result = ProvResult(false, err = "Internal error") // Will only be returned if function is not executed at all, otherwise func's last result is returned
|
||||||
|
for (i in 1..times) {
|
||||||
|
result = func()
|
||||||
|
if (result.success)
|
||||||
|
return@requireLast result
|
||||||
|
Thread.sleep(sleepInSec * 1000L)
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package org.domaindrivenarchitecture.provs.core
|
||||||
|
|
||||||
|
import org.domaindrivenarchitecture.provs.test.testLocal
|
||||||
|
import org.junit.jupiter.api.Assertions.*
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
internal class TaskFunctionsKtTest {
|
||||||
|
|
||||||
|
var count = 1
|
||||||
|
fun Prov.altenateSuccessAndFailure() = task {
|
||||||
|
if (count == 0) {
|
||||||
|
count = 1
|
||||||
|
ProvResult(true, out = "0")
|
||||||
|
} else {
|
||||||
|
count--
|
||||||
|
ProvResult(false, err = "1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Prov.repeating() = requireLast {
|
||||||
|
val res = repeatTaskUntilSuccess(4, 1) {
|
||||||
|
altenateSuccessAndFailure()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.success && ("0" == res.out?.trim())) {
|
||||||
|
ProvResult(true)
|
||||||
|
} else {
|
||||||
|
ProvResult(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun repeat_and_requireLast() {
|
||||||
|
assertTrue(testLocal().repeating().success)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue