1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//
// XCTestCase+Asynchronous.swift
// Methods on XCTestCase for testing asynchronous operations
//
public extension XCTestCase {
/// Creates a point of synchronization in the flow of a test. Only one
/// "wait" can be active at any given time, but multiple discrete sequences
/// of { expectations -> wait } can be chained together. The related
/// XCTWaiter API allows multiple "nested" waits if that is required.
///
/// - Parameter timeout: The amount of time within which all expectation
/// must be fulfilled.
/// - Parameter file: The file name to use in the error message if
/// expectations are not met before the given timeout. Default is the file
/// containing the call to this method. It is rare to provide this
/// parameter when calling this method.
/// - Parameter line: The line number to use in the error message if the
/// expectations are not met before the given timeout. Default is the line
/// number of the call to this method in the calling file. It is rare to
/// provide this parameter when calling this method.
/// - Parameter handler: If provided, the handler will be invoked both on
/// timeout or fulfillment of all expectations. Timeout is always treated
/// as a test failure.
///
/// - SeeAlso: XCTWaiter
///
/// - Note: Whereas Objective-C XCTest determines the file and line
/// number of the "wait" call using symbolication, this implementation
/// opts to take `file` and `line` as parameters instead. As a result,
/// the interface to these methods are not exactly identical between
/// these environments. To ensure compatibility of tests between
/// swift-corelibs-xctest and Apple XCTest, it is not recommended to pass
/// explicit values for `file` and `line`.
// TODO: Having issues with runtime use of MainActor annotated tests on Linux - causes a precondition failure in a dynamic cast
//@preconcurrency @MainActor
func waitForExpectations(timeout: TimeInterval, file: StaticString = #file, line: Int = #line, handler: XCWaitCompletionHandler? = nil) {
precondition(Thread.isMainThread, "\(#function) must be called on the main thread")
if currentWaiter != nil {
return recordFailure(description: "API violation - calling wait on test case while already waiting.", at: SourceLocation(file: file, line: line), expected: false)
}
let expectations = self.expectations
if expectations.isEmpty {
return recordFailure(description: "API violation - call made to wait without any expectations having been set.", at: SourceLocation(file: file, line: line), expected: false)
}
let waiter = XCTWaiter(delegate: self)
currentWaiter = waiter
let waiterResult = waiter.wait(for: expectations, timeout: timeout, file: file, line: line)
currentWaiter = nil
cleanUpExpectations(expectations)
// The handler is invoked regardless of whether the test passed.
if let handler = handler {
let error = (waiterResult == .completed) ? nil : XCTestError(.timeoutWhileWaiting)
handler(error)
}
}
/// Wait on an array of expectations for up to the specified timeout, and optionally specify whether they
/// must be fulfilled in the given order. May return early based on fulfillment of the waited on expectations.
///
/// - Parameter expectations: The expectations to wait on.
/// - Parameter timeout: The maximum total time duration to wait on all expectations.
/// - Parameter enforceOrder: Specifies whether the expectations must be fulfilled in the order
/// they are specified in the `expectations` Array. Default is false.
/// - Parameter file: The file name to use in the error message if
/// expectations are not fulfilled before the given timeout. Default is the file
/// containing the call to this method. It is rare to provide this
/// parameter when calling this method.
/// - Parameter line: The line number to use in the error message if the
/// expectations are not fulfilled before the given timeout. Default is the line
/// number of the call to this method in the calling file. It is rare to
/// provide this parameter when calling this method.
///
/// - SeeAlso: XCTWaiter
@available(*, noasync, message: "Use await fulfillment(of:timeout:enforceOrder:) instead.")
func wait(for expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) {
let waiter = XCTWaiter(delegate: self)
waiter.wait(for: expectations, timeout: timeout, enforceOrder: enforceOrder, file: file, line: line)
cleanUpExpectations(expectations)
}
/// Wait on an array of expectations for up to the specified timeout, and optionally specify whether they
/// must be fulfilled in the given order. May return early based on fulfillment of the waited on expectations.
///
/// - Parameter expectations: The expectations to wait on.
/// - Parameter timeout: The maximum total time duration to wait on all expectations.
/// - Parameter enforceOrder: Specifies whether the expectations must be fulfilled in the order
/// they are specified in the `expectations` Array. Default is false.
/// - Parameter file: The file name to use in the error message if
/// expectations are not fulfilled before the given timeout. Default is the file
/// containing the call to this method. It is rare to provide this
/// parameter when calling this method.
/// - Parameter line: The line number to use in the error message if the
/// expectations are not fulfilled before the given timeout. Default is the line
/// number of the call to this method in the calling file. It is rare to
/// provide this parameter when calling this method.
///
/// - SeeAlso: XCTWaiter
@available(macOS 12.0, *)
func fulfillment(of expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false, file: StaticString = #file, line: Int = #line) async {
let waiter = XCTWaiter(delegate: self)
await waiter.fulfillment(of: expectations, timeout: timeout, enforceOrder: enforceOrder, file: file, line: line)
cleanUpExpectations(expectations)
}
/// Creates and returns an expectation associated with the test case.
///
/// - Parameter description: This string will be displayed in the test log
/// to help diagnose failures.
/// - Parameter file: The file name to use in the error message if
/// this expectation is not waited for. Default is the file
/// containing the call to this method. It is rare to provide this
/// parameter when calling this method.
/// - Parameter line: The line number to use in the error message if the
/// this expectation is not waited for. Default is the line
/// number of the call to this method in the calling file. It is rare to
/// provide this parameter when calling this method.
///
/// - Note: Whereas Objective-C XCTest determines the file and line
/// number of expectations that are created by using symbolication, this
/// implementation opts to take `file` and `line` as parameters instead.
/// As a result, the interface to these methods are not exactly identical
/// between these environments. To ensure compatibility of tests between
/// swift-corelibs-xctest and Apple XCTest, it is not recommended to pass
/// explicit values for `file` and `line`.
@discardableResult func expectation(description: String, file: StaticString = #file, line: Int = #line) -> XCTestExpectation {
let expectation = XCTestExpectation(description: description, file: file, line: line)
addExpectation(expectation)
return expectation
}
/// Creates and returns an expectation for a notification.
///
/// - Parameter notificationName: The name of the notification the
/// expectation observes.
/// - Parameter object: The object whose notifications the expectation will
/// receive; that is, only notifications with this object are observed by
/// the test case. If you pass nil, the expectation doesn't use
/// a notification's object to decide whether it is fulfilled.
/// - Parameter notificationCenter: The specific notification center that
/// the notification will be posted to.
/// - Parameter handler: If provided, the handler will be invoked when the
/// notification is observed. It will not be invoked on timeout. Use the
/// handler to further investigate if the notification fulfills the
/// expectation.
@discardableResult func expectation(forNotification notificationName: Notification.Name, object: Any? = nil, notificationCenter: NotificationCenter = .default, file: StaticString = #file, line: Int = #line, handler: XCTNSNotificationExpectation.Handler? = nil) -> XCTestExpectation {
let expectation = XCTNSNotificationExpectation(name: notificationName, object: object, notificationCenter: notificationCenter, file: file, line: line)
expectation.handler = handler
addExpectation(expectation)
return expectation
}
/// Creates and returns an expectation for a notification.
///
/// - Parameter notificationName: The name of the notification the
/// expectation observes.
/// - Parameter object: The object whose notifications the expectation will
/// receive; that is, only notifications with this object are observed by
/// the test case. If you pass nil, the expectation doesn't use
/// a notification's object to decide whether it is fulfilled.
/// - Parameter notificationCenter: The specific notification center that
/// the notification will be posted to.
/// - Parameter handler: If provided, the handler will be invoked when the
/// notification is observed. It will not be invoked on timeout. Use the
/// handler to further investigate if the notification fulfills the
/// expectation.
@discardableResult func expectation(forNotification notificationName: String, object: Any? = nil, notificationCenter: NotificationCenter = .default, file: StaticString = #file, line: Int = #line, handler: XCTNSNotificationExpectation.Handler? = nil) -> XCTestExpectation {
return expectation(forNotification: Notification.Name(rawValue: notificationName), object: object, notificationCenter: notificationCenter, file: file, line: line, handler: handler)
}
/// Creates and returns an expectation that is fulfilled if the predicate
/// returns true when evaluated with the given object. The expectation
/// periodically evaluates the predicate and also may use notifications or
/// other events to optimistically re-evaluate.
///
/// - Parameter predicate: The predicate that will be used to evaluate the
/// object.
/// - Parameter object: The object that is evaluated against the conditions
/// specified by the predicate, if any. Default is nil.
/// - Parameter file: The file name to use in the error message if
/// this expectation is not waited for. Default is the file
/// containing the call to this method. It is rare to provide this
/// parameter when calling this method.
/// - Parameter line: The line number to use in the error message if the
/// this expectation is not waited for. Default is the line
/// number of the call to this method in the calling file. It is rare to
/// provide this parameter when calling this method.
/// - Parameter handler: A block to be invoked when evaluating the predicate
/// against the object returns true. If the block is not provided the
/// first successful evaluation will fulfill the expectation. If provided,
/// the handler can override that behavior which leaves the caller
/// responsible for fulfilling the expectation.
@discardableResult func expectation(for predicate: NSPredicate, evaluatedWith object: Any? = nil, file: StaticString = #file, line: Int = #line, handler: XCTNSPredicateExpectation.Handler? = nil) -> XCTestExpectation {
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: object, file: file, line: line)
expectation.handler = handler
addExpectation(expectation)
return expectation
}
}
/// A block to be invoked when a call to wait times out or has had all
/// associated expectations fulfilled.
///
/// - Parameter error: If the wait timed out or a failure was raised while
/// waiting, the error's code will specify the type of failure. Otherwise
/// error will be nil.
public typealias XCWaitCompletionHandler = (Error?) -> ()
extension XCTestCase: XCTWaiterDelegate {
public func waiter(_ waiter: XCTWaiter, didTimeoutWithUnfulfilledExpectations unfulfilledExpectations: [XCTestExpectation]) {
let expectationDescription = unfulfilledExpectations.map { $0.expectationDescription }.joined(separator: ", ")
let failureDescription = "Asynchronous wait failed - Exceeded timeout of \(waiter.timeout) seconds, with unfulfilled expectations: \(expectationDescription)"
recordFailure(description: failureDescription, at: waiter.waitSourceLocation ?? .unknown, expected: true)
}
public func waiter(_ waiter: XCTWaiter, fulfillmentDidViolateOrderingConstraintsFor expectation: XCTestExpectation, requiredExpectation: XCTestExpectation) {
let failureDescription = "Failed due to expectation fulfilled in incorrect order: requires '\(requiredExpectation.expectationDescription)', actually fulfilled '\(expectation.expectationDescription)'"
recordFailure(description: failureDescription, at: expectation.fulfillmentSourceLocation ?? .unknown, expected: true)
}
public func waiter(_ waiter: XCTWaiter, didFulfillInvertedExpectation expectation: XCTestExpectation) {
let failureDescription = "Asynchronous wait failed - Fulfilled inverted expectation '\(expectation.expectationDescription)'"
recordFailure(description: failureDescription, at: expectation.fulfillmentSourceLocation ?? .unknown, expected: true)
}
public func nestedWaiter(_ waiter: XCTWaiter, wasInterruptedByTimedOutWaiter outerWaiter: XCTWaiter) {
let failureDescription = "Asynchronous waiter \(waiter) failed - Interrupted by timeout of containing waiter \(outerWaiter)"
recordFailure(description: failureDescription, at: waiter.waitSourceLocation ?? .unknown, expected: true)
}
}
internal extension XCTestCase {
// It is an API violation to create expectations but not wait for them to
// be completed. Notify the user of a mistake via a test failure.
func failIfExpectationsNotWaitedFor(_ expectations: [XCTestExpectation]) {
let orderedUnwaitedExpectations = expectations.filter { !$0.hasBeenWaitedOn }.sorted { $0.creationToken < $1.creationToken }
guard let expectationForFileLineReporting = orderedUnwaitedExpectations.first else {
return
}
let expectationDescriptions = orderedUnwaitedExpectations.map { "'\($0.expectationDescription)'" }.joined(separator: ", ")
let failureDescription = "Failed due to unwaited expectation\(orderedUnwaitedExpectations.count > 1 ? "s" : "") \(expectationDescriptions)"
recordFailure(
description: failureDescription,
at: expectationForFileLineReporting.creationSourceLocation,
expected: false)
}
}
|