StrictConcurrency/tests: fix ValueObservationRecorder warnings

This commit is contained in:
Gwendal Roué committed 2024-07-27 16:37:33 +02:00
1 parent a45ba5a6e0
commit a12211bc28
3 files changed
+105 -70

No files matched your search

@@ -31,7 +31,7 @@ class ValueObservationQueryInterfaceRequestTests: GRDBTestCase {
}
}
private func performDatabaseModifications(_ db: Database) throws {
private let performDatabaseModifications: @Sendable (Database) throws -> Void = { db in
try db.inTransaction {
try db.execute(sql: """
INSERT INTO parent (id, name) VALUES (1, 'foo');
+24 -27
View File
@@ -222,8 +222,8 @@ extension ValueObservation {
public func record(
in reader: some DatabaseReader,
scheduling scheduler: some ValueObservationScheduler = .async(onQueue: .main),
onError: ((Error) -> Void)? = nil,
onChange: ((Reducer.Value) -> Void)? = nil)
onError: (@Sendable (Error) -> Void)? = nil,
onChange: (@Sendable (Reducer.Value) -> Void)? = nil)
-> ValueObservationRecorder<Reducer.Value>
where Reducer: ValueReducer
{
@@ -234,11 +234,11 @@ extension ValueObservation {
onError: { [weak recorder] in
onError?($0)
recorder?.onError($0)
},
},
onChange: { [weak recorder] in
onChange?($0)
recorder?.onChange($0)
})
})
recorder.receive(cancellable)
return recorder
}
@@ -359,32 +359,31 @@ extension GRDBTestCase {
_ observation: ValueObservation<Reducer>,
records expectedValues: [Reducer.Value],
setup: (Database) throws -> Void,
recordedUpdates: @escaping (Database) throws -> Void,
recordedUpdates: @escaping @Sendable (Database) throws -> Void,
file: StaticString = #file,
line: UInt = #line)
throws
where Reducer.Value: Equatable
throws where Reducer.Value: Equatable, Reducer.Value: Sendable
{
func test(
observation: ValueObservation<Reducer>,
scheduling scheduler: some ValueObservationScheduler,
testValueDispatching: @escaping () -> Void) throws
testValueDispatching: @escaping @Sendable () -> Void) throws
{
func testRecordingEqualWhenWriteAfterStart(writer: some DatabaseWriter) throws {
try writer.write(setup)
var value: Reducer.Value?
let valueMutex: Mutex<Reducer.Value?> = Mutex(nil)
let recorder = observation.record(
in: writer,
scheduling: scheduler,
onChange: {
testValueDispatching()
value = $0
valueMutex.store($0)
})
// Test that initial value is set when scheduler is immediate
if scheduler.immediateInitialValue() {
XCTAssertNotNil(value)
XCTAssertNotNil(valueMutex.load())
}
// Perform writes after start
@@ -400,24 +399,23 @@ extension GRDBTestCase {
func testRecordingEqualWhenWriteAfterFirstValue(writer: some DatabaseWriter) throws {
try writer.write(setup)
var valueCount = 0
var value: Reducer.Value?
let valueCountMutex = Mutex(0)
let valueMutex: Mutex<Reducer.Value?> = Mutex(nil)
let recorder = observation.record(
in: writer,
scheduling: scheduler,
onChange: { [unowned writer] in
testValueDispatching()
valueCount += 1
if valueCount == 1 {
if valueCountMutex.increment() == 1 {
// Perform writes after initial value
try! writer.writeWithoutTransaction(recordedUpdates)
}
value = $0
valueMutex.store($0)
})
// Test that initial value is set when scheduler is immediate
if scheduler.immediateInitialValue() {
XCTAssertNotNil(value)
XCTAssertNotNil(valueMutex.load())
}
let expectation = recorder.next(expectedValues.count)
@@ -430,18 +428,18 @@ extension GRDBTestCase {
func testRecordingMatchWhenWriteAfterStart(writer: some DatabaseWriter) throws {
try writer.write(setup)
var value: Reducer.Value?
let valueMutex: Mutex<Reducer.Value?> = Mutex(nil)
let recorder = observation.record(
in: writer,
scheduling: scheduler,
onChange: {
testValueDispatching()
value = $0
valueMutex.store($0)
})
// Test that initial value is set when scheduler is immediate
if scheduler.immediateInitialValue() {
XCTAssertNotNil(value)
XCTAssertNotNil(valueMutex.load())
}
try writer.writeWithoutTransaction(recordedUpdates)
@@ -475,24 +473,23 @@ extension GRDBTestCase {
func testRecordingMatchWhenWriteAfterFirstValue(writer: some DatabaseWriter) throws {
try writer.write(setup)
var valueCount = 0
var value: Reducer.Value?
let valueCountMutex = Mutex(0)
let valueMutex: Mutex<Reducer.Value?> = Mutex(nil)
let recorder = observation.record(
in: writer,
scheduling: scheduler,
onChange: { [unowned writer] in
testValueDispatching()
valueCount += 1
if valueCount == 1 {
if valueCountMutex.increment() == 1 {
// Perform writes after initial value
try! writer.writeWithoutTransaction(recordedUpdates)
}
value = $0
valueMutex.store($0)
})
// Test that initial value is set when scheduler is immediate
if scheduler.immediateInitialValue() {
XCTAssertNotNil(value)
XCTAssertNotNil(valueMutex.load())
}
let recordedValues: [Reducer.Value]
@@ -578,7 +575,7 @@ extension GRDBTestCase {
func test(
observation: ValueObservation<Reducer>,
scheduling scheduler: some ValueObservationScheduler,
testErrorDispatching: @escaping () -> Void) throws
testErrorDispatching: @escaping @Sendable () -> Void) throws
{
func test(writer: some DatabaseWriter) throws {
try writer.write(setup)
+80 -42
View File
@@ -6,16 +6,20 @@ class ValueObservationRowTests: GRDBTestCase {
let request = SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id")
try assertValueObservation(
ValueObservation.trackingConstantRegion(request.fetchAll),
ValueObservation.trackingConstantRegion {
// Compare Row descriptions in order to avoid concurrency warnings
try request.fetchAll($0).map { $0.description }
},
records: [
[],
[["id":1, "name":"foo"]],
[["id":1, "name":"foo"]],
[["id":1, "name":"foo"], ["id":2, "name":"bar"]],
[["id":2, "name":"bar"]]],
[#"[id:1 name:"foo"]"#],
[#"[id:1 name:"foo"]"#],
[#"[id:1 name:"foo"]"#, #"[id:2 name:"bar"]"#],
[#"[id:2 name:"bar"]"#],
],
setup: { db in
try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)")
},
},
recordedUpdates: { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')")
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1")
@@ -26,18 +30,24 @@ class ValueObservationRowTests: GRDBTestCase {
return .commit
}
try db.execute(sql: "DELETE FROM t WHERE id = 1")
})
})
try assertValueObservation(
ValueObservation.trackingConstantRegion(request.fetchAll).removeDuplicates(),
ValueObservation
.trackingConstantRegion {
// Compare Row descriptions in order to avoid concurrency warnings
try request.fetchAll($0).map { $0.description }
}
.removeDuplicates(),
records: [
[],
[["id":1, "name":"foo"]],
[["id":1, "name":"foo"], ["id":2, "name":"bar"]],
[["id":2, "name":"bar"]]],
[#"[id:1 name:"foo"]"#],
[#"[id:1 name:"foo"]"#, #"[id:2 name:"bar"]"#],
[#"[id:2 name:"bar"]"#],
],
setup: { db in
try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)")
},
},
recordedUpdates: { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')")
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1")
@@ -48,24 +58,28 @@ class ValueObservationRowTests: GRDBTestCase {
return .commit
}
try db.execute(sql: "DELETE FROM t WHERE id = 1")
})
})
}
func testOne() throws {
let request = SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id DESC")
try assertValueObservation(
ValueObservation.trackingConstantRegion(request.fetchOne),
ValueObservation.trackingConstantRegion {
// Compare Row description in order to avoid concurrency warnings
try request.fetchOne($0).map { $0.description }
},
records: [
nil,
["id":1, "name":"foo"],
["id":1, "name":"foo"],
["id":2, "name":"bar"],
#"[id:1 name:"foo"]"#,
#"[id:1 name:"foo"]"#,
#"[id:2 name:"bar"]"#,
nil,
["id":3, "name":"toto"]],
#"[id:3 name:"toto"]"#,
],
setup: { db in
try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)")
},
},
recordedUpdates: { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')")
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1")
@@ -77,19 +91,25 @@ class ValueObservationRowTests: GRDBTestCase {
}
try db.execute(sql: "DELETE FROM t")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'toto')")
})
})
try assertValueObservation(
ValueObservation.trackingConstantRegion(request.fetchOne).removeDuplicates(),
ValueObservation
.trackingConstantRegion {
// Compare Row description in order to avoid concurrency warnings
try request.fetchOne($0).map { $0.description }
}
.removeDuplicates(),
records: [
nil,
["id":1, "name":"foo"],
["id":2, "name":"bar"],
#"[id:1 name:"foo"]"#,
#"[id:2 name:"bar"]"#,
nil,
["id":3, "name":"toto"]],
#"[id:3 name:"toto"]"#,
],
setup: { db in
try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)")
},
},
recordedUpdates: { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')")
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1")
@@ -101,29 +121,41 @@ class ValueObservationRowTests: GRDBTestCase {
}
try db.execute(sql: "DELETE FROM t")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'toto')")
})
})
}
func testFTS4Observation() throws {
try assertValueObservation(
ValueObservation.trackingConstantRegion(SQLRequest<Row>(sql: "SELECT * FROM ft_documents").fetchAll),
ValueObservation.trackingConstantRegion {
// Compare Row descriptions in order to avoid concurrency warnings
try SQLRequest<Row>(sql: "SELECT * FROM ft_documents")
.fetchAll($0)
.map { $0.description }
},
records: [
[],
[["content":"foo"]]],
[#"[content:"foo"]"#],
],
setup: { db in
try db.create(virtualTable: "ft_documents", using: FTS4())
},
},
recordedUpdates: { db in
try db.execute(sql: "INSERT INTO ft_documents VALUES (?)", arguments: ["foo"])
})
})
}
func testSynchronizedFTS4Observation() throws {
try assertValueObservation(
ValueObservation.trackingConstantRegion(SQLRequest<Row>(sql: "SELECT * FROM ft_documents").fetchAll),
ValueObservation.trackingConstantRegion {
// Compare Row descriptions in order to avoid concurrency warnings
try SQLRequest<Row>(sql: "SELECT * FROM ft_documents")
.fetchAll($0)
.map { $0.description }
},
records: [
[],
[["content":"foo"]]],
[#"[content:"foo"]"#],
],
setup: { db in
try db.create(table: "documents") { t in
t.primaryKey("id", .integer)
@@ -133,22 +165,28 @@ class ValueObservationRowTests: GRDBTestCase {
t.synchronize(withTable: "documents")
t.column("content")
}
},
},
recordedUpdates: { db in
try db.execute(sql: "INSERT INTO documents (content) VALUES (?)", arguments: ["foo"])
})
})
}
func testJoinedFTS4Observation() throws {
try assertValueObservation(
ValueObservation.trackingConstantRegion(SQLRequest<Row>(sql: """
SELECT document.* FROM document
JOIN ft_document ON ft_document.rowid = document.id
WHERE ft_document MATCH 'foo'
""").fetchAll),
ValueObservation.trackingConstantRegion {
// Compare Row descriptions in order to avoid concurrency warnings
try SQLRequest<Row>(sql: """
SELECT document.* FROM document
JOIN ft_document ON ft_document.rowid = document.id
WHERE ft_document MATCH 'foo'
""")
.fetchAll($0)
.map { $0.description }
},
records: [
[],
[["id":1]]],
[#"[id:1]"#],
],
setup: { db in
try db.create(table: "document") { t in
t.autoIncrementedPrimaryKey("id")
@@ -156,13 +194,13 @@ class ValueObservationRowTests: GRDBTestCase {
try db.create(virtualTable: "ft_document", using: FTS4()) { t in
t.column("content")
}
},
},
recordedUpdates: { db in
try db.inTransaction {
try db.execute(sql: "INSERT INTO document (id) VALUES (?)", arguments: [1])
try db.execute(sql: "INSERT INTO ft_document (rowid, content) VALUES (?, ?)", arguments: [1, "foo"])
return .commit
}
})
})
}
}