StrictConcurrency: ValueObservation requires Sendable fetching closures

This commit is contained in:
Gwendal Roué authored and Gwendal Roué committed 2024-07-27 16:37:33 +02:00
1 parent 45539583a8
commit 8d137a2d16
4 files changed
+27 -33

No files matched your search

+4 -4
View File
@@ -733,7 +733,7 @@ extension ValueObservation {
///
/// - parameter fetch: The closure that fetches the observed value.
public static func trackingConstantRegion<Value>(
_ fetch: @escaping (Database) throws -> Value)
_ fetch: @escaping @Sendable (Database) throws -> Value)
-> Self
where Reducer == ValueReducers.Fetch<Value>
{
@@ -807,7 +807,7 @@ extension ValueObservation {
public static func tracking<Value>(
region: any DatabaseRegionConvertible,
_ otherRegions: any DatabaseRegionConvertible...,
fetch: @escaping (Database) throws -> Value)
fetch: @escaping @Sendable (Database) throws -> Value)
-> Self
where Reducer == ValueReducers.Fetch<Value>
{
@@ -876,7 +876,7 @@ extension ValueObservation {
/// - parameter fetch: The closure that fetches the observed value.
public static func tracking<Value>(
regions: [any DatabaseRegionConvertible],
fetch: @escaping (Database) throws -> Value)
fetch: @escaping @Sendable (Database) throws -> Value)
-> Self
where Reducer == ValueReducers.Fetch<Value>
{
@@ -933,7 +933,7 @@ extension ValueObservation {
///
/// - parameter fetch: The closure that fetches the observed value.
public static func tracking<Value>(
_ fetch: @escaping (Database) throws -> Value)
_ fetch: @escaping @Sendable (Database) throws -> Value)
-> Self
where Reducer == ValueReducers.Fetch<Value>
{
@@ -539,10 +539,10 @@ class SharedValueObservationTests: GRDBTestCase {
}
let log = Log()
var fetchError: Error? = nil
let fetchErrorMutex: Mutex<Error?> = Mutex(nil)
let publisher = ValueObservation
.tracking { db -> Int in
if let error = fetchError { throw error }
if let error = fetchErrorMutex.value { throw error }
return try Table("player").fetchCount(db)
}
.print(to: log)
@@ -556,7 +556,7 @@ class SharedValueObservationTests: GRDBTestCase {
try XCTAssertEqual(wait(for: recorder1.next(), timeout: 1), 0)
try XCTAssertEqual(wait(for: recorder2.next(), timeout: 1), 0)
fetchError = TestError()
fetchErrorMutex.value = TestError()
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES")}
if case .finished = try wait(for: recorder1.completion, timeout: 1) { XCTFail("Expected error") }
@@ -573,7 +573,7 @@ class SharedValueObservationTests: GRDBTestCase {
}
do {
fetchError = nil
fetchErrorMutex.value = nil
let recorder = publisher.record()
if case .finished = try wait(for: recorder.completion, timeout: 1) { XCTFail("Expected error") }
XCTAssertEqual(log.flush(), [])
@@ -595,10 +595,10 @@ class SharedValueObservationTests: GRDBTestCase {
}
let log = Log()
var fetchError: Error? = nil
let fetchErrorMutex: Mutex<Error?> = Mutex(nil)
let publisher = ValueObservation
.tracking { db -> Int in
if let error = fetchError { throw error }
if let error = fetchErrorMutex.value { throw error }
return try Table("player").fetchCount(db)
}
.print(to: log)
@@ -612,7 +612,7 @@ class SharedValueObservationTests: GRDBTestCase {
try XCTAssertEqual(wait(for: recorder1.next(), timeout: 1), 0)
try XCTAssertEqual(wait(for: recorder2.next(), timeout: 1), 0)
fetchError = TestError()
fetchErrorMutex.value = TestError()
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES")}
if case .finished = try wait(for: recorder1.completion, timeout: 1) { XCTFail("Expected error") }
@@ -629,7 +629,7 @@ class SharedValueObservationTests: GRDBTestCase {
}
do {
fetchError = nil
fetchErrorMutex.value = nil
let recorder = publisher.record()
try XCTAssertEqual(wait(for: recorder.next(), timeout: 1), 1)
XCTAssertEqual(log.flush(), ["start", "fetch", "tracked region: player(*)", "value: 1"])
@@ -411,11 +411,10 @@ class ValueObservationPrintTests: GRDBTestCase {
// Force DatabasePool to perform two initial fetches, because between
// its first read access, and its write access that installs the
// transaction observer, some write did happen.
var needsChange = true
let needsChangeMutex = Mutex(true)
let observation = ValueObservation
.trackingConstantRegion { db -> Int? in
if needsChange {
needsChange = false
if needsChangeMutex.wasNeeded {
try dbPool.write { db in
try db.execute(sql: """
INSERT INTO player DEFAULT VALUES;
@@ -462,11 +461,10 @@ class ValueObservationPrintTests: GRDBTestCase {
// Force DatabasePool to perform two initial fetches, because between
// its first read access, and its write access that installs the
// transaction observer, some write did happen.
var needsChange = true
let needsChangeMutex = Mutex(true)
let observation = ValueObservation
.trackingConstantRegion { db -> Int? in
if needsChange {
needsChange = false
if needsChangeMutex.wasNeeded {
try dbPool.write { db in
try db.execute(sql: """
INSERT INTO player DEFAULT VALUES;
+11 -15
View File
@@ -64,10 +64,10 @@ class ValueObservationTests: GRDBTestCase {
notificationExpectation.expectedFulfillmentCount = 4
notificationExpectation.isInverted = true
var nextError: Error? = nil // If not null, observation throws an error
let nextErrorMutex: Mutex<Error?> = Mutex(nil) // If not null, observation throws an error
let observation = ValueObservation.trackingConstantRegion {
_ = try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")
if let error = nextError {
if let error = nextErrorMutex.value {
throw error
}
}
@@ -82,7 +82,7 @@ class ValueObservationTests: GRDBTestCase {
},
onChange: {
XCTAssertFalse(didFailMutex.value)
nextError = TestError()
nextErrorMutex.value = TestError()
notificationExpectation.fulfill()
// Trigger another change
try! dbWriter.writeWithoutTransaction { db in
@@ -353,10 +353,9 @@ class ValueObservationTests: GRDBTestCase {
// Force DatabasePool to perform two initial fetches, because between
// its first read access, and its write access that installs the
// transaction observer, some write did happen.
var needsChange = true
let needsChangeMutex = Mutex(true)
let observation = ValueObservation.trackingConstantRegion { db -> Int in
if needsChange {
needsChange = false
if needsChangeMutex.wasNeeded {
try dbPool.write { db in
try db.execute(sql: """
INSERT INTO t DEFAULT VALUES;
@@ -393,10 +392,9 @@ class ValueObservationTests: GRDBTestCase {
// Force DatabasePool to perform two initial fetches, because between
// its first read access, and its write access that installs the
// transaction observer, some write did happen.
var needsChange = true
let needsChangeMutex = Mutex(true)
let observation = ValueObservation.trackingConstantRegion { db -> Int in
if needsChange {
needsChange = false
if needsChangeMutex.wasNeeded {
try dbPool.write { db in
try db.execute(sql: """
INSERT INTO t DEFAULT VALUES;
@@ -433,10 +431,9 @@ class ValueObservationTests: GRDBTestCase {
// Allow pool to perform a single initial fetch, because between
// its first read access, and its write access that installs the
// transaction observer, no write did happen.
var needsChange = true
let needsChangeMutex = Mutex(true)
let observation = ValueObservation.trackingConstantRegion { db -> Int in
if needsChange {
needsChange = false
if needsChangeMutex.wasNeeded {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
try! dbPool.write { db in
try db.execute(sql: """
@@ -483,10 +480,9 @@ class ValueObservationTests: GRDBTestCase {
// Allow pool to perform a single initial fetch, because between
// its first read access, and its write access that installs the
// transaction observer, no write did happen.
var needsChange = true
let needsChangeMutex = Mutex(true)
let observation = ValueObservation.trackingConstantRegion { db -> Int in
if needsChange {
needsChange = false
if needsChangeMutex.wasNeeded {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
try! dbPool.write { db in
try db.execute(sql: """