diff --git a/Tests/BasicsTests/ConcurrencyHelpersTests.swift b/Tests/BasicsTests/ConcurrencyHelpersTests.swift index e3ae5616b8e..9661a3a0812 100644 --- a/Tests/BasicsTests/ConcurrencyHelpersTests.swift +++ b/Tests/BasicsTests/ConcurrencyHelpersTests.swift @@ -36,16 +36,13 @@ struct ConcurrencyHelpersTest { lock.withLock { expected[index] = value } - cache.memoize(index) { - value - } - cache.memoize(index) { - Int.random(in: Int.min ..< Int.max) - } + cache.memoize(index) { value } + cache.memoize(index) { Int.random(in: Int.min ..< Int.max) } } } - try #require(sync.wait(timeout: .now() + .seconds(2)) == .success) + sync.wait() + expected.forEach { key, value in #expect(cache[key] == value) } @@ -71,149 +68,80 @@ struct ConcurrencyHelpersTest { cache.append(value) } } + sync.wait() - try #require(sync.wait(timeout: .now() + .seconds(2)) == .success) let expectedSorted = expected.sorted() - let resultsSorted = cache.get().sorted() + let resultsSorted = cache.get().sorted() #expect(expectedSorted == resultsSorted) } } - } - @Test - func threadSafeBox() throws { - let queue = DispatchQueue(label: "ConcurrencyHelpersTest", attributes: .concurrent) - for _ in 0 ..< 100 { - let sync = DispatchGroup() + @Test + func threadSafeBox() throws { + let queue = DispatchQueue(label: "ConcurrencyHelpersTest", attributes: .concurrent) + for _ in 0 ..< 100 { + let sync = DispatchGroup() - var winner: Int? - let lock = NSLock() + var winner: Int? + let lock = NSLock() - let serial = DispatchQueue(label: "testThreadSafeBoxSerial") + let serial = DispatchQueue(label: "testThreadSafeBoxSerial") - let cache = ThreadSafeBox() - for index in 0 ..< 1000 { - queue.async(group: sync) { - Thread.sleep(forTimeInterval: Double.random(in: 100 ... 300) * 1.0e-6) - serial.async(group: sync) { - lock.withLock { - if winner == nil { - winner = index + let cache = ThreadSafeBox() + for index in 0 ..< 1000 { + queue.async(group: sync) { + Thread.sleep(forTimeInterval: Double.random(in: 100 ... 300) * 1.0e-6) + serial.async(group: sync) { + lock.withLock { + if winner == nil { + winner = index + } } - } - cache.memoize { - index + cache.memoize { index } } } } - } - - try #require(sync.wait(timeout: .now() + .seconds(2)) == .success) - #expect(cache.get() == winner) - } - } + sync.wait() - @Suite - struct AsyncOperationQueueTests { - fileprivate actor ResultsTracker { - var results = [Int]() - var maxConcurrent = 0 - var currentConcurrent = 0 - - func incrementConcurrent() { - currentConcurrent += 1 - maxConcurrent = max(maxConcurrent, currentConcurrent) + #expect(cache.get() == winner) } + } - func decrementConcurrent() { - currentConcurrent -= 1 - } - func appendResult(_ value: Int) { - results.append(value) - } - } + @Suite + struct AsyncOperationQueueTests { + fileprivate actor ResultsTracker { + var results = [Int]() + var maxConcurrent = 0 + var currentConcurrent = 0 - @Test - func limitsConcurrentOperations() async throws { - let queue = AsyncOperationQueue(concurrentTasks: 5) - - let totalTasks = 20 - let tracker = ResultsTracker() - - try await withThrowingTaskGroup(of: Void.self) { group in - for index in 0.. string line + if let line = String(data: Data(bytes), encoding: .utf8)? + .trimmingCharacters(in: .newlines), + line == "sleeping" + { + startedExp.fulfill() + } + }, + stderr: { _ in /* ignore */ } + ) ) - sync.enter() try process.launch() - - // wait for the process to start - if case .timedOut = sync.wait(timeout: .now() + 60) { - return XCTFail("timeout waiting for process to start") - } - - // interrupt the process - print("interrupting") + wait(for: [startedExp], timeout: 10.0) process.signal(SIGINT) - // check for interrupt result let result = try process.waitUntilExit() -#if os(Windows) + #if os(Windows) XCTAssertEqual(result.exitStatus, .abnormal(exception: 2)) -#else + #else XCTAssertEqual(result.exitStatus, .signalled(signal: SIGINT)) -#endif + #endif } + } class OutputHandler { let sync: DispatchGroup @@ -248,7 +249,7 @@ class RunCommandTestCase: CommandsBuildProviderTestCase { } } -} + class RunCommandNativeTests: RunCommandTestCase { override open var buildSystemProvider: BuildSystemProvider.Kind {