Skip to content

Instantly share code, notes, and snippets.

@jaredsburrows
Created September 18, 2018 22:14
Show Gist options
  • Save jaredsburrows/b435725e055772c80f705849b94f74b1 to your computer and use it in GitHub Desktop.
Save jaredsburrows/b435725e055772c80f705849b94f74b1 to your computer and use it in GitHub Desktop.
Test Retry Rule
@Retention(AnnotationRetention.RUNTIME)
annotation class Retry(
val retryCount: Int = -1
)
import org.junit.rules.MethodRule
import org.junit.runners.model.FrameworkMethod
import org.junit.runners.model.Statement
class RetryRule : MethodRule {
override fun apply(base: Statement, method: FrameworkMethod, target: Any): Statement {
return object : Statement() {
override fun evaluate() {
try {
base.evaluate()
} catch (t: Throwable) {
val retry = method.getAnnotation(Retry::class.java)
if (retry != null) {
if (retry.retryCount > 0) {
var throwable = Throwable("Number of max retries reached: $retry.retryCount")
for (i in 0 until retry.retryCount) {
try {
base.evaluate()
return
} catch (t: Throwable) {
throwable = t
}
}
throw throwable
} else {
base.evaluate()
}
} else {
throw t
}
}
}
}
}
}
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
class RetryRuleTest {
@get:Rule var rule = RetryRule()
private var countForever = 0
private var countMax = 0
private var countMaxExceeded = 0
private var countFail = 0
@Test
@Retry
fun itRetriesUntilPasses() {
countForever++
assertEquals(2, countForever)
}
@Test
@Retry(4)
fun itRetriesUntilMaxRetriesExactly() {
countMax++
assertEquals(5, countMax)
}
@Test
@Retry(10)
fun itRetriesUntilMaxRetriesExceeded() {
countMaxExceeded++
assertEquals(5, countMaxExceeded)
}
@Test
@Retry(5)
fun itRetriesUntilMaxRetriesButTestStillFails() {
try {
countFail++
throw Exception("Fail the test")
} catch (e: Exception) {
assertEquals("Fail the test", e.message)
assertEquals(6, countFail)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment