Skip to content

Commit

Permalink
fix the bug
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Sep 2, 2024
1 parent b2d6960 commit adf7b82
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
55 changes: 33 additions & 22 deletions maestro-cli/src/main/java/maestro/cli/command/TestCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,34 +151,36 @@ class TestCommand : Callable<Int> {
}

override fun call(): Int {
TestDebugReporter.install(
debugOutputPathAsString = debugOutput,
flattenDebugOutput = flattenDebugOutput,
printToConsole = parent?.verbose == true,
)

if (parent?.platform != null) {
throw CliError("--platform option was deprecated. You can remove it to run your test.")
}

val executionPlan = try {
WorkspaceExecutionPlanner.plan(
flowFile.toPath().toAbsolutePath(),
includeTags,
excludeTags
input = flowFile.toPath().toAbsolutePath(),
includeTags = includeTags,
excludeTags = excludeTags,
)
} catch (e: ValidationError) {
throw CliError(e.message)
}

env = env.withInjectedShellEnvVars()

TestDebugReporter.install(
debugOutputPathAsString = debugOutput,
flattenDebugOutput = flattenDebugOutput,
printToConsole = parent?.verbose == true,
)
val debugOutputPath = TestDebugReporter.getDebugOutputPath()

return handleSessions(debugOutputPath, executionPlan)
}

private fun handleSessions(debugOutputPath: Path, plan: ExecutionPlan): Int = runBlocking(Dispatchers.IO) {
val sharded = shards > 1
val onlySequenceFlows = plan.sequence.flows.isNotEmpty() && plan.flowsToRun.isEmpty() // An edge case

runCatching {
val deviceIds = (if (isWebFlow())
Expand All @@ -195,23 +197,32 @@ class TestCommand : Callable<Int> {
it.instanceId
}.toMutableSet())

if (shards > 1 && plan.sequence?.flows?.isNotEmpty() == true) {
if (sharded && plan.sequence.flows.isNotEmpty()) {
error("Cannot run sharded tests with sequential execution")
}

val effectiveShards = shards.coerceAtMost(plan.flowsToRun.size)
val chunkPlans = plan.flowsToRun
.withIndex()
.groupBy { it.index % shards }
.map { (shardIndex, files) ->
ExecutionPlan(
files.map { it.value },
plan.sequence.also {
if (it?.flows?.isNotEmpty() == true && sharded)
error("Cannot run sharded tests with sequential execution.")
}
)
}
if (sharded) {
PrintUtils.info("Requested to run ${plan.flowsToRun.size} flows in $shards shard(s)")
}

val effectiveShards = if (onlySequenceFlows) 1 else shards.coerceAtMost(plan.flowsToRun.size)
val chunkPlans: List<ExecutionPlan> = if (onlySequenceFlows) {
// Handle an edge case
// We only want to run sequential flows in this case.
listOf(plan)
} else {
plan.flowsToRun
.withIndex()
.groupBy { it.index % shards }
.map { (shardIndex, files) ->
ExecutionPlan(
flowsToRun = files.map { it.value },
sequence = plan.sequence,
)
}
}

PrintUtils.info("Will run ${if (onlySequenceFlows) plan.sequence.flows.size else plan.flowsToRun.size} flows in $effectiveShards shard(s)")

// Collect device configurations for missing shards, if any
val missing = effectiveShards - if (deviceIds.isNotEmpty()) deviceIds.size else initialActiveDevices.size
Expand Down
10 changes: 10 additions & 0 deletions maestro-cli/src/main/java/maestro/cli/util/PrintUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import kotlin.system.exitProcess

object PrintUtils {

fun info(message: String, bold: Boolean = false, newline: Boolean = true) {
val function: (Any) -> Unit = if (newline) ::println else ::print
function(
Ansi.ansi()
.bold(apply = bold)
.render(message)
.boldOff()
)
}

fun message(message: String) {
println(Ansi.ansi().render("@|cyan \n$message|@"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ object WorkspaceExecutionPlanner {
validateFlowFile(input)
return ExecutionPlan(
flowsToRun = listOf(input),
sequence = FlowSequence(emptyList()),
)
}

Expand Down Expand Up @@ -177,6 +178,6 @@ object WorkspaceExecutionPlanner {

data class ExecutionPlan(
val flowsToRun: List<Path>,
val sequence: FlowSequence? = null,
val sequence: FlowSequence,
)
}

0 comments on commit adf7b82

Please sign in to comment.