Skip to content

Commit 8845467

Browse files
author
Sebastien Stormacq
committed
Also check that the test responses are in the correct order
1 parent 585a414 commit 8845467

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

Tests/AWSLambdaRuntimeTests/LambdaLocalServerTests.swift

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ extension LambdaRuntimeTests {
9898
setenv("LOCAL_LAMBDA_PORT", "\(customPort)", 1)
9999
defer { unsetenv("LOCAL_LAMBDA_PORT") }
100100

101-
let results = try await withThrowingTaskGroup(of: [Int].self) { group in
101+
struct RequestResult {
102+
let requestIndex: Int
103+
let statusCode: Int
104+
let responseBody: String
105+
}
106+
107+
let results = try await withThrowingTaskGroup(of: [RequestResult].self) { group in
102108

103109
// Start the Lambda runtime with local server
104110
group.addTask {
@@ -118,39 +124,62 @@ extension LambdaRuntimeTests {
118124
try await Task.sleep(for: .milliseconds(200))
119125

120126
// Make 10 rapid concurrent POST requests to /invoke
121-
return try await withThrowingTaskGroup(of: Int.self) { clientGroup in
122-
var statuses: [Int] = []
127+
return try await withThrowingTaskGroup(of: RequestResult.self) { clientGroup in
128+
var requestResults: [RequestResult] = []
123129

124130
for i in 0..<10 {
125131
try await Task.sleep(for: .milliseconds(0))
126132
clientGroup.addTask {
127-
let (_, response) = try await self.makeInvokeRequest(
133+
let (data, response) = try await self.makeInvokeRequest(
128134
host: "127.0.0.1",
129135
port: customPort,
130136
payload: "\"World\(i)\""
131137
)
132-
return response.statusCode
138+
let responseBody = String(data: data, encoding: .utf8) ?? ""
139+
return RequestResult(
140+
requestIndex: i,
141+
statusCode: response.statusCode,
142+
responseBody: responseBody
143+
)
133144
}
134145
}
135146

136-
for try await status in clientGroup {
137-
statuses.append(status)
147+
for try await result in clientGroup {
148+
requestResults.append(result)
138149
}
139150

140-
return statuses
151+
return requestResults
141152
}
142153
}
143154

144-
// Get the first result (HTTP statuses) and cancel the runtime
155+
// Get the first result (request results) and cancel the runtime
145156
let first = try await group.next()
146157
group.cancelAll()
147158
return first ?? []
148159
}
149160

150-
// Verify all requests returned 200 OK (no HTTP 400 errors)
161+
// Verify all requests returned 202 OK (no HTTP 400 errors)
151162
#expect(results.count == 10, "Expected 10 responses")
152-
for (index, status) in results.enumerated() {
153-
#expect(status == 202, "Request \(index) returned \(status), expected 202 OK")
163+
for result in results {
164+
#expect(
165+
result.statusCode == 202,
166+
"Request \(result.requestIndex) returned \(result.statusCode), expected 202 OK"
167+
)
168+
}
169+
170+
// Verify that each request was processed correctly by checking response content
171+
// Sort results by request index to verify proper execution order
172+
let sortedResults = results.sorted { $0.requestIndex < $1.requestIndex }
173+
for (index, result) in sortedResults.enumerated() {
174+
let expectedResponse = "\"Hello World\(index)\""
175+
#expect(
176+
result.responseBody == expectedResponse,
177+
"Request \(index) response was '\(result.responseBody)', expected '\(expectedResponse)'"
178+
)
179+
#expect(
180+
result.requestIndex == index,
181+
"Request order mismatch: got index \(result.requestIndex), expected \(index)"
182+
)
154183
}
155184
}
156185

0 commit comments

Comments
 (0)