Add granular configuration for retrying wait time in test (#470)
* kick CI * improve withRetry utility
This commit is contained in:
parent
b26110bfa2
commit
59730f982f
|
@ -145,17 +145,24 @@ func fetchWebContent(at url: URL, timeout: Duration) async throws -> (response:
|
|||
return (response: response, body: body)
|
||||
}
|
||||
|
||||
func withRetry<R>(maxAttempts: Int, delay: Duration, body: () async throws -> R) async throws -> R {
|
||||
func withRetry<R>(
|
||||
maxAttempts: Int,
|
||||
initialDelay: Duration,
|
||||
retryInterval: Duration,
|
||||
body: () async throws -> R
|
||||
) async throws -> R {
|
||||
try await Task.sleep(for: initialDelay)
|
||||
|
||||
var attempt = 0
|
||||
while true {
|
||||
try await Task.sleep(for: delay)
|
||||
|
||||
attempt += 1
|
||||
do {
|
||||
return try await body()
|
||||
} catch {
|
||||
if attempt < maxAttempts {
|
||||
print("attempt \(attempt) failed: \(error), retrying...")
|
||||
|
||||
try await Task.sleep(for: retryInterval)
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ final class DevCommandTests: XCTestCase {
|
|||
let count = 5
|
||||
|
||||
do {
|
||||
return try await withRetry(maxAttempts: count, delay: delay) {
|
||||
return try await withRetry(maxAttempts: count, initialDelay: delay, retryInterval: delay) {
|
||||
try await fetchWebContent(at: url, timeout: timeOut)
|
||||
}
|
||||
} catch {
|
||||
|
|
|
@ -41,12 +41,16 @@ final class FrontendDevServerTests: XCTestCase {
|
|||
defer {
|
||||
devServer.signal(SIGINT)
|
||||
}
|
||||
try await Task.sleep(for: .seconds(3))
|
||||
|
||||
let host = try URL(string: "http://127.0.0.1:8080").unwrap("url")
|
||||
|
||||
do {
|
||||
let indexHtml = try await fetchString(at: host)
|
||||
let indexHtml = try await withRetry(
|
||||
maxAttempts: 5, initialDelay: .seconds(3), retryInterval: .seconds(10)
|
||||
) {
|
||||
try await fetchString(at: host)
|
||||
}
|
||||
|
||||
XCTAssertEqual(indexHtml, """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
|
Loading…
Reference in New Issue