diff --git a/murphy-core/src/main/kotlin/io/murphy/core/effect/DelayEffect.kt b/murphy-core/src/main/kotlin/io/murphy/core/effect/DelayEffect.kt index 2965bdd..d19d09e 100644 --- a/murphy-core/src/main/kotlin/io/murphy/core/effect/DelayEffect.kt +++ b/murphy-core/src/main/kotlin/io/murphy/core/effect/DelayEffect.kt @@ -1,7 +1,17 @@ package io.murphy.core.effect import io.murphy.core.Effect +import io.murphy.core.MurphyContext +import io.murphy.core.MurphyResponse sealed class DelayEffect : Effect { abstract val duration: Long + + override fun apply(context: MurphyContext): MurphyResponse? { + val d = duration + if (d > 0) { + Thread.sleep(d) + } + return null + } } diff --git a/murphy-core/src/main/kotlin/io/murphy/core/effect/JitterEffect.kt b/murphy-core/src/main/kotlin/io/murphy/core/effect/JitterEffect.kt index b8fb33b..72e74a9 100644 --- a/murphy-core/src/main/kotlin/io/murphy/core/effect/JitterEffect.kt +++ b/murphy-core/src/main/kotlin/io/murphy/core/effect/JitterEffect.kt @@ -1,7 +1,5 @@ package io.murphy.core.effect -import io.murphy.core.MurphyContext -import io.murphy.core.MurphyResponse import java.util.concurrent.ThreadLocalRandom internal class JitterEffect( @@ -11,12 +9,4 @@ internal class JitterEffect( override val duration: Long get() = if (min >= max) min else ThreadLocalRandom.current().nextLong(min, max + 1) - - override fun apply(context: MurphyContext): MurphyResponse? { - val delay = duration - if (delay > 0) { - Thread.sleep(delay) - } - return null - } } diff --git a/murphy-core/src/main/kotlin/io/murphy/core/effect/LatencyEffect.kt b/murphy-core/src/main/kotlin/io/murphy/core/effect/LatencyEffect.kt index e15e98d..2849c5b 100644 --- a/murphy-core/src/main/kotlin/io/murphy/core/effect/LatencyEffect.kt +++ b/murphy-core/src/main/kotlin/io/murphy/core/effect/LatencyEffect.kt @@ -1,14 +1,3 @@ package io.murphy.core.effect -import io.murphy.core.MurphyContext -import io.murphy.core.MurphyResponse - -internal class LatencyEffect(override val duration: Long) : DelayEffect() { - - override fun apply(context: MurphyContext): MurphyResponse? { - if (duration > 0) { - Thread.sleep(duration) - } - return null - } -} +internal class LatencyEffect(override val duration: Long) : DelayEffect() diff --git a/murphy-core/src/main/kotlin/io/murphy/core/effect/ProbabilisticDelayEffect.kt b/murphy-core/src/main/kotlin/io/murphy/core/effect/ProbabilisticDelayEffect.kt index 16dfdaf..0f53219 100644 --- a/murphy-core/src/main/kotlin/io/murphy/core/effect/ProbabilisticDelayEffect.kt +++ b/murphy-core/src/main/kotlin/io/murphy/core/effect/ProbabilisticDelayEffect.kt @@ -1,7 +1,5 @@ package io.murphy.core.effect -import io.murphy.core.MurphyContext -import io.murphy.core.MurphyResponse import java.util.concurrent.ThreadLocalRandom internal class ProbabilisticDelayEffect( @@ -10,17 +8,10 @@ internal class ProbabilisticDelayEffect( ) : DelayEffect() { override val duration: Long - get() = delegate.duration + get() { + val roll = ThreadLocalRandom.current().nextDouble() + return if (roll <= probability) delegate.duration else 0L + } override fun probability(): Double = probability - - override fun apply(context: MurphyContext): MurphyResponse? { - val roll = ThreadLocalRandom.current().nextDouble() - - return if (roll <= probability) { - delegate.apply(context) - } else { - null - } - } } diff --git a/murphy-core/src/test/kotlin/io/murphy/core/EffectsTest.kt b/murphy-core/src/test/kotlin/io/murphy/core/EffectsTest.kt index 24bc37a..ab08a8c 100644 --- a/murphy-core/src/test/kotlin/io/murphy/core/EffectsTest.kt +++ b/murphy-core/src/test/kotlin/io/murphy/core/EffectsTest.kt @@ -237,6 +237,5 @@ class EffectsTest { assertIs(effect) assertEquals(0.5, effect.probability()) - assertTrue(effect.duration in 10..20) } }