SPB Git

Repository Public

TestSupport.swift 1.4 KB 42 lines swift

1// ORIX DJ2// File: Packages/ORIXDJCore/Tests/ORIXMIDITests/TestSupport.swift3// Description: Shared helpers for the ORIXMIDI tests.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import Foundation9import Testing1011/// Equality check outside the `#expect` macro expansion: array literals of enum cases inside `#expect`12/// make the type checker time out.13func expectEqual<T: Equatable>(_ actual: T, _ expected: T, sourceLocation: SourceLocation = #_sourceLocation) {14    #expect(actual == expected, sourceLocation: sourceLocation)15}1617/// A lock-protected box for values written from CoreMIDI's thread and read by the test.18final class Locked<Value>: @unchecked Sendable {19    private let lock = NSLock()20    private var value: Value2122    init(_ value: Value) { self.value = value }2324    func withLock<T>(_ body: (inout Value) -> T) -> T {25        lock.lock()26        defer { lock.unlock() }27        return body(&value)28    }2930    var current: Value { withLock { $0 } }31}3233/// Polls `condition` until it holds or `timeout` seconds elapse; returns its final value.34func waitUntil(timeout: TimeInterval = 5, _ condition: @Sendable () -> Bool) async -> Bool {35    let deadline = Date().addingTimeInterval(timeout)36    while Date() < deadline {37        if condition() { return true }38        try? await Task.sleep(nanoseconds: 2_000_000)39    }40    return condition()41}42
Files main
Menu

Theme