-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created RetryFlakyTestUntilSuccessRule class
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...loudApp/src/androidTest/java/com/owncloud/android/utils/RetryFlakyTestUntilSuccessRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.owncloud.android.utils | ||
|
||
import android.util.Log | ||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
class RetryFlakyTestUntilSuccessRule(val count: Int = 3) : TestRule { | ||
|
||
companion object { | ||
private const val TAG = "RetryFlakyTestUntilSuccessRule" | ||
} | ||
|
||
override fun apply(base: Statement, description: Description): Statement = statement(base, description) | ||
|
||
private fun statement(base: Statement, description: Description): Statement { | ||
return object : Statement() { | ||
@Throws(Throwable::class) | ||
override fun evaluate() { | ||
var throwable: Throwable? = null | ||
val displayName = description.displayName | ||
for (i in 1 until count + 1) { | ||
try { | ||
Log.i(TAG, "$displayName: Run $i") | ||
base.evaluate() | ||
return | ||
} catch (t: Throwable) { | ||
throwable = t | ||
Log.e(TAG, "$displayName: Run $i failed.") | ||
} | ||
} | ||
Log.e(TAG, "$displayName: Giving up after run $count failures.") | ||
throw throwable!! | ||
} | ||
} | ||
} | ||
} |