Skip to content

Commit 1119893

Browse files
authored
Add XCTAsyncTest and XCTAssertThrowsError from gRPC Swift (#493)
1 parent f2bb283 commit 1119893

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

NOTICE.txt

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
The AsyncHTTPClient Project
3+
===========================
4+
5+
Please visit the AsyncHTTPClient web site for more information:
6+
7+
* https://github.com/swift-server/async-http-client
8+
9+
Copyright 2017-2021 The AsyncHTTPClient Project
10+
11+
The AsyncHTTPClient Project licenses this file to you under the Apache License,
12+
version 2.0 (the "License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at:
14+
15+
https://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing, software
18+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20+
License for the specific language governing permissions and limitations
21+
under the License.
22+
23+
Also, please refer to each LICENSE.txt file, which is located in
24+
the 'license' directory of the distribution file, for the license terms of the
25+
components that this product depends on.
26+
27+
---
28+
29+
This product contains derivations of various scripts from SwiftNIO.
30+
31+
* LICENSE (Apache License 2.0):
32+
* https://www.apache.org/licenses/LICENSE-2.0
33+
* HOMEPAGE:
34+
* https://github.com/apple/swift-nio
35+
36+
---
37+
38+
This product contains a derivation of "XCTest+AsyncAwait.swift" from gRPC Swift.
39+
40+
* LICENSE (Apache License 2.0):
41+
* https://www.apache.org/licenses/LICENSE-2.0
42+
* HOMEPAGE:
43+
* https://github.com/grpc/grpc-swift
44+
45+
---
46+
47+
This product contains a derivation of the Tony Stone's 'process_test_files.rb'.
48+
49+
* LICENSE (Apache License 2.0):
50+
* https://www.apache.org/licenses/LICENSE-2.0
51+
* HOMEPAGE:
52+
* https://github.com/tonystone/build-tools/commit/6c417b7569df24597a48a9aa7b505b636e8f73a1
53+
* https://github.com/tonystone/build-tools/blob/master/source/xctest_tool.rb
54+
55+
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the AsyncHTTPClient open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
/*
15+
* Copyright 2021, gRPC Authors All rights reserved.
16+
*
17+
* Licensed under the Apache License, Version 2.0 (the "License");
18+
* you may not use this file except in compliance with the License.
19+
* You may obtain a copy of the License at
20+
*
21+
* http://www.apache.org/licenses/LICENSE-2.0
22+
*
23+
* Unless required by applicable law or agreed to in writing, software
24+
* distributed under the License is distributed on an "AS IS" BASIS,
25+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26+
* See the License for the specific language governing permissions and
27+
* limitations under the License.
28+
*/
29+
#if compiler(>=5.5)
30+
import XCTest
31+
32+
extension XCTestCase {
33+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
34+
/// Cross-platform XCTest support for async-await tests.
35+
///
36+
/// Currently the Linux implementation of XCTest doesn't have async-await support.
37+
/// Until it does, we make use of this shim which uses a detached `Task` along with
38+
/// `XCTest.wait(for:timeout:)` to wrap the operation.
39+
///
40+
/// - NOTE: Support for Linux is tracked by https://bugs.swift.org/browse/SR-14403.
41+
/// - NOTE: Implementation currently in progress: https://github.com/apple/swift-corelibs-xctest/pull/326
42+
func XCTAsyncTest(
43+
expectationDescription: String = "Async operation",
44+
timeout: TimeInterval = 30,
45+
file: StaticString = #filePath,
46+
line: UInt = #line,
47+
function: StaticString = #function,
48+
operation: @escaping @Sendable () async throws -> Void
49+
) {
50+
let expectation = self.expectation(description: expectationDescription)
51+
Task {
52+
do {
53+
try await operation()
54+
} catch {
55+
XCTFail("Error thrown while executing \(function): \(error)", file: file, line: line)
56+
Thread.callStackSymbols.forEach { print($0) }
57+
}
58+
expectation.fulfill()
59+
}
60+
self.wait(for: [expectation], timeout: timeout)
61+
}
62+
}
63+
64+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
65+
internal func XCTAssertThrowsError<T>(
66+
_ expression: @autoclosure () async throws -> T,
67+
verify: (Error) -> Void = { _ in },
68+
file: StaticString = #file,
69+
line: UInt = #line
70+
) async {
71+
do {
72+
_ = try await expression()
73+
XCTFail("Expression did not throw error", file: file, line: line)
74+
} catch {
75+
verify(error)
76+
}
77+
}
78+
79+
#endif // compiler(>=5.5)

0 commit comments

Comments
 (0)