Skip to content
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

Add granular configuration for retrying wait time in test #470

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Tests/CartonCommandTests/CommandTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sleepの場所を組み替えて、
最初と、リトライ時の2つに分けます。

attempt += 1
do {
return try await body()
} catch {
if attempt < maxAttempts {
print("attempt \(attempt) failed: \(error), retrying...")

try await Task.sleep(for: retryInterval)
continue
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/CartonCommandTests/DevCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions Tests/CartonCommandTests/FrontendDevServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ final class FrontendDevServerTests: XCTestCase {
defer {
devServer.signal(SIGINT)
}
try await Task.sleep(for: .seconds(3))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最初に3秒まつ処理を withRetry でやります。


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>
Expand Down