Skip to content

Commit d11a83a

Browse files
authored
fix: Add retry mechanism to flakey tests due to IAM eventual consistency (#2061)
1 parent 2aa2c8c commit d11a83a

File tree

3 files changed

+74
-35
lines changed

3 files changed

+74
-35
lines changed

IntegrationTests/Services/AWSCognitoIdentityIntegrationTests/CognitoAWSCredentialIdentityResolverTests.swift

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,31 @@ class CognitoAWSCredentialIdentityResolverTests: XCTestCase {
9595
region: "us-east-1" // different from cognito pool region
9696
)
9797
let cognitoStsClient = STSClient(config: cognitoStsConfig)
98-
let response = try await cognitoStsClient.getCallerIdentity(
99-
input: GetCallerIdentityInput()
100-
)
10198

102-
let account = try XCTUnwrap(response.account)
103-
let userId = try XCTUnwrap(response.userId)
104-
let arn = try XCTUnwrap(response.arn)
99+
// Retry to handle IAM eventual consistency
100+
var lastError: Error?
101+
let totalRetries = 5
102+
for attempt in 0..<totalRetries {
103+
do {
104+
let response = try await cognitoStsClient.getCallerIdentity(
105+
input: GetCallerIdentityInput()
106+
)
107+
108+
let account = try XCTUnwrap(response.account)
109+
let userId = try XCTUnwrap(response.userId)
110+
let arn = try XCTUnwrap(response.arn)
105111

106-
XCTAssertNotEqual(account, "")
107-
XCTAssertNotEqual(userId, "")
108-
XCTAssertTrue(arn.contains(roleName))
112+
XCTAssertNotEqual(account, "")
113+
XCTAssertNotEqual(userId, "")
114+
XCTAssertTrue(arn.contains(roleName))
115+
return
116+
} catch {
117+
lastError = error
118+
if attempt < (totalRetries-1) {
119+
try await Task.sleep(nanoseconds: 3_000_000_000) // 3 seconds
120+
}
121+
}
122+
}
123+
throw lastError!
109124
}
110125
}

IntegrationTests/Services/AWSSTSIntegrationTests/STSAssumeRoleAWSCredentialIdentityResolverTests.swift

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,31 @@ class STSAssumeRoleAWSCredentialIdentityResolverTests: XCTestCase {
6262

6363
// Confirm STS assume role credentials provider works by validating response.
6464
func testGetCallerIdentity() async throws {
65-
let response = try await assumeRoleStsClient.getCallerIdentity(
66-
input: GetCallerIdentityInput()
67-
)
68-
69-
// Ensure returned caller info aren't nil
70-
let account = try XCTUnwrap(response.account)
71-
let userId = try XCTUnwrap(response.userId)
72-
let arn = try XCTUnwrap(response.arn)
73-
74-
// Ensure returned caller info aren't empty strings
75-
XCTAssertNotEqual(account, "")
76-
XCTAssertNotEqual(userId, "")
77-
XCTAssertNotEqual(arn, "")
65+
// Retry to handle IAM eventual consistency
66+
var lastError: Error?
67+
let totalRetries = 5
68+
for attempt in 0..<totalRetries {
69+
do {
70+
let response = try await assumeRoleStsClient.getCallerIdentity(
71+
input: GetCallerIdentityInput()
72+
)
73+
74+
let account = try XCTUnwrap(response.account)
75+
let userId = try XCTUnwrap(response.userId)
76+
let arn = try XCTUnwrap(response.arn)
77+
78+
XCTAssertNotEqual(account, "")
79+
XCTAssertNotEqual(userId, "")
80+
XCTAssertNotEqual(arn, "")
81+
return
82+
} catch {
83+
lastError = error
84+
if attempt < (totalRetries-1) {
85+
try await Task.sleep(nanoseconds: 3_000_000_000) // 3 seconds
86+
}
87+
}
88+
}
89+
throw lastError!
7890
}
7991

8092
// Right now opentelemetry-swift doesnt support linux or visionos

IntegrationTests/Services/AWSSTSIntegrationTests/STSWebIdentityAWSCredentialIdentityResolverTests.swift

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,31 @@ class STSWebIdentityAWSCredentialIdentityResolverTests: XCTestCase {
136136

137137
// Confirm STS web identity credentials provider works by validating response.
138138
func testGetCallerIdentity() async throws {
139-
let response = try await webIdentityStsClient.getCallerIdentity(
140-
input: GetCallerIdentityInput()
141-
)
142-
143-
// Ensure returned caller info aren't nil
144-
let account = try XCTUnwrap(response.account)
145-
let userId = try XCTUnwrap(response.userId)
146-
let arn = try XCTUnwrap(response.arn)
147-
148-
// Ensure returned caller info aren't empty strings
149-
XCTAssertNotEqual(account, "")
150-
XCTAssertNotEqual(userId, "")
151-
XCTAssertNotEqual(arn, "")
139+
// Retry to handle IAM eventual consistency
140+
var lastError: Error?
141+
let totalRetries = 5
142+
for attempt in 0..<totalRetries {
143+
do {
144+
let response = try await webIdentityStsClient.getCallerIdentity(
145+
input: GetCallerIdentityInput()
146+
)
147+
148+
let account = try XCTUnwrap(response.account)
149+
let userId = try XCTUnwrap(response.userId)
150+
let arn = try XCTUnwrap(response.arn)
151+
152+
XCTAssertNotEqual(account, "")
153+
XCTAssertNotEqual(userId, "")
154+
XCTAssertNotEqual(arn, "")
155+
return
156+
} catch {
157+
lastError = error
158+
if attempt < (totalRetries-1) {
159+
try await Task.sleep(nanoseconds: 3_000_000_000) // 3 seconds
160+
}
161+
}
162+
}
163+
throw lastError!
152164
}
153165

154166
// MARK: - SETUP HELPER FUNCTIONS

0 commit comments

Comments
 (0)