Commit c958f8a Sergey Chelombitko
committed
1 parent 5bb61f1 commit c958f8a Copy full SHA for c958f8a
File tree 3 files changed +29
-23
lines changed
core/src/main/kotlin/com/malinskiy/marathon/execution
vendor/vendor-android/ddmlib/src/main/kotlin/com/malinskiy/marathon/android
3 files changed +29
-23
lines changed Original file line number Diff line number Diff line change 1
1
package com.malinskiy.marathon.execution
2
2
3
- import kotlinx.coroutines.delay
3
+ import kotlinx.coroutines.currentCoroutineContext
4
+ import kotlinx.coroutines.ensureActive
5
+ import kotlinx.coroutines.isActive
6
+ import kotlinx.coroutines.time.delay
7
+ import java.time.Duration
8
+
9
+ suspend fun withRetry (maxAttempts : Int , retryDelay : Duration , block : suspend () -> Unit ) {
10
+ check(maxAttempts >= 1 ) { " maxAttempts must be >= 1" }
4
11
5
- @Suppress(" TooGenericExceptionCaught" )
6
- suspend fun <T > withRetry (attempts : Int , delayTime : Long = 0, f : suspend () -> T ): T {
7
12
var attempt = 1
8
- while (true ) {
13
+ while (currentCoroutineContext().isActive ) {
9
14
try {
10
- return f()
11
- } catch (th: Throwable ) {
12
- if (attempt == attempts) {
13
- throw th
15
+ return block()
16
+ } catch (@Suppress(" TooGenericExceptionCaught" ) e: Throwable ) {
17
+ currentCoroutineContext().ensureActive()
18
+ if (attempt == maxAttempts) {
19
+ throw e
14
20
} else {
15
- delay(delayTime )
21
+ delay(retryDelay )
16
22
}
17
23
}
18
24
++ attempt
Original file line number Diff line number Diff line change @@ -21,8 +21,8 @@ import kotlinx.coroutines.CompletionHandler
21
21
import kotlinx.coroutines.Job
22
22
import kotlinx.coroutines.async
23
23
import kotlinx.coroutines.channels.SendChannel
24
- import kotlinx.coroutines.isActive
25
24
import kotlinx.coroutines.launch
25
+ import java.time.Duration
26
26
import java.time.Instant
27
27
import kotlin.coroutines.CoroutineContext
28
28
@@ -157,18 +157,16 @@ class DeviceActor(
157
157
158
158
private fun initialize () {
159
159
logger.debug(" [{}] Initializing" , device.serialNumber)
160
- job = scope.async {
160
+ job = scope.launch {
161
161
try {
162
- withRetry(30 , 10000 ) {
163
- if (isActive) {
164
- try {
165
- device.prepare(configuration)
166
- } catch (e: CancellationException ) {
167
- throw e
168
- } catch (@Suppress(" TooGenericExceptionCaught" ) e: Exception ) {
169
- logger.debug(" [{}] Initialization failed. Retrying" , device.serialNumber, e)
170
- throw e
171
- }
162
+ withRetry(maxAttempts = 30 , retryDelay = Duration .ofSeconds(10 )) {
163
+ try {
164
+ device.prepare(configuration)
165
+ } catch (e: CancellationException ) {
166
+ throw e
167
+ } catch (@Suppress(" TooGenericExceptionCaught" ) e: Exception ) {
168
+ logger.debug(" [{}] Initialization failed. Retrying" , device.serialNumber, e)
169
+ throw e
172
170
}
173
171
}
174
172
state.transition(DeviceEvent .Complete )
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import com.malinskiy.marathon.execution.withRetry
7
7
import com.malinskiy.marathon.io.FileHasher
8
8
import com.malinskiy.marathon.log.MarathonLogging
9
9
import java.io.File
10
+ import java.time.Duration
10
11
import java.time.Instant
11
12
import kotlin.system.measureTimeMillis
12
13
@@ -39,7 +40,7 @@ class AndroidAppInstaller(
39
40
40
41
@Suppress(" TooGenericExceptionThrown" )
41
42
private suspend fun ensureInstalled (device : AndroidDevice , appPackage : String , appApk : File ) {
42
- withRetry(attempts = MAX_RETIRES , delayTime = 1000 ) {
43
+ withRetry(maxAttempts = MAX_INSTALLATION_ATTEMPTS , retryDelay = INSTALLATION_RETRY_DELAY ) {
43
44
try {
44
45
val checkStarted = Instant .now()
45
46
val fileHash = fileHasher.getHash(appApk)
@@ -138,7 +139,8 @@ class AndroidAppInstaller(
138
139
}
139
140
140
141
companion object {
141
- private const val MAX_RETIRES = 3
142
+ private const val MAX_INSTALLATION_ATTEMPTS = 3
143
+ private val INSTALLATION_RETRY_DELAY = Duration .ofSeconds(1 )
142
144
private const val MARSHMALLOW_VERSION_CODE = 23
143
145
private const val MD5_HASH_SIZE = 32
144
146
private const val INSTALLED_TEST_APPS_SCRIPT = " pm list packages -3 | grep -E '\\ .test\$ ' | tr -d '\\ r' | cut -d ':' -f 2"
You can’t perform that action at this time.
0 commit comments