Skip to content

Commit

Permalink
Stop waiting for device to boot when the device gets disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Chelombitko committed Dec 30, 2024
1 parent 891043e commit e9e7cc1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,19 @@ class Track : Tracker {

suspend fun trackDevicePreparing(device: Device, block: suspend () -> Unit) {
val start = Instant.now()
block.invoke()
val finish = Instant.now()

devicePreparing(device.serialNumber, start, finish)
try {
block()
} finally {
devicePreparing(device.serialNumber, start, Instant.now())
}
}

suspend fun trackProviderDevicePreparing(device: Device, block: suspend () -> Unit) {
val start = Instant.now()
block.invoke()
val finish = Instant.now()

deviceProviderInit(device.serialNumber, start, finish)
try {
block()
} finally {
deviceProviderInit(device.serialNumber, start, Instant.now())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.time.delay
import kotlinx.coroutines.time.withTimeout
import java.awt.image.BufferedImage
import java.io.File
import java.io.IOException
import java.time.Duration
import java.util.UUID
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -228,7 +233,7 @@ class DdmlibAndroidDevice(
}
}

val booted: Boolean
private val booted: Boolean
get() = ddmsDevice.getProperty("sys.boot_completed") != null

override val serialNumber: String = when {
Expand Down Expand Up @@ -355,6 +360,25 @@ class DdmlibAndroidDevice(
coroutineScope.cancel()
}

suspend fun waitForBoot() {
try {
withTimeout(WAIT_FOR_BOOT_TIMEOUT) {
while (isActive && ddmsDevice.state != IDevice.DeviceState.DISCONNECTED) {
if (booted) {
logger.debug("[{}] Booted", serialNumber)
break
} else {
delay(CHECK_BOOTED_INTERVAL)
logger.debug("[{}] Still booting...", serialNumber)
}
}
}
} catch (e: TimeoutCancellationException) {
logger.warn("[{}] Timeout waiting for device to boot", serialNumber)
throw e
}
}

private fun prepareRecorderListener(
feature: DeviceFeature,
attachmentProviders: MutableList<AttachmentProvider>
Expand Down Expand Up @@ -383,5 +407,7 @@ class DdmlibAndroidDevice(

private companion object {
private const val SERVICE_LOGS_TAG = "marathon"
private val WAIT_FOR_BOOT_TIMEOUT = Duration.ofSeconds(30)
private val CHECK_BOOTED_INTERVAL = Duration.ofSeconds(1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.time.Duration
import java.util.concurrent.ConcurrentHashMap
Expand Down Expand Up @@ -182,34 +180,9 @@ class DdmlibDeviceProvider(
}

private suspend fun verifyBooted(device: DdmlibAndroidDevice) {
if (!waitForBoot(device)) {
throw TimeoutException("Timeout waiting for device ${device.serialNumber} to boot")
}
}

private suspend fun waitForBoot(device: DdmlibAndroidDevice): Boolean {
var booted = false

track.trackProviderDevicePreparing(device) {
@Suppress("UnusedPrivateProperty")
for (i in 1..30) {
if (device.booted) {
logger.debug("[{}] Booted", device.serialNumber)
booted = true
break
} else {
delay(1000)
logger.debug("[{}] Still booting...", device.serialNumber)
}

if (Thread.interrupted() || !currentCoroutineContext().isActive) {
booted = true
break
}
}
device.waitForBoot()
}

return booted
}

private suspend fun notifyConnected(device: DdmlibAndroidDevice) {
Expand Down

0 comments on commit e9e7cc1

Please sign in to comment.