|
| 1 | +// RUN: %{swiftc} %s -o %T/Use |
| 2 | +// RUN: %T/Use > %t || true |
| 3 | +// RUN: %{xctest_checker} %t %s |
| 4 | +// REQUIRES: concurrency_runtime |
| 5 | + |
| 6 | +// UNSUPPORTED: OS=windows |
| 7 | + |
| 8 | +#if os(macOS) |
| 9 | + import SwiftXCTest |
| 10 | +#else |
| 11 | + import XCTest |
| 12 | +#endif |
| 13 | + |
| 14 | +actor TestActor { |
| 15 | + |
| 16 | + enum Errors: String, Error { |
| 17 | + case example |
| 18 | + } |
| 19 | + |
| 20 | + private(set) var counter: Int = 0 |
| 21 | + |
| 22 | + func increment() async { |
| 23 | + counter += 1 |
| 24 | + } |
| 25 | + |
| 26 | + func decrement() async { |
| 27 | + counter -= 1 |
| 28 | + } |
| 29 | + |
| 30 | + func alwaysThrows() async throws { |
| 31 | + throw TestActor.Errors.example |
| 32 | + } |
| 33 | + |
| 34 | + func neverThrows() async throws {} |
| 35 | +} |
| 36 | + |
| 37 | +// CHECK: Test Suite 'All tests' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 38 | +// CHECK: Test Suite '.*\.xctest' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 39 | + |
| 40 | +// CHECK: Test Suite 'AsyncAwaitTests' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 41 | + |
| 42 | +class AsyncAwaitTests: XCTestCase { |
| 43 | + |
| 44 | + lazy var subject = TestActor() |
| 45 | + |
| 46 | + static let allTests = { |
| 47 | + return [ |
| 48 | + ("test_explicitFailures_withinAsyncTests_areReported", asyncTest(test_explicitFailures_withinAsyncTests_areReported)), |
| 49 | + ("test_asyncAnnotatedFunctionsCanPass", asyncTest(test_asyncAnnotatedFunctionsCanPass)), |
| 50 | + ("test_actorsAreSupported", asyncTest(test_actorsAreSupported)), |
| 51 | + ("test_asyncErrors_withinTestMethods_areReported", asyncTest(test_asyncErrors_withinTestMethods_areReported)), |
| 52 | + ("test_asyncAwaitCalls_withinTeardownBlocks_areSupported", asyncTest(test_asyncAwaitCalls_withinTeardownBlocks_areSupported)), |
| 53 | + ("test_asyncErrors_withinTeardownBlocks_areReported", asyncTest(test_asyncErrors_withinTeardownBlocks_areReported)), |
| 54 | + ("test_somethingAsyncWithDelay", asyncTest(test_somethingAsyncWithDelay)), |
| 55 | + ("test_syncWithinClassWithAsyncTestMethods", test_syncWithinClassWithAsyncTestMethods), |
| 56 | + ] |
| 57 | + }() |
| 58 | + |
| 59 | + override func setUp() async throws {} |
| 60 | + |
| 61 | + override func tearDown() async throws {} |
| 62 | + |
| 63 | + // CHECK: Test Case 'AsyncAwaitTests.test_explicitFailures_withinAsyncTests_areReported' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 64 | + // CHECK: .*[/\\]Asynchronous[/\\]Use[/\\]main.swift:[[@LINE+3]]: error: AsyncAwaitTests.test_explicitFailures_withinAsyncTests_areReported : XCTAssertTrue failed - |
| 65 | + // CHECK: Test Case 'AsyncAwaitTests.test_explicitFailures_withinAsyncTests_areReported' failed \(\d+\.\d+ seconds\) |
| 66 | + func test_explicitFailures_withinAsyncTests_areReported() async throws { |
| 67 | + XCTAssert(false) |
| 68 | + } |
| 69 | + |
| 70 | + // CHECK: Test Case 'AsyncAwaitTests.test_asyncAnnotatedFunctionsCanPass' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 71 | + // CHECK: Test Case 'AsyncAwaitTests.test_asyncAnnotatedFunctionsCanPass' passed \(\d+\.\d+ seconds\) |
| 72 | + func test_asyncAnnotatedFunctionsCanPass() async throws { |
| 73 | + let value = await makeString() |
| 74 | + XCTAssertNotEqual(value, "") |
| 75 | + } |
| 76 | + |
| 77 | + // CHECK: Test Case 'AsyncAwaitTests.test_actorsAreSupported' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 78 | + // CHECK: Test Case 'AsyncAwaitTests.test_actorsAreSupported' passed \(\d+\.\d+ seconds\) |
| 79 | + func test_actorsAreSupported() async throws { |
| 80 | + let initialCounterValue = await subject.counter |
| 81 | + XCTAssertEqual(initialCounterValue, 0) |
| 82 | + |
| 83 | + await subject.increment() |
| 84 | + await subject.increment() |
| 85 | + |
| 86 | + let secondCounterValue = await subject.counter |
| 87 | + XCTAssertEqual(secondCounterValue, 2) |
| 88 | + |
| 89 | + await subject.decrement() |
| 90 | + let thirdCounterValue = await subject.counter |
| 91 | + XCTAssertEqual(thirdCounterValue, 1) |
| 92 | + } |
| 93 | + |
| 94 | + // CHECK: Test Case 'AsyncAwaitTests.test_asyncErrors_withinTestMethods_areReported' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 95 | + // CHECK: \<EXPR\>:0: error: AsyncAwaitTests.test_asyncErrors_withinTestMethods_areReported : threw error "example" |
| 96 | + // CHECK: Test Case 'AsyncAwaitTests.test_asyncErrors_withinTestMethods_areReported' failed \(\d+\.\d+ seconds\) |
| 97 | + func test_asyncErrors_withinTestMethods_areReported() async throws { |
| 98 | + try await subject.alwaysThrows() |
| 99 | + } |
| 100 | + |
| 101 | + // CHECK: Test Case 'AsyncAwaitTests.test_asyncAwaitCalls_withinTeardownBlocks_areSupported' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 102 | + // CHECK: In teardown block\n |
| 103 | + // CHECK: \<EXPR\>:0: error: AsyncAwaitTests.test_asyncAwaitCalls_withinTeardownBlocks_areSupported : threw error "example" |
| 104 | + // CHECK: Test Case 'AsyncAwaitTests.test_asyncAwaitCalls_withinTeardownBlocks_areSupported' failed \(\d+\.\d+ seconds\) |
| 105 | + func test_asyncAwaitCalls_withinTeardownBlocks_areSupported() async throws { |
| 106 | + addTeardownBlock { |
| 107 | + print("In teardown block") |
| 108 | + try await self.subject.alwaysThrows() |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // CHECK: Test Case 'AsyncAwaitTests.test_asyncErrors_withinTeardownBlocks_areReported' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+\ |
| 113 | + // CHECK: <EXPR>:0: error: AsyncAwaitTests.test_asyncErrors_withinTeardownBlocks_areReported : threw error "example"\n |
| 114 | + // CHECK: Test Case 'AsyncAwaitTests.test_asyncErrors_withinTeardownBlocks_areReported' failed \(\d+\.\d+ seconds\) |
| 115 | + func test_asyncErrors_withinTeardownBlocks_areReported() throws { |
| 116 | + let issueRecordedExpectation = XCTestExpectation(description: "Asynchronous error recorded in: \(#function)") |
| 117 | + |
| 118 | + addTeardownBlock { |
| 119 | + // Use addTeardownBlock here because the `addTeardownBlock` below intentionally throws an error so we can't `wait` after that in the same scope |
| 120 | + self.wait(for: [issueRecordedExpectation], timeout: 1) |
| 121 | + } |
| 122 | + |
| 123 | + addTeardownBlock { |
| 124 | + do { |
| 125 | + try await self.subject.alwaysThrows() |
| 126 | + } catch { |
| 127 | + issueRecordedExpectation.fulfill() |
| 128 | + throw error |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // CHECK: Test Case 'AsyncAwaitTests.test_somethingAsyncWithDelay' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 134 | + // CHECK: Test Case 'AsyncAwaitTests.test_somethingAsyncWithDelay' passed \(\d+\.\d+ seconds\) |
| 135 | + func test_somethingAsyncWithDelay() async throws { |
| 136 | + try await doSomethingWithDelay() |
| 137 | + } |
| 138 | + |
| 139 | + // CHECK: Test Case 'AsyncAwaitTests.test_syncWithinClassWithAsyncTestMethods' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 140 | + // CHECK: Test Case 'AsyncAwaitTests.test_syncWithinClassWithAsyncTestMethods' passed \(\d+\.\d+ seconds\) |
| 141 | + func test_syncWithinClassWithAsyncTestMethods() /* intentionally non-async */ throws { |
| 142 | + XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.") |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +private extension AsyncAwaitTests { |
| 147 | + |
| 148 | + func makeString() async -> String { |
| 149 | + """ |
| 150 | + Some arbitrary text. |
| 151 | + Nothing to see here, folx. |
| 152 | + """ |
| 153 | + } |
| 154 | + |
| 155 | + func doSomethingWithDelay() async throws { |
| 156 | + func doSomethingWithDelay(completion: @escaping (Error?) -> Void) { |
| 157 | + DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(10)) { |
| 158 | + completion(nil) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation<Void, Error>) in |
| 163 | + doSomethingWithDelay { error in |
| 164 | + if let error = error { |
| 165 | + continuation.resume(throwing: error) |
| 166 | + } else { |
| 167 | + continuation.resume() |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// CHECK: Test Suite 'AsyncAwaitTests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 175 | +// CHECK: \t Executed 8 tests, with 4 failures \(3 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds |
| 176 | +// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 177 | +// CHECK: \t Executed 8 tests, with 4 failures \(3 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds |
| 178 | + |
| 179 | +XCTMain([testCase(AsyncAwaitTests.allTests)]) |
| 180 | + |
| 181 | +// CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+ |
| 182 | +// CHECK: \t Executed 8 tests, with 4 failures \(3 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds |
| 183 | + |
0 commit comments