Skip to content

Commit ae2cb67

Browse files
Guard out awaitUsingExpectation for SWIFT_CONCURRENCY_WAITER mode
Also this revealed teardown blocks were not being run in the mode, so fix that as well.
1 parent e7ea525 commit ae2cb67

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

Sources/XCTest/Private/XCTestCase.TearDownBlocksState.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@ extension XCTestCase {
1212
/// Supports async and sync throwing methods.
1313
final class TeardownBlocksState {
1414

15+
#if USE_SWIFT_CONCURRENCY_WAITER
16+
typealias TeardownBlock = @Sendable () async throws -> Void
17+
#else
18+
typealias TeardownBlock = () throws -> Void
19+
#endif
20+
1521
private var wasFinalized = false
16-
private var blocks: [() throws -> Void] = []
22+
private var blocks: [TeardownBlock] = []
1723

1824
// We don't want to overload append(_:) below because of how Swift will implicitly promote sync closures to async closures,
1925
// which can unexpectedly change their semantics in difficult to track down ways.
2026
//
2127
// Because of this, we chose the unusual decision to forgo overloading (which is a super sweet language feature <3) to prevent this issue from surprising any contributors to corelibs-xctest
2228
@available(macOS 12.0, *)
2329
func appendAsync(_ block: @Sendable @escaping () async throws -> Void) {
30+
#if USE_SWIFT_CONCURRENCY_WAITER
31+
XCTWaiter.subsystemQueue.sync {
32+
precondition(wasFinalized == false, "API violation -- attempting to add a teardown block after teardown blocks have been dequeued")
33+
blocks.append(block)
34+
}
35+
#else
2436
self.append {
2537
try awaitUsingExpectation { try await block() }
2638
}
39+
#endif
2740
}
2841

2942
func append(_ block: @escaping () throws -> Void) {
@@ -33,7 +46,7 @@ extension XCTestCase {
3346
}
3447
}
3548

36-
func finalize() -> [() throws -> Void] {
49+
func finalize() -> [TeardownBlock] {
3750
XCTWaiter.subsystemQueue.sync {
3851
precondition(wasFinalized == false, "API violation -- attempting to run teardown blocks after they've already run")
3952
wasFinalized = true

Sources/XCTest/Public/XCTestCase.swift

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -300,21 +300,8 @@ open class XCTestCase: XCTest {
300300
}
301301
}
302302

303-
private func runTeardownBlocks() {
304-
for block in self.teardownBlocksState.finalize().reversed() {
305-
do {
306-
try block()
307-
} catch {
308-
handleErrorDuringTearDown(error)
309-
}
310-
}
311-
}
312-
313-
private func performPreTearDown() {
314-
runTeardownBlocks()
315-
316-
func syncTearDown() { tearDown() }
317-
syncTearDown()
303+
private func performSyncTearDown() {
304+
tearDown()
318305

319306
do {
320307
try tearDownWithError()
@@ -324,6 +311,16 @@ open class XCTestCase: XCTest {
324311
}
325312

326313
#if USE_SWIFT_CONCURRENCY_WAITER
314+
private func runTeardownBlocks() async {
315+
for block in self.teardownBlocksState.finalize().reversed() {
316+
do {
317+
try await block()
318+
} catch {
319+
handleErrorDuringTearDown(error)
320+
}
321+
}
322+
}
323+
327324
private func performSetUpSequence() async {
328325
do {
329326
if #available(macOS 12.0, *) {
@@ -337,7 +334,8 @@ open class XCTestCase: XCTest {
337334
}
338335

339336
private func performTearDownSequence() async {
340-
performPreTearDown()
337+
await runTeardownBlocks()
338+
performSyncTearDown()
341339

342340
do {
343341
try await self.tearDown()
@@ -346,6 +344,16 @@ open class XCTestCase: XCTest {
346344
}
347345
}
348346
#else
347+
private func runTeardownBlocks() {
348+
for block in self.teardownBlocksState.finalize().reversed() {
349+
do {
350+
try block()
351+
} catch {
352+
handleErrorDuringTearDown(error)
353+
}
354+
}
355+
}
356+
349357
private func performSetUpSequence() {
350358
do {
351359
if #available(macOS 12.0, *) {
@@ -360,7 +368,8 @@ open class XCTestCase: XCTest {
360368
}
361369

362370
private func performTearDownSequence() {
363-
performPreTearDown()
371+
runTeardownBlocks()
372+
performSyncTearDown()
364373

365374
do {
366375
if #available(macOS 12.0, *) {
@@ -436,11 +445,11 @@ public func asyncTest<T: XCTestCase>(
436445
#endif
437446
}
438447

448+
#if !USE_SWIFT_CONCURRENCY_WAITER
439449
@available(macOS 12.0, *)
440450
func awaitUsingExpectation(
441451
_ closure: @escaping () async throws -> Void
442452
) throws -> Void {
443-
#if !USE_SWIFT_CONCURRENCY_WAITER
444453
let expectation = XCTestExpectation(description: "async test completion")
445454
let thrownErrorWrapper = ThrownErrorWrapper()
446455

@@ -459,8 +468,8 @@ func awaitUsingExpectation(
459468
if let error = thrownErrorWrapper.error {
460469
throw error
461470
}
462-
#endif
463471
}
472+
#endif
464473

465474
private final class ThrownErrorWrapper: @unchecked Sendable {
466475

0 commit comments

Comments
 (0)