Skip to content

Commit dc6933e

Browse files
committed
feat: created RetryFlakyTestUntilSuccessRule class
1 parent ca7c7ba commit dc6933e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

owncloudApp/src/androidTest/java/com/owncloud/android/files/details/FileDetailsFragmentTest.kt

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AND_SPACE
2020
import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AND_WITHOUT_PERSONAL_SPACE
2121
import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AVAILABLE_OFFLINE
2222
import com.owncloud.android.utils.DisplayUtils
23+
import com.owncloud.android.utils.RetryFlakyTestUntilSuccessRule
2324
import com.owncloud.android.utils.matchers.assertVisibility
2425
import com.owncloud.android.utils.matchers.isDisplayed
2526
import com.owncloud.android.utils.matchers.withDrawable
@@ -29,6 +30,7 @@ import io.mockk.mockk
2930
import kotlinx.coroutines.flow.MutableStateFlow
3031
import org.junit.Before
3132
import org.junit.Ignore
33+
import org.junit.Rule
3234
import org.junit.Test
3335
import org.koin.androidx.viewmodel.dsl.viewModel
3436
import org.koin.core.context.startKoin
@@ -47,6 +49,10 @@ class FileDetailsFragmentTest {
4749
private var currentFileSyncInfo: MutableStateFlow<OCFileWithSyncInfo?> = MutableStateFlow(OC_FILE_WITH_SYNC_INFO)
4850
private var currentFileAvailableOffline: MutableStateFlow<OCFileWithSyncInfo?> = MutableStateFlow(OC_FILE_WITH_SYNC_INFO_AVAILABLE_OFFLINE)
4951

52+
@Rule
53+
@JvmField
54+
val retryFlakyTestUntilSuccessRule = RetryFlakyTestUntilSuccessRule()
55+
5056
@Before
5157
fun setUp() {
5258
context = ApplicationProvider.getApplicationContext()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.owncloud.android.utils
2+
3+
import android.util.Log
4+
import org.junit.rules.TestRule
5+
import org.junit.runner.Description
6+
import org.junit.runners.model.Statement
7+
8+
class RetryFlakyTestUntilSuccessRule(val count: Int = 3) : TestRule {
9+
10+
companion object {
11+
private const val TAG = "RetryFlakyTestUntilSuccessRule"
12+
}
13+
14+
override fun apply(base: Statement, description: Description): Statement = statement(base, description)
15+
16+
private fun statement(base: Statement, description: Description): Statement {
17+
return object : Statement() {
18+
@Throws(Throwable::class)
19+
override fun evaluate() {
20+
var throwable: Throwable? = null
21+
val displayName = description.displayName
22+
for (i in 1 until count + 1) {
23+
try {
24+
Log.i(TAG, "$displayName: Run $i")
25+
base.evaluate()
26+
return
27+
} catch (t: Throwable) {
28+
throwable = t
29+
Log.e(TAG, "$displayName: Run $i failed.")
30+
}
31+
}
32+
Log.e(TAG, "$displayName: Giving up after run $count failures.")
33+
throw throwable!!
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)