All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avito.android.runner.devices.internal.AbstractDevice.kt Maven / Gradle / Ivy

Go to download

Collection of infrastructure libraries and gradle plugins of Avito Android project

There is a newer version: 2024.32
Show newest version
package com.avito.android.runner.devices.internal

import com.avito.android.Result
import com.avito.android.waiter.waitForCondition
import com.avito.logger.LoggerFactory
import com.avito.logger.create
import com.avito.runner.service.worker.device.adb.Adb
import com.avito.utils.ProcessRunner
import kotlinx.coroutines.delay
import java.io.File
import java.time.Duration

internal abstract class AbstractDevice(
    protected val loggerFactory: LoggerFactory,
    protected val processRunner: ProcessRunner,
) : Device {

    private val logger = loggerFactory.create()

    protected abstract val adb: Adb

    override fun redirectLogcatToFile(
        file: File,
        tags: Collection
    ) {
        val tagsString = if (tags.isEmpty()) {
            ""
        } else {
            " ${tags.joinToString(" ")}"
        }

        // TODO stop endless process
        processRunner.spawn(
            command = "$adb -s $serial logcat$tagsString",
            outputTo = file
        )
    }

    protected suspend fun isBootCompleted() =
        waitForCommand(
            command = {
                processRunner.run(
                    command = "$adb -s $serial $CHECK_BOOT_COMPLETED_COMMAND",
                    timeout = Duration.ofSeconds(10)
                )
            }
        )

    protected suspend fun  waitForCommand(
        command: suspend () -> Result,
        attempts: Int = 12,
        frequencySec: Long = 5,
        timeoutSec: Long = 60
    ) = waitForCondition(
        conditionName = "Wait device with serial: $serial",
        maxAttempts = attempts,
        onSuccess = { conditionName: String, durationMs: Long, attempt: Int ->
            logger.verbose("$conditionName succeed in $durationMs at attempt=$attempt")
        },
        sleepAction = { frequencyMs: Long -> delay(frequencyMs) },
        frequencySeconds = frequencySec,
        timeoutSec = timeoutSec,
        condition = command
    )
}

private const val CHECK_BOOT_COMPLETED_COMMAND = "shell getprop sys.boot_completed"




© 2015 - 2024 Weber Informatics LLC | Privacy Policy