Skip to content

Commit

Permalink
feat: created RetryFlakyTestUntilSuccessRule class
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitorbp committed Apr 25, 2024
1 parent ca7c7ba commit d8dce5c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AND_SPACE
import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AND_WITHOUT_PERSONAL_SPACE
import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AVAILABLE_OFFLINE
import com.owncloud.android.utils.DisplayUtils
import com.owncloud.android.utils.RetryFlakyTestUntilSuccessRule
import com.owncloud.android.utils.matchers.assertVisibility
import com.owncloud.android.utils.matchers.isDisplayed
import com.owncloud.android.utils.matchers.withDrawable
Expand All @@ -29,6 +30,7 @@ import io.mockk.mockk
import kotlinx.coroutines.flow.MutableStateFlow
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.context.startKoin
Expand All @@ -47,6 +49,10 @@ class FileDetailsFragmentTest {
private var currentFileSyncInfo: MutableStateFlow<OCFileWithSyncInfo?> = MutableStateFlow(OC_FILE_WITH_SYNC_INFO)
private var currentFileAvailableOffline: MutableStateFlow<OCFileWithSyncInfo?> = MutableStateFlow(OC_FILE_WITH_SYNC_INFO_AVAILABLE_OFFLINE)

@Rule
@JvmField
val retryFlakyTestUntilSuccessRule = RetryFlakyTestUntilSuccessRule()

@Before
fun setUp() {
context = ApplicationProvider.getApplicationContext()
Expand Down
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!!
}
}
}
}

0 comments on commit d8dce5c

Please sign in to comment.