Test that a cancelled ValueObservation does not notify of changes that were enqueued before cancellation.

This is a test for #1550
This commit is contained in:
Gwendal Roué committed 2024-06-08 13:47:21 +02:00
1 parent 6689470457
commit 263a549c21
3 files changed
+51

No files matched your search

@@ -865,6 +865,8 @@ extension ValueConcurrentObserver: DatabaseCancellable {
// Notify cancellation
let (events, dbPool): (ValueObservationEvents?, DatabasePool?) = lock.synchronized {
let events = notificationCallbacks?.events
// Set callbacks to nil so that we can't notify anything after
// the cancellation.
notificationCallbacks = nil
return (events, databaseAccess?.dbPool)
}
@@ -448,6 +448,8 @@ extension ValueWriteOnlyObserver: DatabaseCancellable {
// Notify cancellation
let (events, writer) = lock.synchronized {
let events = notificationCallbacks?.events
// Set callbacks to nil so that we can't notify anything after
// the cancellation.
notificationCallbacks = nil
return (events, databaseAccess?.writer)
}
@@ -748,6 +748,53 @@ class ValueObservationTests: GRDBTestCase {
try test(makeDatabasePool())
}
func testIssue1550() throws {
func test(_ writer: some DatabaseWriter) throws {
try writer.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
// Start observing
var counts: [Int] = []
let cancellable = ValueObservation
.trackingConstantRegion { try Table("t").fetchCount($0) }
.start(in: writer) { error in
XCTFail("Unexpected error: \(error)")
} onChange: { count in
counts.append(count)
}
// Perform a write after cancellation, but before the
// observation could schedule the removal of its transaction
// observer from the writer dispatch queue.
let semaphore = DispatchSemaphore(value: 0)
writer.asyncWriteWithoutTransaction { db in
semaphore.wait()
do {
try db.execute(sql: "INSERT INTO t(id) VALUES (NULL)")
} catch {
XCTFail("Unexpected error: \(error)")
}
}
cancellable.cancel()
semaphore.signal()
// Wait until a second write could run, after the observation
// has removed its transaction observer from the writer
// dispatch queue.
let secondWriteExpectation = expectation(description: "cancelled")
writer.asyncWriteWithoutTransaction { _ in
secondWriteExpectation.fulfill()
}
wait(for: [secondWriteExpectation], timeout: 5)
// We should not have been notified of the first write, because
// it was performed after cancellation.
XCTAssertFalse(counts.contains(1))
}
try test(makeDatabaseQueue())
try test(makeDatabasePool())
}
func testIssue1209() throws {
func test(_ dbWriter: some DatabaseWriter) throws {
try dbWriter.write {