Malinskiy/adam

Can you provide a simple example about coroutines ?

Closed this issue · 5 comments

I'm learning java and kotlin, for Android automated testing. But my understanding of coroutines is limited, and my code is blocked and unable to exit after running in idea. Can you give me some advice?

My code:

package my.demo

import com.malinskiy.adam.AndroidDebugBridgeClient
import com.malinskiy.adam.AndroidDebugBridgeClientFactory
import com.malinskiy.adam.request.misc.GetAdbServerVersionRequest
import kotlinx.coroutines.runBlocking


fun main() {
    runBlocking {
        val adb: AndroidDebugBridgeClient = AndroidDebugBridgeClientFactory().build()
        val actual = adb.execute(GetAdbServerVersionRequest())
        println("actual: $actual")
    }

    val expected = ProcessBuilder("adb", "version")
        .start().inputStream.bufferedReader().readText()
    val expectedString = expected.lines().first().substring("Android Debug Bridge version ".length)
    val expectedInt = expectedString.split(".")[2].toInt()
    println("expectedInt: $expectedInt")

}

I'm running on jdk11 + maven 3.8.1 + IntelliJ IDEA 2022.3.2 (Community Edition)。
image

The threads spawned by the default Vertx socket factory are not daemon threads, so they will block exit from the process unless they are explicitly stopped. All the socket factories can be closed using close()

fun main() {
    runBlocking {
        val socketFactory = VertxSocketFactory()
        val adb: AndroidDebugBridgeClient = AndroidDebugBridgeClientFactory().apply {
            this.socketFactory = socketFactory
        }.build()
        val actual = adb.execute(GetAdbServerVersionRequest())
        println("actual: $actual")
        socketFactory.close()
    }

    val expected = ProcessBuilder("adb", "version")
        .start().inputStream.bufferedReader().readText()
    val expectedString = expected.lines().first().substring("Android Debug Bridge version ".length)
    val expectedInt = expectedString.split(".")[2].toInt()
    println("expectedInt: $expectedInt")
}
java.lang.IllegalStateException: Already undeployed
	at io.vertx.core.impl.DeploymentManager$DeploymentImpl.doUndeploy(DeploymentManager.java:322)
	at io.vertx.core.impl.DeploymentManager.undeployVerticle(DeploymentManager.java:82)
	at io.vertx.core.impl.DeploymentManager.undeployAll(DeploymentManager.java:109)
	at io.vertx.core.impl.VertxImpl.lambda$close$15(VertxImpl.java:594)
	at io.vertx.core.impl.future.FutureImpl$3.onSuccess(FutureImpl.java:141)
	at io.vertx.core.impl.future.FutureBase.emitSuccess(FutureBase.java:60)
	at io.vertx.core.impl.future.FutureImpl.addListener(FutureImpl.java:196)
	at io.vertx.core.impl.future.PromiseImpl.addListener(PromiseImpl.java:23)
	at io.vertx.core.impl.future.FutureImpl.onComplete(FutureImpl.java:164)
	at io.vertx.core.impl.future.PromiseImpl.onComplete(PromiseImpl.java:23)
	at io.vertx.core.impl.VertxImpl.close(VertxImpl.java:593)
	at io.vertx.core.impl.VertxImpl.close(VertxImpl.java:562)
	at com.malinskiy.adam.transport.vertx.VertxSocketFactory.close(VertxSocketFactory.kt:49)
	at MyTestKt.main(MyTest.kt:33)
	at MyTestKt.main(MyTest.kt)

The close method raises an error

No clue why, but it still cleans up the sockets which are represented via vertices. The operation is not idempotent though in terms of errors. You can filter these via regular logging methods in Java

Got it, thanks for the answer.