Skip to content

Commit

Permalink
fix: remove retries and simplify driver open logic
Browse files Browse the repository at this point in the history
  • Loading branch information
amanjeetsingh150 committed Nov 17, 2024
1 parent 17e67dc commit 622892b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 274 deletions.
1 change: 0 additions & 1 deletion maestro-client/src/main/java/maestro/Errors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,4 @@ sealed class MaestroException(override val message: String) : RuntimeException(m
sealed class MaestroDriverStartupException(override val message: String): RuntimeException() {
class AndroidDriverTimeoutException(message: String): MaestroDriverStartupException(message)
class AndroidInstrumentationSetupFailure(message: String): MaestroDriverStartupException(message)
class IOSDriverTimeoutException(message: String): MaestroDriverStartupException(message)
}
16 changes: 1 addition & 15 deletions maestro-client/src/main/java/maestro/drivers/IOSDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class IOSDriver(
}

override fun open() {
awaitLaunch()
iosDevice.open()
}

override fun close() {
Expand Down Expand Up @@ -459,20 +459,6 @@ class IOSDriver(
return runDeviceCall { iosDevice.isScreenStatic() }
}

private fun awaitLaunch() {
val startTime = System.currentTimeMillis()

while (System.currentTimeMillis() - startTime < getStartupTimeout()) {
runCatching {
iosDevice.open()
return
}
Thread.sleep(100)
}

throw IOSDriverTimeoutException("Maestro iOS driver did not start up in time")
}

private fun <T> runDeviceCall(call: () -> T): T {
return try {
call()
Expand Down
83 changes: 0 additions & 83 deletions maestro-ios-driver/src/main/kotlin/xcuitest/XCTestDriverClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import maestro.utils.network.XCUITestServerError
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody.Companion.toResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import org.slf4j.LoggerFactory
import xcuitest.api.*
import xcuitest.api.NetworkErrorHandler.Companion.RETRY_RESPONSE_CODE
import xcuitest.installer.XCTestInstaller
import java.io.IOException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.util.concurrent.TimeUnit

class XCTestDriverClient(
Expand All @@ -32,8 +27,6 @@ class XCTestDriverClient(

private var isShuttingDown = false

private val networkErrorHandler by lazy { NetworkErrorHandler(installer) }

init {
Runtime.getRuntime().addShutdownHook(Thread {
isShuttingDown = true
Expand All @@ -52,13 +45,6 @@ class XCTestDriverClient(
private val okHttpClient = OkHttpClient.Builder()
.connectTimeout(40, TimeUnit.SECONDS)
.readTimeout(100, TimeUnit.SECONDS)
.apply {
httpInterceptor?.let {
this.addInterceptor(it)
}
}
.addRetryInterceptor()
.addRetryAndShutdownInterceptor()
.build()

class XCTestDriverUnreachable(message: String) : IOException(message)
Expand Down Expand Up @@ -272,12 +258,6 @@ class XCTestDriverClient(
responseBodyAsString
)
}
code == NetworkErrorHandler.NO_RETRY_RESPONSE_CODE -> {
logger.error("Request for $pathString failed, because of XCUITest server got crashed/exit, body: $responseBodyAsString")
throw XCUITestServerError.NetworkError(
"Request for $pathString failed, because of XCUITest server got crashed/exit, body: $responseBodyAsString"
)
}
error.errorMessage.contains("Lost connection to the application.*".toRegex()) -> {
logger.error("Request for $pathString failed, because of app crash, body: $responseBodyAsString")
throw XCUITestServerError.AppCrash(
Expand Down Expand Up @@ -311,67 +291,4 @@ class XCTestDriverClient(
}
}

private fun OkHttpClient.Builder.addRetryInterceptor() = addInterceptor(Interceptor { chain ->
val response = try {
chain.proceed(chain.request())
} catch (ioException: IOException) {
val networkException = mapNetworkException(ioException)
return@Interceptor networkErrorHandler.retryConnection(chain, networkException) {
client = it
}
}

return@Interceptor when (response.code) {
RETRY_RESPONSE_CODE -> {
networkErrorHandler.retryConnection(chain.call(), response) {
logger.info("Reinitialized the xctest client after reestablishing connection")
client = it
}
}
else -> {
networkErrorHandler.resetRetryCount()
response
}
}
})

private fun OkHttpClient.Builder.addRetryAndShutdownInterceptor() = addNetworkInterceptor(Interceptor {
val request = it.request()
try {
it.proceed(request)
} catch (ioException: IOException) {
// Fake an Ok response when shutting down and receiving an error
// to prevent a stack trace in the cli when running maestro studio.

if (isShuttingDown) {
val message = "Shutting down xctest server"
val responseBody = """
{ "message" : "$message" }
""".trimIndent().toResponseBody("application/json; charset=utf-8".toMediaType())

Response.Builder()
.request(it.request())
.protocol(Protocol.HTTP_1_1)
.message(message)
.body(responseBody)
.code(200)
.build()
} else {
val networkException = mapNetworkException(ioException)
return@Interceptor networkErrorHandler.getRetrialResponse(networkException, request)
}
}
})

private fun mapNetworkException(e: IOException): NetworkException {
return when (e) {
is SocketTimeoutException -> NetworkException.TimeoutException("Socket timeout")
is ConnectException -> NetworkException.ConnectionException("Connection error")
is UnknownHostException -> NetworkException.UnknownHostException("Unknown host")
else -> {
logger.info("Exception $e is not mapped io exception")
NetworkException.UnknownNetworkException(e.message ?: e.stackTraceToString())
}
}
}
}
127 changes: 0 additions & 127 deletions maestro-ios-driver/src/main/kotlin/xcuitest/api/NetworkErrorHandler.kt

This file was deleted.

Loading

0 comments on commit 622892b

Please sign in to comment.