diff --git a/Sources/AsyncSequenceValidation/SourceLocation.swift b/Sources/AsyncSequenceValidation/SourceLocation.swift index f2bfa17..c0107cc 100644 --- a/Sources/AsyncSequenceValidation/SourceLocation.swift +++ b/Sources/AsyncSequenceValidation/SourceLocation.swift @@ -13,6 +13,11 @@ public struct SourceLocation: Sendable, CustomStringConvertible { public var file: StaticString public var line: UInt + public init(file: StaticString, line: UInt) { + self.file = file + self.line = line + } + public var description: String { return "\(file):\(line)" } diff --git a/Sources/AsyncSequenceValidation/TaskDriver.swift b/Sources/AsyncSequenceValidation/TaskDriver.swift index 05acf53..3df15ba 100644 --- a/Sources/AsyncSequenceValidation/TaskDriver.swift +++ b/Sources/AsyncSequenceValidation/TaskDriver.swift @@ -19,17 +19,32 @@ import _CAsyncSequenceValidationSupport #error("TODO: Port TaskDriver threading to windows") #endif +#if canImport(Darwin) @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? { Unmanaged.fromOpaque(raw).takeRetainedValue().run() return nil } +#elseif canImport(Glibc) +func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? { + Unmanaged.fromOpaque(raw!).takeRetainedValue().run() + return nil +} +#elseif canImport(WinSDK) +#error("TODO: Port TaskDriver threading to windows") +#endif @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) final class TaskDriver { let work: (TaskDriver) -> Void let queue: WorkQueue +#if canImport(Darwin) var thread: pthread_t? +#elseif canImport(Glibc) + var thread = pthread_t() +#elseif canImport(WinSDK) +#error("TODO: Port TaskDriver threading to windows") +#endif init(queue: WorkQueue, _ work: @escaping (TaskDriver) -> Void) { self.queue = queue @@ -42,12 +57,20 @@ final class TaskDriver { } func run() { +#if canImport(Darwin) pthread_setname_np("Validation Diagram Clock Driver") +#endif work(self) } func join() { +#if canImport(Darwin) pthread_join(thread!, nil) +#elseif canImport(Glibc) + pthread_join(thread, nil) +#elseif canImport(WinSDK) +#error("TODO: Port TaskDriver threading to windows") +#endif } func enqueue(_ job: JobRef) { diff --git a/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift b/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift index 9a28674..95fd3be 100644 --- a/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift +++ b/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift @@ -12,6 +12,7 @@ import XCTest import AsyncAlgorithms +#if canImport(Darwin) final class TestThroughput: XCTestCase { func test_chain2() async { await measureSequenceThroughput(output: 1) { @@ -64,3 +65,4 @@ final class TestThroughput: XCTestCase { } } } +#endif diff --git a/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift b/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift index 8ffc3b1..c1fc811 100644 --- a/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift +++ b/Tests/AsyncAlgorithmsTests/Performance/ThroughputMeasurement.swift @@ -13,6 +13,7 @@ import AsyncAlgorithms import Foundation @preconcurrency import XCTest +#if canImport(Darwin) public struct InfiniteAsyncSequence: AsyncSequence, Sendable { public typealias Element = Value let value: Value @@ -138,3 +139,4 @@ final class TestMeasurements: XCTestCase { } } } +#endif diff --git a/Tests/AsyncAlgorithmsTests/Support/Asserts.swift b/Tests/AsyncAlgorithmsTests/Support/Asserts.swift index 160f74c..a0f84db 100644 --- a/Tests/AsyncAlgorithmsTests/Support/Asserts.swift +++ b/Tests/AsyncAlgorithmsTests/Support/Asserts.swift @@ -150,13 +150,3 @@ fileprivate func ==(_ lhs: [(A, B, C)] public func XCTAssertEqual(_ expression1: @autoclosure () throws -> [(A, B, C)], _ expression2: @autoclosure () throws -> [(A, B, C)], _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) { _XCTAssertEqual(expression1, expression2, { $0 == $1 }, message, file: file, line: line) } - -extension XCTestCase { - func expectFailures(_ failures: Set) { - let options = XCTExpectedFailure.Options() - options.issueMatcher = { issue in - return failures.contains(issue.compactDescription) - } - XCTExpectFailure(nil, options: options) - } -} diff --git a/Tests/AsyncAlgorithmsTests/Support/ValidationTest.swift b/Tests/AsyncAlgorithmsTests/Support/ValidationTest.swift index 1916841..be25cd4 100644 --- a/Tests/AsyncAlgorithmsTests/Support/ValidationTest.swift +++ b/Tests/AsyncAlgorithmsTests/Support/ValidationTest.swift @@ -14,9 +14,19 @@ import AsyncAlgorithms import AsyncSequenceValidation extension XCTestCase { - public func validate(theme: Theme, @AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test, file: StaticString = #file, line: UInt = #line) { - let baseLocation = XCTSourceCodeLocation(filePath: file.description, lineNumber: Int(line)) - let baseContext = XCTSourceCodeContext(location: baseLocation) + func recordFailure(_ description: String, detail: String? = nil, system: Bool = false, at location: AsyncSequenceValidation.SourceLocation) { +#if canImport(Darwin) + let context = XCTSourceCodeContext(location: XCTSourceCodeLocation(filePath: location.file.description, lineNumber: Int(location.line))) + let issue = XCTIssue(type: system ? .system : .assertionFailure, compactDescription: description, detailedDescription: detail, sourceCodeContext: context, associatedError: nil, attachments: []) + record(issue) +#else + XCTFail(description, file: location.file, line: location.line) +#endif + } + + func validate(theme: Theme, expectedFailures: Set, @AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test, file: StaticString = #file, line: UInt = #line) { + var expectations = expectedFailures + let baseLoc = AsyncSequenceValidation.SourceLocation(file: file, line: line) do { let (result, failures) = try AsyncSequenceValidationDiagram.test(theme: theme, build) var detail: String? @@ -33,30 +43,30 @@ extension XCTestCase { print(result.reconstituteActual(theme: theme)) } for failure in failures { - if let specification = failure.specification { - let location = XCTSourceCodeLocation(filePath: specification.location.file.description, lineNumber: Int(specification.location.line)) - let context = XCTSourceCodeContext(location: location) - let issue = XCTIssue(type: .assertionFailure, compactDescription: failure.description, detailedDescription: detail, sourceCodeContext: context, associatedError: nil, attachments: []) - record(issue) - } else { - let issue = XCTIssue(type: .assertionFailure, compactDescription: failure.description, detailedDescription: detail, sourceCodeContext: baseContext, associatedError: nil, attachments: []) - record(issue) + if expectations.remove(failure.description) == nil { + recordFailure(failure.description, detail: detail, at: failure.specification?.location ?? baseLoc) } } } catch { - if let sourceFailure = error as? SourceFailure { - let location = XCTSourceCodeLocation(filePath: sourceFailure.location.file.description, lineNumber: Int(sourceFailure.location.line)) - let context = XCTSourceCodeContext(location: location) - let issue = XCTIssue(type: .system, compactDescription: "\(error)", detailedDescription: nil, sourceCodeContext: context, associatedError: nil, attachments: []) - record(issue) - } else { - let issue = XCTIssue(type: .system, compactDescription: "\(error)", detailedDescription: nil, sourceCodeContext: baseContext, associatedError: nil, attachments: []) - record(issue) + if expectations.remove("\(error)") == nil { + recordFailure("\(error)", system: true, at: (error as? SourceFailure)?.location ?? baseLoc) } } + // any remaining expectations are failures that were expected but did not happen + for expectation in expectations { + XCTFail("Expected failure: \(expectation) did not occur.", file: file, line: line) + } + } + + func validate(expectedFailures: Set, @AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test, file: StaticString = #file, line: UInt = #line) { + validate(theme: .ascii, expectedFailures: expectedFailures, build, file: file, line: line) + } + + public func validate(theme: Theme, @AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test, file: StaticString = #file, line: UInt = #line) { + validate(theme: theme, expectedFailures: [], build, file: file, line: line) } public func validate(@AsyncSequenceValidationDiagram _ build: (AsyncSequenceValidationDiagram) -> Test, file: StaticString = #file, line: UInt = #line) { - validate(theme: .ascii, build, file: file, line: line) + validate(theme: .ascii, expectedFailures: [], build, file: file, line: line) } } diff --git a/Tests/AsyncAlgorithmsTests/TestBuffer.swift b/Tests/AsyncAlgorithmsTests/TestBuffer.swift index bfc998b..57d9d26 100644 --- a/Tests/AsyncAlgorithmsTests/TestBuffer.swift +++ b/Tests/AsyncAlgorithmsTests/TestBuffer.swift @@ -254,17 +254,17 @@ final class TestBuffer: XCTestCase { return result } - try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 10) + try? await Task.sleep(nanoseconds: 100_000_000) values.advance() values.advance() values.advance() - try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 10) + try? await Task.sleep(nanoseconds: 100_000_000) values.advance() values.advance() values.advance() - try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 10) + try? await Task.sleep(nanoseconds: 100_000_000) values.advance() values.advance() values.advance() @@ -320,17 +320,17 @@ final class TestBuffer: XCTestCase { return (result, err) } - try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 10) + try? await Task.sleep(nanoseconds: 100_000_000) values.advance() values.advance() values.advance() - try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 10) + try? await Task.sleep(nanoseconds: 100_000_000) values.advance() values.advance() values.advance() - try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 10) + try? await Task.sleep(nanoseconds: 100_000_000) values.advance() let task1Results = await task1.value @@ -406,17 +406,17 @@ final class TestBuffer: XCTestCase { return (result, err) } - try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 10) + try? await Task.sleep(nanoseconds: 100_000_000) values.advance() values.advance() values.advance() - try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 10) + try? await Task.sleep(nanoseconds: 100_000_000) values.advance() values.advance() values.advance() - try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 10) + try? await Task.sleep(nanoseconds: 100_000_000) values.advance() let task1Results = await task1.value diff --git a/Tests/AsyncAlgorithmsTests/TestTaskSelect.swift b/Tests/AsyncAlgorithmsTests/TestTaskSelect.swift index e938e06..d054257 100644 --- a/Tests/AsyncAlgorithmsTests/TestTaskSelect.swift +++ b/Tests/AsyncAlgorithmsTests/TestTaskSelect.swift @@ -10,6 +10,7 @@ //===----------------------------------------------------------------------===// @preconcurrency import XCTest +import Dispatch import AsyncAlgorithms final class TestTaskSelect: XCTestCase { @@ -17,7 +18,7 @@ final class TestTaskSelect: XCTestCase { let firstValue = await Task.select(Task { return 1 }, Task { - try! await Task.sleep(nanoseconds: NSEC_PER_SEC * 2) + try! await Task.sleep(until: .now + .seconds(2), clock: .continuous) return 2 }).value XCTAssertEqual(firstValue, 1) @@ -25,7 +26,7 @@ final class TestTaskSelect: XCTestCase { func test_second() async { let firstValue = await Task.select(Task { - try! await Task.sleep(nanoseconds: NSEC_PER_SEC * 2) + try! await Task.sleep(until: .now + .seconds(2), clock: .continuous) return 1 }, Task { return 2 @@ -36,7 +37,7 @@ final class TestTaskSelect: XCTestCase { func test_throwing() async { do { _ = try await Task.select(Task { () async throws -> Int in - try await Task.sleep(nanoseconds: NSEC_PER_SEC * 2) + try await Task.sleep(until: .now + .seconds(2), clock: .continuous) return 1 }, Task { () async throws -> Int in throw NSError(domain: NSCocoaErrorDomain, code: -1, userInfo: nil) @@ -58,7 +59,7 @@ final class TestTaskSelect: XCTestCase { firstCancelled.fulfill() } operation: { () -> Int in firstReady.fulfill() - try? await Task.sleep(nanoseconds: NSEC_PER_SEC * 2) + try? await Task.sleep(until: .now + .seconds(2), clock: .continuous) return 1 } }, Task { @@ -66,7 +67,7 @@ final class TestTaskSelect: XCTestCase { secondCancelled.fulfill() } operation: { () -> Int in secondReady.fulfill() - try? await Task.sleep(nanoseconds: NSEC_PER_SEC * 2) + try? await Task.sleep(until: .now + .seconds(2), clock: .continuous) return 1 } }) diff --git a/Tests/AsyncAlgorithmsTests/TestValidationTests.swift b/Tests/AsyncAlgorithmsTests/TestValidationTests.swift index 2ae3a2f..2f42c5f 100644 --- a/Tests/AsyncAlgorithmsTests/TestValidationTests.swift +++ b/Tests/AsyncAlgorithmsTests/TestValidationTests.swift @@ -118,8 +118,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_mismatch_value() { - expectFailures(["expected \"X\" but got \"C\" at tick 6"]) - validate { + validate(expectedFailures: ["expected \"X\" but got \"C\" at tick 6"]) { "a--b--c---|" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B--X---|" @@ -127,9 +126,8 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_value_for_finish() { - expectFailures(["expected finish but got \"C\" at tick 6", - "unexpected finish at tick 10"]) - validate { + validate(expectedFailures: ["expected finish but got \"C\" at tick 6", + "unexpected finish at tick 10"]) { "a--b--c---|" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B--|" @@ -137,9 +135,8 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_finish_for_value() { - expectFailures(["expected \"C\" but got finish at tick 6", - "expected finish at tick 7"]) - validate { + validate(expectedFailures: ["expected \"C\" but got finish at tick 6", + "expected finish at tick 7"]) { "a--b--|" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B--C|" @@ -147,8 +144,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_finish_for_error() { - expectFailures(["expected failure but got finish at tick 6"]) - validate { + validate(expectedFailures: ["expected failure but got finish at tick 6"]) { "a--b--|" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B--^" @@ -156,8 +152,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_error_for_finish() { - expectFailures(["expected finish but got failure at tick 6"]) - validate { + validate(expectedFailures: ["expected finish but got failure at tick 6"]) { "a--b--^" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B--|" @@ -165,9 +160,8 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_value_for_error() { - expectFailures(["expected failure but got \"C\" at tick 6", - "unexpected finish at tick 7"]) - validate { + validate(expectedFailures: ["expected failure but got \"C\" at tick 6", + "unexpected finish at tick 7"]) { "a--b--c|" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B--^" @@ -175,9 +169,8 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_error_for_value() { - expectFailures(["expected \"C\" but got failure at tick 6", - "expected finish at tick 7"]) - validate { + validate(expectedFailures: ["expected \"C\" but got failure at tick 6", + "expected finish at tick 7"]) { "a--b--^" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B--C|" @@ -185,9 +178,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_expected_value() { - expectFailures(["expected \"C\" at tick 6", - "unexpected finish at tick 7"]) - validate { + validate(expectedFailures: ["expected \"C\" at tick 6"]) { "a--b---|" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B--C|" @@ -195,9 +186,8 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_expected_failure() { - expectFailures(["expected failure at tick 6", - "unexpected finish at tick 7"]) - validate { + validate(expectedFailures: ["expected failure at tick 6", + "unexpected finish at tick 7"]) { "a--b---|" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B--^" @@ -205,8 +195,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_unexpected_value() { - expectFailures(["unexpected \"C\" at tick 6"]) - validate { + validate(expectedFailures: ["unexpected \"C\" at tick 6"]) { "a--b--c|" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B---|" @@ -214,9 +203,8 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_failure_unexpected_failure() { - expectFailures(["unexpected failure at tick 6", - "expected finish at tick 7"]) - validate { + validate(expectedFailures: ["unexpected failure at tick 6", + "expected finish at tick 7"]) { "a--b--^|" $0.inputs[0].map { item in await Task { item.capitalized }.value } "A--B---|" @@ -224,8 +212,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_parse_failure_unbalanced_group() { - expectFailures(["validation diagram unbalanced grouping"]) - validate { + validate(expectedFailures: ["validation diagram unbalanced grouping"]) { " ab|" $0.inputs[0] "[ab|" @@ -233,8 +220,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_parse_failure_unbalanced_group_input() { - expectFailures(["validation diagram unbalanced grouping"]) - validate { + validate(expectedFailures: ["validation diagram unbalanced grouping"]) { "[ab|" $0.inputs[0] " ab|" @@ -242,8 +228,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_parse_failure_nested_group() { - expectFailures(["validation diagram nested grouping"]) - validate { + validate(expectedFailures: ["validation diagram nested grouping"]) { " ab|" $0.inputs[0] "[[ab|" @@ -251,8 +236,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_parse_failure_nested_group_input() { - expectFailures(["validation diagram nested grouping"]) - validate { + validate(expectedFailures: ["validation diagram nested grouping"]) { "[[ab|" $0.inputs[0] " ab|" @@ -260,8 +244,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_parse_failure_step_in_group() { - expectFailures(["validation diagram step symbol in group"]) - validate { + validate(expectedFailures: ["validation diagram step symbol in group"]) { " ab|" $0.inputs[0] "[a-]b|" @@ -269,8 +252,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_parse_failure_step_in_group_input() { - expectFailures(["validation diagram step symbol in group"]) - validate { + validate(expectedFailures: ["validation diagram step symbol in group"]) { "[a-]b|" $0.inputs[0] " ab|" @@ -278,8 +260,7 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_specification_produce_past_end() { - expectFailures(["specification violation got \"d\" after iteration terminated at tick 9"]) - validate { + validate(expectedFailures: ["specification violation got \"d\" after iteration terminated at tick 9"]) { "a--b--c--|" $0.inputs[0].violatingSpecification(returningPastEndIteration: "d") "a--b--c--|" @@ -287,14 +268,13 @@ final class TestValidationDiagram: XCTestCase { } func test_diagram_specification_throw_past_end() { - expectFailures(["specification violation got failure after iteration terminated at tick 9"]) - validate { + validate(expectedFailures: ["specification violation got failure after iteration terminated at tick 9"]) { "a--b--c--|" $0.inputs[0].violatingSpecification(throwingPastEndIteration: Failure()) "a--b--c--|" } } - + func test_delayNext() { validate { "xxx--- |"