From 20feaf7089cd2bd58abdb0ce472d699d5a24e6fc Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Thu, 20 Jul 2023 09:50:12 -0300 Subject: [PATCH] Fix tests --- .../PostgRESTTests/BuildURLRequestTests.swift | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/Tests/PostgRESTTests/BuildURLRequestTests.swift b/Tests/PostgRESTTests/BuildURLRequestTests.swift index 3bd7383..ad8ed18 100644 --- a/Tests/PostgRESTTests/BuildURLRequestTests.swift +++ b/Tests/PostgRESTTests/BuildURLRequestTests.swift @@ -114,20 +114,36 @@ XCTAssertNotNil(clientInfoHeader) } } -#endif final class LockIsolated: @unchecked Sendable { - private let lock = NSLock() - private var value: Value + private let lock = NSRecursiveLock() + private var _value: Value init(_ value: Value) { - self.value = value + self._value = value + } + + @discardableResult + func withValue(_ block: (inout Value) throws -> T) rethrows -> T { + try lock.sync { + var value = self._value + defer { self._value = value } + return try block(&value) + } } + var value: Value { + lock.sync { self._value } + } +} + +extension NSRecursiveLock { @discardableResult - func withValue(_ block: (inout Value) -> T) -> T { - lock.lock() - defer { lock.unlock() } - return block(&value) + func sync(work: () throws -> R) rethrows -> R { + lock() + defer { unlock() } + return try work() } } + +#endif