Skip to content

Commit 7e6dc1e

Browse files
authored
Remove XCTAsyncTest usage (#2461)
# Motivation We introduced `XCTAsyncTest` to bridge the gap where some Swift versions did not have `async` test support. # Modification This PR removes `XCTAsyncTest` and migrates all the places where we used it to native `async` tests. # Result Less custom code.
1 parent 3a88bf6 commit 7e6dc1e

9 files changed

+1428
-1660
lines changed

Tests/NIOCoreTests/AsyncChannel/AsyncChannelTests.swift

Lines changed: 294 additions & 318 deletions
Large diffs are not rendered by default.

Tests/NIOCoreTests/AsyncSequenceTests.swift

Lines changed: 118 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -26,136 +26,134 @@ fileprivate struct TestCase {
2626
}
2727

2828
final class AsyncSequenceCollectTests: XCTestCase {
29-
func testAsyncSequenceCollect() {
29+
func testAsyncSequenceCollect() async throws {
3030
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return }
31-
XCTAsyncTest(timeout: 5) {
32-
let testCases = [
33-
TestCase([
34-
[],
35-
]),
36-
TestCase([
37-
[],
38-
[],
39-
]),
40-
TestCase([
41-
[0],
42-
[],
43-
]),
44-
TestCase([
45-
[],
46-
[0],
47-
]),
48-
TestCase([
49-
[0],
50-
[1],
51-
]),
52-
TestCase([
53-
[0],
54-
[1],
55-
]),
56-
TestCase([
57-
[0],
58-
[1],
59-
[2],
60-
]),
61-
TestCase([
62-
[],
63-
[0],
64-
[],
65-
[1],
66-
[],
67-
[2],
68-
[],
69-
]),
70-
TestCase([
71-
[0],
72-
[1],
73-
[2],
74-
[],
75-
[],
76-
]),
77-
TestCase([
78-
Array(0..<10),
79-
]),
80-
TestCase([
81-
Array(0..<10),
82-
Array(10..<20),
83-
]),
84-
TestCase([
85-
Array(0..<10),
86-
Array(10..<20),
87-
Array(20..<30),
88-
]),
89-
TestCase([
90-
Array(0..<10),
91-
Array(10..<20),
92-
Array(20..<30),
93-
Array(repeating: 99, count: 1000),
94-
]),
95-
]
96-
for testCase in testCases {
97-
let expectedBytes = testCase.buffers.flatMap({ $0 })
98-
99-
// happy case where maxBytes is exactly the same as number of buffers received
100-
101-
// test for the generic version
102-
let accumulatedBytes1 = try await testCase.buffers
31+
let testCases = [
32+
TestCase([
33+
[],
34+
]),
35+
TestCase([
36+
[],
37+
[],
38+
]),
39+
TestCase([
40+
[0],
41+
[],
42+
]),
43+
TestCase([
44+
[],
45+
[0],
46+
]),
47+
TestCase([
48+
[0],
49+
[1],
50+
]),
51+
TestCase([
52+
[0],
53+
[1],
54+
]),
55+
TestCase([
56+
[0],
57+
[1],
58+
[2],
59+
]),
60+
TestCase([
61+
[],
62+
[0],
63+
[],
64+
[1],
65+
[],
66+
[2],
67+
[],
68+
]),
69+
TestCase([
70+
[0],
71+
[1],
72+
[2],
73+
[],
74+
[],
75+
]),
76+
TestCase([
77+
Array(0..<10),
78+
]),
79+
TestCase([
80+
Array(0..<10),
81+
Array(10..<20),
82+
]),
83+
TestCase([
84+
Array(0..<10),
85+
Array(10..<20),
86+
Array(20..<30),
87+
]),
88+
TestCase([
89+
Array(0..<10),
90+
Array(10..<20),
91+
Array(20..<30),
92+
Array(repeating: 99, count: 1000),
93+
]),
94+
]
95+
for testCase in testCases {
96+
let expectedBytes = testCase.buffers.flatMap({ $0 })
97+
98+
// happy case where maxBytes is exactly the same as number of buffers received
99+
100+
// test for the generic version
101+
let accumulatedBytes1 = try await testCase.buffers
102+
.asAsyncSequence()
103+
.collect(upTo: expectedBytes.count, using: .init())
104+
XCTAssertEqual(
105+
accumulatedBytes1,
106+
ByteBuffer(bytes: expectedBytes),
107+
file: testCase.file,
108+
line: testCase.line
109+
)
110+
111+
// test for the `ByteBuffer` optimised version
112+
let accumulatedBytes2 = try await testCase.buffers
113+
.map(ByteBuffer.init(bytes:))
114+
.asAsyncSequence()
115+
.collect(upTo: expectedBytes.count)
116+
XCTAssertEqual(
117+
accumulatedBytes2,
118+
ByteBuffer(bytes: expectedBytes),
119+
file: testCase.file,
120+
line: testCase.line
121+
)
122+
123+
// unhappy case where maxBytes is one byte less than actually received
124+
guard expectedBytes.count >= 1 else {
125+
continue
126+
}
127+
128+
// test for the generic version
129+
await XCTAssertThrowsError(
130+
try await testCase.buffers
103131
.asAsyncSequence()
104-
.collect(upTo: expectedBytes.count, using: .init())
105-
XCTAssertEqual(
106-
accumulatedBytes1,
107-
ByteBuffer(bytes: expectedBytes),
132+
.collect(upTo: max(expectedBytes.count - 1, 0), using: .init()),
133+
file: testCase.file,
134+
line: testCase.line
135+
) { error in
136+
XCTAssertTrue(
137+
error is NIOTooManyBytesError,
108138
file: testCase.file,
109139
line: testCase.line
110140
)
111-
112-
// test for the `ByteBuffer` optimised version
113-
let accumulatedBytes2 = try await testCase.buffers
141+
}
142+
143+
// test for the `ByteBuffer` optimised version
144+
await XCTAssertThrowsError(
145+
try await testCase.buffers
114146
.map(ByteBuffer.init(bytes:))
115147
.asAsyncSequence()
116-
.collect(upTo: expectedBytes.count)
117-
XCTAssertEqual(
118-
accumulatedBytes2,
119-
ByteBuffer(bytes: expectedBytes),
148+
.collect(upTo: max(expectedBytes.count - 1, 0)),
149+
file: testCase.file,
150+
line: testCase.line
151+
) { error in
152+
XCTAssertTrue(
153+
error is NIOTooManyBytesError,
120154
file: testCase.file,
121155
line: testCase.line
122156
)
123-
124-
// unhappy case where maxBytes is one byte less than actually received
125-
guard expectedBytes.count >= 1 else {
126-
continue
127-
}
128-
129-
// test for the generic version
130-
await XCTAssertThrowsError(
131-
try await testCase.buffers
132-
.asAsyncSequence()
133-
.collect(upTo: max(expectedBytes.count - 1, 0), using: .init()),
134-
file: testCase.file,
135-
line: testCase.line
136-
) { error in
137-
XCTAssertTrue(
138-
error is NIOTooManyBytesError,
139-
file: testCase.file,
140-
line: testCase.line
141-
)
142-
}
143-
144-
// test for the `ByteBuffer` optimised version
145-
await XCTAssertThrowsError(
146-
try await testCase.buffers
147-
.map(ByteBuffer.init(bytes:))
148-
.asAsyncSequence()
149-
.collect(upTo: max(expectedBytes.count - 1, 0)),
150-
file: testCase.file,
151-
line: testCase.line
152-
) { error in
153-
XCTAssertTrue(
154-
error is NIOTooManyBytesError,
155-
file: testCase.file,
156-
line: testCase.line
157-
)
158-
}
159157
}
160158
}
161159
}

Tests/NIOCoreTests/XCTest+AsyncAwait.swift

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,6 @@
4141
*/
4242

4343
import XCTest
44-
45-
extension XCTestCase {
46-
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
47-
/// Cross-platform XCTest support for async-await tests.
48-
///
49-
/// Currently the Linux implementation of XCTest doesn't have async-await support.
50-
/// Until it does, we make use of this shim which uses a detached `Task` along with
51-
/// `XCTest.wait(for:timeout:)` to wrap the operation.
52-
///
53-
/// - NOTE: Support for Linux is tracked by https://bugs.swift.org/browse/SR-14403.
54-
/// - NOTE: Implementation currently in progress: https://github.com/apple/swift-corelibs-xctest/pull/326
55-
func XCTAsyncTest(
56-
expectationDescription: String = "Async operation",
57-
timeout: TimeInterval = 30,
58-
file: StaticString = #filePath,
59-
line: UInt = #line,
60-
function: StaticString = #function,
61-
operation: @escaping @Sendable () async throws -> Void
62-
) {
63-
let expectation = self.expectation(description: expectationDescription)
64-
Task {
65-
do {
66-
try await operation()
67-
} catch {
68-
XCTFail("Error thrown while executing \(function): \(error)", file: file, line: line)
69-
Thread.callStackSymbols.forEach { print($0) }
70-
}
71-
expectation.fulfill()
72-
}
73-
self.wait(for: [expectation], timeout: timeout)
74-
}
75-
}
76-
7744
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
7845
internal func XCTAssertThrowsError<T>(
7946
_ expression: @autoclosure () async throws -> T,

0 commit comments

Comments
 (0)