-
Notifications
You must be signed in to change notification settings - Fork 281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-restart xctest runner and idb_companion on connection error #882
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c42868
Auto-restart xctest runner on connection error
berikv e879513
Auto-restart idb_companion on connection error
berikv 070fc45
Correctly silence error stack traces in XCTest Runner when shutting d…
berikv 6e97409
Simplify returnOkOnShutdown network interceptor
berikv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
maestro-cli/src/main/java/maestro/cli/idb/IdbCompanion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package maestro.cli.idb | ||
|
||
import com.github.michaelbull.result.Ok | ||
import com.github.michaelbull.result.Result | ||
import com.github.michaelbull.result.runCatching | ||
import idb.CompanionServiceGrpc | ||
import idb.HIDEventKt | ||
import idb.Idb | ||
import idb.hIDEvent | ||
import idb.point | ||
import io.grpc.ManagedChannel | ||
import io.grpc.ManagedChannelBuilder | ||
import ios.grpc.BlockingStreamObserver | ||
import maestro.cli.device.Device | ||
import maestro.debuglog.DebugLogStore | ||
import maestro.utils.MaestroTimer | ||
import java.net.Socket | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.concurrent.thread | ||
|
||
object IdbCompanion { | ||
private val logger = DebugLogStore.loggerFor(IdbCompanion::class.java) | ||
|
||
// TODO: Understand why this is a separate method from strartIdbCompanion | ||
fun setup(device: Device.Connected) { | ||
val idbProcessBuilder = ProcessBuilder("idb_companion", "--udid", device.instanceId) | ||
idbProcessBuilder.start() | ||
|
||
val idbHost = "localhost" | ||
val idbPort = 10882 | ||
MaestroTimer.retryUntilTrue(timeoutMs = 30000, delayMs = 100) { | ||
Socket(idbHost, idbPort).use { true } | ||
} | ||
} | ||
|
||
fun startIdbCompanion(host: String, port: Int, deviceId: String): ManagedChannel { | ||
logger.info("startIDBCompanion on $deviceId") | ||
|
||
// idb is associated with a device, it can't be assumed that a running idb_companion is | ||
// associated with the device under test: Shut down before starting a fresh idb if needed. | ||
if (isSocketAvailable(host, port)) { | ||
ProcessBuilder(listOf("killall", "idb_companion")).start().waitFor() | ||
} | ||
|
||
val idbProcessBuilder = ProcessBuilder("idb_companion", "--udid", deviceId) | ||
DebugLogStore.logOutputOf(idbProcessBuilder) | ||
val idbProcess = idbProcessBuilder.start() | ||
|
||
Runtime.getRuntime().addShutdownHook(thread(start = false) { | ||
idbProcess.destroy() | ||
}) | ||
|
||
logger.warning("Waiting for idb service to start..") | ||
MaestroTimer.retryUntilTrue(timeoutMs = 60000, delayMs = 100) { | ||
Socket(host, port).use { true } | ||
} || error("idb_companion did not start in time") | ||
|
||
|
||
// The first time a simulator boots up, it can | ||
// take 10's of seconds to complete. | ||
logger.warning("Waiting for Simulator to boot..") | ||
MaestroTimer.retryUntilTrue(timeoutMs = 120000, delayMs = 100) { | ||
val process = ProcessBuilder("xcrun", "simctl", "bootstatus", deviceId) | ||
.start() | ||
process | ||
.waitFor(1000, TimeUnit.MILLISECONDS) | ||
process.exitValue() == 0 | ||
} || error("Simulator failed to boot") | ||
|
||
val channel = ManagedChannelBuilder.forAddress(host, port) | ||
.usePlaintext() | ||
.build() | ||
|
||
// Test if idb can get accessibility info elements with non-zero frame width | ||
logger.warning("Waiting for successful taps") | ||
MaestroTimer.retryUntilTrue(timeoutMs = 20000, delayMs = 100) { | ||
testPressAction(channel) is Ok | ||
} || error("idb_companion is not able dispatch successful tap events") | ||
|
||
logger.warning("Simulator ready") | ||
|
||
return channel | ||
} | ||
|
||
private fun testPressAction(channel: ManagedChannel): Result<Unit, Throwable> { | ||
val x = 0 | ||
val y = 0 | ||
val holdDelay = 50L | ||
val asyncStub = CompanionServiceGrpc.newStub(channel) | ||
|
||
return runCatching { | ||
val responseObserver = BlockingStreamObserver<Idb.HIDResponse>() | ||
val stream = asyncStub.hid(responseObserver) | ||
|
||
val pressAction = HIDEventKt.hIDPressAction { | ||
touch = HIDEventKt.hIDTouch { | ||
point = point { | ||
this.x = x.toDouble() | ||
this.y = y.toDouble() | ||
} | ||
} | ||
} | ||
|
||
stream.onNext( | ||
hIDEvent { | ||
press = HIDEventKt.hIDPress { | ||
action = pressAction | ||
direction = Idb.HIDEvent.HIDDirection.DOWN | ||
} | ||
} | ||
) | ||
|
||
Thread.sleep(holdDelay) | ||
|
||
stream.onNext( | ||
hIDEvent { | ||
press = HIDEventKt.hIDPress { | ||
action = pressAction | ||
direction = Idb.HIDEvent.HIDDirection.UP | ||
} | ||
} | ||
) | ||
stream.onCompleted() | ||
|
||
responseObserver.awaitResult() | ||
} | ||
} | ||
|
||
|
||
private fun isSocketAvailable(host: String, port: Int): Boolean { | ||
return try { | ||
Socket(host, port).use { true } | ||
} catch (_: Exception) { | ||
false | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amanjeetsingh150 Do you know the details on why this was implemented differently than
startIdbCompanion
?