forked from orgzly-revived/orgzly-android-revived
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retry test rule and tidy up more between tests
- Loading branch information
Showing
4 changed files
with
69 additions
and
10 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
42 changes: 42 additions & 0 deletions
42
app/src/androidTest/java/com/orgzly/android/RetryTestRule.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,42 @@ | ||
package com.orgzly.android | ||
|
||
import android.util.Log | ||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
/** | ||
* Retry test rule used to retry test that failed. Retry failed test 3 times | ||
*/ | ||
class RetryTestRule(val retryCount: Int = 3) : TestRule { | ||
|
||
private val TAG = RetryTestRule::class.java.simpleName | ||
|
||
override fun apply(base: Statement, description: Description): Statement { | ||
return statement(base, description) | ||
} | ||
|
||
private fun statement(base: Statement, description: Description): Statement { | ||
return object : Statement() { | ||
@Throws(Throwable::class) | ||
override fun evaluate() { | ||
var caughtThrowable: Throwable? = null | ||
|
||
for (i in 0 until retryCount) { | ||
try { | ||
base.evaluate() | ||
return | ||
} catch (t: Throwable) { | ||
caughtThrowable = t | ||
Log.e(TAG, description.displayName + ": run " + (i + 1) + " failed") | ||
val sleep = 1000 * (i + 1).toLong() // Increase sleep time with each failed attempt | ||
Thread.sleep(sleep) | ||
} | ||
} | ||
|
||
Log.e(TAG, description.displayName + ": giving up after " + retryCount + " failures") | ||
throw caughtThrowable!! | ||
} | ||
} | ||
} | ||
} |
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
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