Skip to content

Commit 8034671

Browse files
authored
Merge pull request #2970 from spevans/pr_urlsession_fix1
TestURLSession: Fix test_concurrentRequests()
2 parents 698c297 + e8bcf51 commit 8034671

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

Tests/Foundation/Tests/TestURLSession.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,32 +1165,34 @@ class TestURLSession: LoopbackServerTest {
11651165
}
11661166

11671167

1168-
func test_concurrentRequests() {
1169-
// "10 tasks ought to be enough for anybody"
1168+
func test_concurrentRequests() throws {
11701169
let tasks = 10
11711170
let syncQ = dispatchQueueMake("test_dataTaskWithURL.syncQ")
11721171
var dataTasks: [DataTask] = []
1172+
dataTasks.reserveCapacity(tasks)
1173+
1174+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
1175+
let url = try XCTUnwrap(URL(string: urlString))
1176+
11731177
let g = dispatchGroupMake()
11741178
for f in 0..<tasks {
11751179
g.enter()
1176-
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
11771180
let expectation = self.expectation(description: "GET \(urlString) [\(f)]: with a delegate")
11781181
globalDispatchQueue.async {
1179-
let url = URL(string: urlString)!
11801182
let d = DataTask(with: expectation)
11811183
d.run(with: url)
1182-
syncQ.async {
1184+
syncQ.sync {
11831185
dataTasks.append(d)
1184-
g.leave()
11851186
}
1187+
g.leave()
11861188
}
11871189
}
11881190
waitForExpectations(timeout: 12)
1189-
g.wait()
1190-
for d in syncQ.sync(execute: {dataTasks}) {
1191-
if !d.error {
1192-
XCTAssertEqual(d.capital, "Kathmandu", "test_dataTaskWithURLRequest returned an unexpected result")
1193-
}
1191+
XCTAssertEqual(g.wait(timeout: .now() + .milliseconds(1)), .success)
1192+
XCTAssertEqual(dataTasks.count, tasks)
1193+
for task in dataTasks {
1194+
XCTAssertFalse(task.error)
1195+
XCTAssertEqual(task.capital, "Kathmandu", "test_dataTaskWithURLRequest returned an unexpected result")
11941196
}
11951197
}
11961198

@@ -1819,7 +1821,7 @@ class TestURLSession: LoopbackServerTest {
18191821
("test_simpleUploadWithDelegate", test_simpleUploadWithDelegate),
18201822
("test_requestWithEmptyBody", test_requestWithEmptyBody),
18211823
("test_requestWithNonEmptyBody", test_requestWithNonEmptyBody),
1822-
/* ⚠️ */ ("test_concurrentRequests", testExpectedToFail(test_concurrentRequests, "Fails about 4% of the time")),
1824+
("test_concurrentRequests", test_concurrentRequests),
18231825
("test_disableCookiesStorage", test_disableCookiesStorage),
18241826
("test_cookiesStorage", test_cookiesStorage),
18251827
("test_cookieStorageForEphemeralConfiguration", test_cookieStorageForEphemeralConfiguration),

0 commit comments

Comments
 (0)