From 141f5b4e1703cadf56fe8e53b8f964784c91aa84 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 9 Sep 2025 04:04:31 -0400 Subject: [PATCH] Make the tests Swift 6 ready. (#409) This little box type allows us to easily audit the correctness of the locking. --- .../BoringSSL/ECToolboxBoringSSLTests.swift | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/Tests/_CryptoExtrasTests/ECToolbox/BoringSSL/ECToolboxBoringSSLTests.swift b/Tests/_CryptoExtrasTests/ECToolbox/BoringSSL/ECToolboxBoringSSLTests.swift index 00dfdfff..0e9a9d5a 100644 --- a/Tests/_CryptoExtrasTests/ECToolbox/BoringSSL/ECToolboxBoringSSLTests.swift +++ b/Tests/_CryptoExtrasTests/ECToolbox/BoringSSL/ECToolboxBoringSSLTests.swift @@ -36,8 +36,7 @@ final class ECToolboxBoringSSLTests: XCTestCase { thisThreadFinished: XCTestExpectation )] = [] - var objectIdentifiers: [(threadID: Int, ffacID: ObjectIdentifier)] = [] - let lock = NSLock() + let objectIdentifiers: LockedBox<[(threadID: Int, ffacID: ObjectIdentifier)]> = .init(initialValue: []) for i in 1...numThreads { let thisThreadDidReads = expectation(description: "this thread did its reads") @@ -45,9 +44,9 @@ final class ECToolboxBoringSSLTests: XCTestCase { let thisThreadFinished = expectation(description: "this thread is finished") let thread = Thread { for _ in 1...numReadsPerThread { - lock.lock() - objectIdentifiers.append((i, ObjectIdentifier(Curve.__ffac))) - lock.unlock() + objectIdentifiers.withLockedValue { + $0.append((i, ObjectIdentifier(Curve.__ffac))) + } } thisThreadDidReads.fulfill() XCTWaiter().wait(for: [allThreadsDidReads], timeout: .greatestFiniteMagnitude) @@ -61,15 +60,39 @@ final class ECToolboxBoringSSLTests: XCTestCase { for thread in threads { thread.allThreadsDidReads.fulfill() } await fulfillment(of: threads.map(\.thisThreadFinished), timeout: 0.5) - XCTAssertEqual(objectIdentifiers.count, numThreads * numReadsPerThread) - for threadID in 1...numThreads { - let partitionBoundary = objectIdentifiers.partition(by: { $0.threadID == threadID }) - let otherThreadsObjIDs = objectIdentifiers[..: @unchecked Sendable { + private let lock: NSLock + private var value: Value + + init(initialValue: Value) { + self.value = initialValue + self.lock = NSLock() + } + + func withLockedValue(_ body: (inout Value) throws -> ReturnType) rethrows -> ReturnType { + self.lock.lock() + defer { + self.lock.unlock() + } + return try body(&self.value) + } +}