From 3c1e1bea2b98dd7ab71013bcab907082a4c15654 Mon Sep 17 00:00:00 2001 From: Damian Cudzik Date: Tue, 12 Sep 2023 16:18:48 +0200 Subject: [PATCH] Improve timeouts --- .../pl/leancode/patrol/PatrolAppServiceClient.kt | 7 +++++-- .../patrol/contracts/PatrolAppServiceClient.kt | 6 +++--- .../AutomatorServer/PatrolAppServiceClient.swift | 10 ++++++---- .../ios/Classes/ObjCPatrolAppServiceClient.swift | 4 +++- .../src/native/contracts/native_automator_client.dart | 2 +- packages/patrol/lib/src/native/patrol_app_service.dart | 3 ++- .../android/android_http4k_client_generator.dart | 6 +++--- .../generators/dart/dart_http_client_generator.dart | 2 +- .../ios/ios_url_session_client_generator.dart | 10 ++++++---- 9 files changed, 30 insertions(+), 20 deletions(-) diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt index 10e33c7eb..e5db7780d 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt @@ -1,5 +1,6 @@ package pl.leancode.patrol; +import org.apache.hc.core5.util.Timeout import pl.leancode.patrol.contracts.Contracts import pl.leancode.patrol.contracts.PatrolAppServiceClientException import pl.leancode.patrol.contracts.PatrolAppServiceClient as Client @@ -10,14 +11,16 @@ import pl.leancode.patrol.contracts.PatrolAppServiceClient as Client class PatrolAppServiceClient { private var client: Client + // https://github.com/leancodepl/patrol/issues/1683 + private val timeout = Timeout.ofHours(2) constructor() { - client = Client(address = "localhost", port = 8082) + client = Client(address = "localhost", port = 8082, timeout = timeout) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } constructor(address: String) { - client = Client(address = address, port = 8082) + client = Client(address = address, port = 8082, timeout = timeout) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt index 3e29dfb1f..f1a9b8c7a 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/contracts/PatrolAppServiceClient.kt @@ -15,7 +15,7 @@ import org.http4k.core.Method import org.http4k.core.Request import org.http4k.core.Status -class PatrolAppServiceClient(private val address: String, private val port: Int) { +class PatrolAppServiceClient(private val address: String, private val port: Int, private val timeout: Timeout) { fun listDartTests() : Contracts.ListDartTestsResponse { val response = performRequest("listDartTests") @@ -37,8 +37,8 @@ class PatrolAppServiceClient(private val address: String, private val port: Int) HttpClients.custom().setDefaultRequestConfig( RequestConfig .copy(RequestConfig.DEFAULT) - .setResponseTimeout(Timeout.ofSeconds(300)) - .setConnectionRequestTimeout(Timeout.ofSeconds(300)) + .setResponseTimeout(timeout) + .setConnectionRequestTimeout(timeout) .build() ).build()) diff --git a/packages/patrol/ios/Classes/AutomatorServer/PatrolAppServiceClient.swift b/packages/patrol/ios/Classes/AutomatorServer/PatrolAppServiceClient.swift index 321fc2d86..a7bc419ad 100644 --- a/packages/patrol/ios/Classes/AutomatorServer/PatrolAppServiceClient.swift +++ b/packages/patrol/ios/Classes/AutomatorServer/PatrolAppServiceClient.swift @@ -6,10 +6,12 @@ class PatrolAppServiceClient { private let port: Int private let address: String + private let timeout: TimeInterval - init(port: Int, address: String) { + init(port: Int, address: String, timeout: TimeInterval) { self.port = port self.address = address + self.timeout = timeout } func listDartTests() async throws -> ListDartTestsResponse { @@ -25,13 +27,13 @@ class PatrolAppServiceClient { let url = URL(string: "http://\(address):\(port)/\(requestName)")! let urlconfig = URLSessionConfiguration.default - urlconfig.timeoutIntervalForRequest = TimeInterval(300) - urlconfig.timeoutIntervalForResource = TimeInterval(300) + urlconfig.timeoutIntervalForRequest = timeout + urlconfig.timeoutIntervalForResource = timeout var request = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = body - request.timeoutInterval = TimeInterval(300) + request.timeoutInterval = timeout let (data, response) = try await URLSession(configuration: urlconfig).data(for: request) guard (response as? HTTPURLResponse)?.statusCode == 200 else { diff --git a/packages/patrol/ios/Classes/ObjCPatrolAppServiceClient.swift b/packages/patrol/ios/Classes/ObjCPatrolAppServiceClient.swift index e16546de8..1b5fa086a 100644 --- a/packages/patrol/ios/Classes/ObjCPatrolAppServiceClient.swift +++ b/packages/patrol/ios/Classes/ObjCPatrolAppServiceClient.swift @@ -21,10 +21,12 @@ /// Construct client for accessing PatrolAppService server using the existing channel. @objc public override init() { let port = 8082 // TODO: Document this value better + // https://github.com/leancodepl/patrol/issues/1683 + let timeout = TimeInterval(2*60*60) NSLog("PatrolAppServiceClient: created, port: \(port)") - client = PatrolAppServiceClient(port: port, address: "localhost") + client = PatrolAppServiceClient(port: port, address: "localhost", timeout: timeout) } @objc public func listDartTests() async throws -> [String] { diff --git a/packages/patrol/lib/src/native/contracts/native_automator_client.dart b/packages/patrol/lib/src/native/contracts/native_automator_client.dart index f9aa3b891..0956fe8d8 100644 --- a/packages/patrol/lib/src/native/contracts/native_automator_client.dart +++ b/packages/patrol/lib/src/native/contracts/native_automator_client.dart @@ -27,7 +27,7 @@ class NativeAutomatorClient { NativeAutomatorClient( this._client, this._apiUri, { - Duration timeout = const Duration(seconds: 300), + Duration timeout = const Duration(seconds: 30), }) : _timeout = timeout, _headers = { 'Connection': 'keep-alive', diff --git a/packages/patrol/lib/src/native/patrol_app_service.dart b/packages/patrol/lib/src/native/patrol_app_service.dart index a487da014..bfb315f4f 100644 --- a/packages/patrol/lib/src/native/patrol_app_service.dart +++ b/packages/patrol/lib/src/native/patrol_app_service.dart @@ -11,6 +11,7 @@ import 'package:shelf/shelf.dart' as shelf; import 'package:shelf/shelf_io.dart' as shelf_io; const _port = 8082; +const _idleTimeout = Duration(hours: 2); class _TestExecutionResult { const _TestExecutionResult({required this.passed, required this.details}); @@ -32,7 +33,7 @@ Future runAppService(PatrolAppService service) async { poweredByHeader: null, ); - server.idleTimeout = const Duration(seconds: 300); + server.idleTimeout = _idleTimeout; final address = server.address; diff --git a/packages/patrol_gen/lib/src/generators/android/android_http4k_client_generator.dart b/packages/patrol_gen/lib/src/generators/android/android_http4k_client_generator.dart index 58cc0b730..0bf666f0f 100644 --- a/packages/patrol_gen/lib/src/generators/android/android_http4k_client_generator.dart +++ b/packages/patrol_gen/lib/src/generators/android/android_http4k_client_generator.dart @@ -48,7 +48,7 @@ import org.http4k.core.Status final urlWithPath = r'"$serverUrl$path"'; return ''' -class ${service.name}Client(private val address: String, private val port: Int) { +class ${service.name}Client(private val address: String, private val port: Int, private val timeout: Timeout) { $endpoints @@ -62,8 +62,8 @@ $endpoints HttpClients.custom().setDefaultRequestConfig( RequestConfig .copy(RequestConfig.DEFAULT) - .setResponseTimeout(Timeout.ofSeconds(300)) - .setConnectionRequestTimeout(Timeout.ofSeconds(300)) + .setResponseTimeout(timeout) + .setConnectionRequestTimeout(timeout) .build() ).build()) diff --git a/packages/patrol_gen/lib/src/generators/dart/dart_http_client_generator.dart b/packages/patrol_gen/lib/src/generators/dart/dart_http_client_generator.dart index efaf3e581..8606ed668 100644 --- a/packages/patrol_gen/lib/src/generators/dart/dart_http_client_generator.dart +++ b/packages/patrol_gen/lib/src/generators/dart/dart_http_client_generator.dart @@ -47,7 +47,7 @@ class ${service.name}Client { ${service.name}Client( this._client, this._apiUri, { - Duration timeout = const Duration(seconds: 300), + Duration timeout = const Duration(seconds: 30), }) : _timeout = timeout, $headers; final Duration _timeout; diff --git a/packages/patrol_gen/lib/src/generators/ios/ios_url_session_client_generator.dart b/packages/patrol_gen/lib/src/generators/ios/ios_url_session_client_generator.dart index 09bdf6cfe..35cd091ee 100644 --- a/packages/patrol_gen/lib/src/generators/ios/ios_url_session_client_generator.dart +++ b/packages/patrol_gen/lib/src/generators/ios/ios_url_session_client_generator.dart @@ -36,10 +36,12 @@ class IOSURLSessionClientGenerator { class ${service.name}Client { private let port: Int private let address: String + private let timeout: TimeInterval - init(port: Int, address: String) { + init(port: Int, address: String, timeout: TimeInterval) { self.port = port self.address = address + self.timeout = timeout } $endpoints @@ -48,13 +50,13 @@ $endpoints let url = URL(string: "$url")! let urlconfig = URLSessionConfiguration.default - urlconfig.timeoutIntervalForRequest = TimeInterval(300) - urlconfig.timeoutIntervalForResource = TimeInterval(300) + urlconfig.timeoutIntervalForRequest = timeout + urlconfig.timeoutIntervalForResource = timeout var request = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = body - request.timeoutInterval = TimeInterval(300) + request.timeoutInterval = timeout let (data, response) = try await URLSession(configuration: urlconfig).data(for: request) guard (response as? HTTPURLResponse)?.statusCode == 200 else {