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) + } +}