Skip to content

Commit

Permalink
Merge pull request #927 from MarathonLabs/fix/actor-execution-cancell…
Browse files Browse the repository at this point in the history
…ation-propagation

fix(vendor): propagate cancellation exception to execution of tests
  • Loading branch information
Malinskiy authored Apr 29, 2024
2 parents 8e21f3d + 7d180fe commit 36ab47c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class QueueActor(
}

for (test in uncompletedRetryQuotaExceeded) {
logger.debug { "uncompletedTestRetryQuota exceeded for ${test.test.toTestName()}}" }
logger.debug { "uncompletedTestRetryQuota exceeded for ${test.test.toTestName()}" }
val testAction = poolProgressAccumulator.testEnded(device, test, final = true)
processTestAction(testAction, test)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ import com.malinskiy.marathon.test.TestBatch
import com.malinskiy.marathon.time.Timer
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.newFixedThreadPoolContext
Expand Down Expand Up @@ -201,7 +203,14 @@ class AdamAndroidDevice(
try {
measureFileTransfer(File(localFilePath)) {
val channel = client.execute(
CompatPushFileRequest(file, remoteFilePath, supportedFeatures, coroutineScope = this, mode = "0777", coroutineContext = installContext),
CompatPushFileRequest(
file,
remoteFilePath,
supportedFeatures,
coroutineScope = this,
mode = "0777",
coroutineContext = installContext
),
serial = adbSerial
)
for (update in channel) {
Expand Down Expand Up @@ -409,17 +418,26 @@ class AdamAndroidDevice(
testBatch: TestBatch,
deferred: CompletableDeferred<TestBatchResults>
) {
var job: Job? = null
try {
async(coroutineContext) {
job = async(coroutineContext + CoroutineName("execute $serialNumber")) {
supervisorScope {
val listener = createExecutionListeners(configuration, devicePoolId, testBatch, deferred)
AndroidDeviceTestRunner(this@AdamAndroidDevice, testBundleIdentifier).execute(configuration, testBatch, listener)
AndroidDeviceTestRunner(this@AdamAndroidDevice, testBundleIdentifier).execute(
configuration,
testBatch,
listener
)
}
}.await()
}
job.await()
} catch (e: RequestRejectedException) {
throw DeviceLostException(e)
} catch (e: CommandRejectedException) {
throw DeviceLostException(e)
} catch (e: CancellationException) {
job?.cancel(e)
throw e
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TestResultsListener(
private val attachmentCollector: AttachmentCollector = AttachmentCollector(attachmentProviders),
) : AccumulatingResultTestRunListener(timer), AttachmentListener by attachmentCollector {

private val logger = MarathonLogging.logger("TestRunResultsListener")
private val logger = MarathonLogging.logger {}

override suspend fun afterTestRun() {
val results = mergeParameterisedResults(runResult.temporalTestResults)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ import com.malinskiy.marathon.report.attachment.AttachmentProvider
import com.malinskiy.marathon.report.logs.LogWriter
import com.malinskiy.marathon.test.TestBatch
import com.malinskiy.marathon.time.Timer
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
Expand Down Expand Up @@ -248,8 +250,9 @@ class AppleSimulatorDevice(
testBatch: TestBatch,
deferred: CompletableDeferred<TestBatchResults>
) {
var job: Job? = null
try {
async(coroutineContext + CoroutineName("execute $serialNumber")) {
job = async(coroutineContext + CoroutineName("execute $serialNumber")) {
supervisorScope {
var executionLineListeners = setOf<LineListener>()
try {
Expand All @@ -270,7 +273,8 @@ class AppleSimulatorDevice(
executionLineListeners.forEach { removeLineListener(it) }
}
}
}.await()
}
job.await()
} catch (e: ConnectionException) {
throw DeviceLostException(e)
} catch (e: TransportException) {
Expand All @@ -287,6 +291,9 @@ class AppleSimulatorDevice(

else -> throw DeviceLostException(e)
}
} catch(e: CancellationException) {
job?.cancel(e)
throw e
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.newFixedThreadPoolContext
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.withContext
import mu.KLogger
import net.schmizz.sshj.connection.ConnectionException
import net.schmizz.sshj.connection.channel.OpenFailException
import net.schmizz.sshj.transport.TransportException
Expand All @@ -74,6 +72,7 @@ import java.io.File
import java.time.Duration
import java.util.concurrent.CopyOnWriteArrayList
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.cancellation.CancellationException

class MacosDevice(
override val udid: String,
Expand Down Expand Up @@ -290,8 +289,9 @@ class MacosDevice(
testBatch: TestBatch,
deferred: CompletableDeferred<TestBatchResults>
) {
var job: Job? = null
try {
async(CoroutineName("execute $serialNumber")) {
job = async(coroutineContext + CoroutineName("execute $serialNumber")) {
supervisorScope {
var executionLineListeners = setOf<LineListener>()
try {
Expand All @@ -307,7 +307,8 @@ class MacosDevice(
executionLineListeners.forEach { removeLineListener(it) }
}
}
}.await()
}
job.await()
} catch (e: ConnectionException) {
throw DeviceLostException(e)
} catch (e: TransportException) {
Expand All @@ -324,6 +325,9 @@ class MacosDevice(

else -> throw DeviceLostException(e)
}
} catch(e: CancellationException) {
job?.cancel(e)
throw e
}
}

Expand Down Expand Up @@ -418,13 +422,14 @@ class MacosDevice(
}

suspend fun grant(client: String, permission: Set<Permission>) {
if (operatingSystem.major?.let { it > 10 } != true) {
if (operatingSystem.major?.let { it > 10 } != true) {
throw IncompatibleDeviceException("Modifying permissions is supported to macOS 10+")
}

val epoch = System.currentTimeMillis() / 1000
permission.forEach {
val query = "replace into access (service,client,client_type,auth_value,auth_reason,auth_version,indirect_object_identifier,flags,last_modified) values (\"${it.value}\",\"${client}\",0,2,1,1,\"UNUSED\",0,$epoch);"
val query =
"replace into access (service,client,client_type,auth_value,auth_reason,auth_version,indirect_object_identifier,flags,last_modified) values (\"${it.value}\",\"${client}\",0,2,1,1,\"UNUSED\",0,$epoch);"
binaryEnvironment.sqlite3.query("/Library/Application Support/com.apple.TCC/TCC.db", query, sudo = true)
binaryEnvironment.sqlite3.query("/Users/${env["USER"]}/Library/Application Support/com.apple.TCC/TCC.db", query, sudo = true)
}
Expand Down

0 comments on commit 36ab47c

Please sign in to comment.