Skip to content
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
10 changes: 10 additions & 0 deletions murphy-core/src/main/kotlin/io/murphy/core/effect/DelayEffect.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 0 additions & 10 deletions murphy-core/src/main/kotlin/io/murphy/core/effect/JitterEffect.kt
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
}
}
}
1 change: 0 additions & 1 deletion murphy-core/src/test/kotlin/io/murphy/core/EffectsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,5 @@ class EffectsTest {

assertIs<ProbabilisticDelayEffect>(effect)
assertEquals(0.5, effect.probability())
assertTrue(effect.duration in 10..20)
}
}