Remove ValueObservation.extent

This commit is contained in:
Gwendal Roué committed 2019-03-01 13:17:48 +01:00
1 parent 4935c6f258
commit e966544aeb
16 files changed
+635 -723

No files matched your search

+1 -1
View File
@@ -231,7 +231,7 @@ extension DatabaseWriter {
notificationQueue: observation.notificationQueue,
onError: onError,
onChange: onChange)
db.add(transactionObserver: valueObserver, extent: observation.extent)
db.add(transactionObserver: valueObserver, extent: .observerLifetime)
return valueObserver
}
+8 -2
View File
@@ -119,7 +119,13 @@ extension DatabaseValue {
extension ValueScheduling {
@available(*, unavailable, renamed: "async(onQueue:startImmediately:)")
public static func onQueue(_ queue: DispatchQueue, startImmediately: Bool) -> ValueScheduling {
return .async(onQueue: queue, startImmediately: startImmediately)
public static func onQueue(_ queue: DispatchQueue, startImmediately: Bool) -> ValueScheduling { preconditionFailure() }
}
extension ValueObservation {
@available(*, unavailable, message: "Observation extent is controlled by the lifetime of observers returned by the start() method.")
public var extent: Database.TransactionObservationExtent {
get { preconditionFailure() }
set { preconditionFailure() }
}
}
@@ -7,7 +7,6 @@ extension ValueObservation {
var observation = ValueObservation<R>(
tracking: observedRegion,
reducer: { db in try transform(db, makeReducer(db)) })
observation.extent = extent
observation.scheduling = scheduling
observation.requiresWriteAccess = requiresWriteAccess
return observation
@@ -89,13 +89,6 @@ public struct ValueObservation<Reducer> {
/// observation is less efficient than a read-only observation.
public var requiresWriteAccess: Bool = false
// TODO: replace ValueObservation.extent
/// The extent of the database observation. The default is
/// `.observerLifetime`: the observation lasts until the
/// observer returned by the `start(in:onError:onChange:)` method
/// is deallocated.
public var extent = Database.TransactionObservationExtent.observerLifetime
/// `scheduling` controls how fresh values are notified. Default
/// is `.mainQueue`.
///
+198 -198
View File
@@ -31,43 +31,43 @@ class ValueObservationCombineTests: GRDBTestCase {
struct T2: TableRecord { }
let observation1 = ValueObservation.trackingCount(T1.all())
let observation2 = ValueObservation.trackingCount(T2.all())
var observation = ValueObservation.combine(observation1, observation2)
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { value in
let observation = ValueObservation.combine(observation1, observation2)
let observer = try observation.start(in: dbQueue) { value in
values.append(value)
notificationExpectation.fulfill()
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(values.count, 4)
XCTAssert(values[0] == (0, 0))
XCTAssert(values[1] == (1, 0))
XCTAssert(values[2] == (1, 1))
XCTAssert(values[3] == (2, 2))
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(values.count, 4)
XCTAssert(values[0] == (0, 0))
XCTAssert(values[1] == (1, 0))
XCTAssert(values[2] == (1, 1))
XCTAssert(values[3] == (2, 2))
}
func testCombine3() throws {
@@ -91,54 +91,54 @@ class ValueObservationCombineTests: GRDBTestCase {
let observation1 = ValueObservation.trackingCount(T1.all())
let observation2 = ValueObservation.trackingCount(T2.all())
let observation3 = ValueObservation.trackingCount(T3.all())
var observation = ValueObservation.combine(observation1, observation2, observation3)
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { value in
let observation = ValueObservation.combine(observation1, observation2, observation3)
let observer = try observation.start(in: dbQueue) { value in
values.append(value)
notificationExpectation.fulfill()
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(values.count, 5)
XCTAssert(values[0] == (0, 0, 0))
XCTAssert(values[1] == (1, 0, 0))
XCTAssert(values[2] == (1, 1, 0))
XCTAssert(values[3] == (1, 1, 1))
XCTAssert(values[4] == (2, 2, 2))
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(values.count, 5)
XCTAssert(values[0] == (0, 0, 0))
XCTAssert(values[1] == (1, 0, 0))
XCTAssert(values[2] == (1, 1, 0))
XCTAssert(values[3] == (1, 1, 1))
XCTAssert(values[4] == (2, 2, 2))
}
func testCombine4() throws {
@@ -165,65 +165,65 @@ class ValueObservationCombineTests: GRDBTestCase {
let observation2 = ValueObservation.trackingCount(T2.all())
let observation3 = ValueObservation.trackingCount(T3.all())
let observation4 = ValueObservation.trackingCount(T4.all())
var observation = ValueObservation.combine(observation1, observation2, observation3, observation4)
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { value in
let observation = ValueObservation.combine(observation1, observation2, observation3, observation4)
let observer = try observation.start(in: dbQueue) { value in
values.append(value)
notificationExpectation.fulfill()
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t4")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "DELETE FROM t4")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(values.count, 6)
XCTAssert(values[0] == (0, 0, 0, 0))
XCTAssert(values[1] == (1, 0, 0, 0))
XCTAssert(values[2] == (1, 1, 0, 0))
XCTAssert(values[3] == (1, 1, 1, 0))
XCTAssert(values[4] == (1, 1, 1, 1))
XCTAssert(values[5] == (2, 2, 2, 2))
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t4")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "DELETE FROM t4")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(values.count, 6)
XCTAssert(values[0] == (0, 0, 0, 0))
XCTAssert(values[1] == (1, 0, 0, 0))
XCTAssert(values[2] == (1, 1, 0, 0))
XCTAssert(values[3] == (1, 1, 1, 0))
XCTAssert(values[4] == (1, 1, 1, 1))
XCTAssert(values[5] == (2, 2, 2, 2))
}
func testCombine5() throws {
@@ -253,76 +253,76 @@ class ValueObservationCombineTests: GRDBTestCase {
let observation3 = ValueObservation.trackingCount(T3.all())
let observation4 = ValueObservation.trackingCount(T4.all())
let observation5 = ValueObservation.trackingCount(T5.all())
var observation = ValueObservation.combine(observation1, observation2, observation3, observation4, observation5)
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { value in
let observation = ValueObservation.combine(observation1, observation2, observation3, observation4, observation5)
let observer = try observation.start(in: dbQueue) { value in
values.append(value)
notificationExpectation.fulfill()
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t5 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t4")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t5")
try db.execute(sql: "INSERT INTO t5 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "DELETE FROM t4")
try db.execute(sql: "DELETE FROM t5")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t5 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t5 DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(values.count, 7)
XCTAssert(values[0] == (0, 0, 0, 0, 0))
XCTAssert(values[1] == (1, 0, 0, 0, 0))
XCTAssert(values[2] == (1, 1, 0, 0, 0))
XCTAssert(values[3] == (1, 1, 1, 0, 0))
XCTAssert(values[4] == (1, 1, 1, 1, 0))
XCTAssert(values[5] == (1, 1, 1, 1, 1))
XCTAssert(values[6] == (2, 2, 2, 2, 2))
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t5 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t4")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t5")
try db.execute(sql: "INSERT INTO t5 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "DELETE FROM t1")
try db.execute(sql: "DELETE FROM t2")
try db.execute(sql: "DELETE FROM t3")
try db.execute(sql: "DELETE FROM t4")
try db.execute(sql: "DELETE FROM t5")
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t5 DEFAULT VALUES")
}
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t1 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t2 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t3 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t4 DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t5 DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(values.count, 7)
XCTAssert(values[0] == (0, 0, 0, 0, 0))
XCTAssert(values[1] == (1, 0, 0, 0, 0))
XCTAssert(values[2] == (1, 1, 0, 0, 0))
XCTAssert(values[3] == (1, 1, 1, 0, 0))
XCTAssert(values[4] == (1, 1, 1, 1, 0))
XCTAssert(values[5] == (1, 1, 1, 1, 1))
XCTAssert(values[6] == (2, 2, 2, 2, 2))
}
func testHeterogeneusCombine2() throws {
@@ -33,27 +33,27 @@ class ValueObservationCompactMapTests: GRDBTestCase {
})
// Create an observation
var observation = ValueObservation
let observation = ValueObservation
.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
.compactMap { count -> String? in
if count % 2 == 0 { return nil }
return "\(count)"
}
observation.extent = .databaseLifetime
// Start observation
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["1", "3"])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["1", "3"])
}
try test(makeDatabaseQueue())
@@ -62,12 +62,10 @@ class ValueObservationCompactMapTests: GRDBTestCase {
func testCompactMapPreservesConfiguration() {
var observation = ValueObservation.tracking(DatabaseRegion(), fetch: { _ in })
observation.extent = .nextTransaction
observation.requiresWriteAccess = true
observation.scheduling = .unsafe(startImmediately: true)
let mappedObservation = observation.compactMap { _ in }
XCTAssertEqual(mappedObservation.extent, observation.extent)
XCTAssertEqual(mappedObservation.requiresWriteAccess, observation.requiresWriteAccess)
switch mappedObservation.scheduling {
case .unsafe:
@@ -23,27 +23,27 @@ class ValueObservationCountTests: GRDBTestCase {
notificationExpectation.expectedFulfillmentCount = 5
struct T: TableRecord { }
var observation = ValueObservation.trackingCount(T.all())
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { count in
let observation = ValueObservation.trackingCount(T.all())
let observer = try observation.start(in: dbQueue) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES") // +1
try db.execute(sql: "UPDATE t SET id = id") // =
try db.execute(sql: "INSERT INTO t DEFAULT VALUES") // +1
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "DELETE FROM t WHERE id = 1")
return .commit
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES") // +1
try db.execute(sql: "UPDATE t SET id = id") // =
try db.execute(sql: "INSERT INTO t DEFAULT VALUES") // +1
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "DELETE FROM t WHERE id = 1")
return .commit
}
try db.execute(sql: "DELETE FROM t WHERE id = 2") // -1
}
try db.execute(sql: "DELETE FROM t WHERE id = 2") // -1
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1, 2, 3, 2])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1, 2, 3, 2])
}
}
@@ -37,31 +37,31 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 4
var observation = ValueObservation.trackingAll(SQLRequest<Name>(sql: "SELECT name FROM t ORDER BY id"))
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { names in
let observation = ValueObservation.trackingAll(SQLRequest<Name>(sql: "SELECT name FROM t ORDER BY id"))
let observer = try observation.start(in: dbQueue) { names in
results.append(names)
notificationExpectation.fulfill()
}
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [
[],
["foo"],
["foo", "bar"],
["bar"]])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [
[],
["foo"],
["foo", "bar"],
["bar"]])
}
func testOne() throws {
@@ -73,39 +73,39 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 7
var observation = ValueObservation.trackingOne(SQLRequest<Name>(sql: "SELECT name FROM t ORDER BY id DESC"))
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { name in
let observation = ValueObservation.trackingOne(SQLRequest<Name>(sql: "SELECT name FROM t ORDER BY id DESC"))
let observer = try observation.start(in: dbQueue) { name in
results.append(name)
notificationExpectation.fulfill()
}
try dbQueue.inDatabase { 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")
try db.inTransaction {
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { 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")
try db.inTransaction {
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'baz')")
try db.execute(sql: "UPDATE t SET name = NULL")
try db.execute(sql: "DELETE FROM t")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, NULL)")
try db.execute(sql: "UPDATE t SET name = 'qux'")
}
try db.execute(sql: "DELETE FROM t")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'baz')")
try db.execute(sql: "UPDATE t SET name = NULL")
try db.execute(sql: "DELETE FROM t")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, NULL)")
try db.execute(sql: "UPDATE t SET name = 'qux'")
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [
nil,
"foo",
"bar",
nil,
"baz",
nil,
"qux"])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [
nil,
"foo",
"bar",
nil,
"baz",
nil,
"qux"])
}
func testAllOptional() throws {
@@ -117,31 +117,31 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 4
var observation = ValueObservation.trackingAll(SQLRequest<Name?>(sql: "SELECT name FROM t ORDER BY id"))
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { names in
let observation = ValueObservation.trackingAll(SQLRequest<Name?>(sql: "SELECT name FROM t ORDER BY id"))
let observer = try observation.start(in: dbQueue) { names in
results.append(names)
notificationExpectation.fulfill()
}
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, NULL)")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, NULL)")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0?.rawValue }}, [
[],
["foo"],
["foo", nil],
[nil]])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0?.rawValue }}, [
[],
["foo"],
["foo", nil],
[nil]])
}
func testViewOptimization() throws {
@@ -168,33 +168,33 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase {
// Test that view v is not included in the observed region.
// This optimization helps observation of views that feed from a
// single table.
var observation = ValueObservation.trackingAll(request)
observation.extent = .databaseLifetime
let transactionObserver = try observation.start(in: dbQueue) { names in
let observation = ValueObservation.trackingAll(request)
let observer = try observation.start(in: dbQueue) { names in
results.append(names)
notificationExpectation.fulfill()
}
let valueObserver = transactionObserver as! ValueObserver<DatabaseValuesReducer<SQLRequest<Name>>>
let valueObserver = observer as! ValueObserver<DatabaseValuesReducer<SQLRequest<Name>>>
XCTAssertEqual(valueObserver.region.description, "t(id,name)") // view is not tracked
// Test view observation
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
try withExtendedLifetime(observer) {
// Test view observation
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [
[],
["foo"],
["foo", "bar"],
["bar"]])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [
[],
["foo"],
["foo", "bar"],
["bar"]])
}
}
@@ -13,48 +13,6 @@ import XCTest
#endif
class ValueObservationExtentTests: GRDBTestCase {
func testDefaultExtentIsObserverLifetime() {
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { _ in })
XCTAssertEqual(observation.extent, .observerLifetime)
}
func testExtentDatabaseLifetime() throws {
// We need something to change
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
// Track reducer process
let notificationExpectation = expectation(description: "notification")
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 3
// A reducer
let reducer = AnyValueReducer(
fetch: { _ in },
value: { $0 })
// Create an observation
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .databaseLifetime
// Start observation
_ = try observation.start(in: dbQueue) {
notificationExpectation.fulfill()
}
// notified
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
// notified
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
}
func testExtentObserverLifetime() throws {
// We need something to change
let dbQueue = try makeDatabaseQueue()
@@ -72,8 +30,7 @@ class ValueObservationExtentTests: GRDBTestCase {
value: { $0 })
// Create an observation
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .observerLifetime
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
// Start observation and deallocate observer after second change
var observer: TransactionObserver?
@@ -101,43 +58,4 @@ class ValueObservationExtentTests: GRDBTestCase {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(changesCount, 2)
}
func testExtentNextTransaction() throws {
// We need something to change
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
// Track reducer process
let notificationExpectation = expectation(description: "notification")
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 2
// A reducer
let reducer = AnyValueReducer(
fetch: { _ in },
value: { $0 })
// Create an observation
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .nextTransaction
// Start observation
let observer = try observation.start(in: dbQueue) {
notificationExpectation.fulfill()
}
try withExtendedLifetime(observer) {
// notified
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
// not notified
try dbQueue.write { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
}
}
}
@@ -31,23 +31,23 @@ class ValueObservationFetchTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 4
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
})
observation.extent = .databaseLifetime
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "UPDATE t SET id = id")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "UPDATE t SET id = id")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1, 1, 2])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1, 1, 2])
}
try test(makeDatabaseQueue())
@@ -63,23 +63,23 @@ class ValueObservationFetchTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 3
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
}).distinctUntilChanged()
observation.extent = .databaseLifetime
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "UPDATE t SET id = id")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "UPDATE t SET id = id")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1, 2])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1, 2])
}
try test(makeDatabaseQueue())
+10 -12
View File
@@ -33,24 +33,24 @@ class ValueObservationMapTests: GRDBTestCase {
})
// Create an observation
var observation = ValueObservation
let observation = ValueObservation
.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
.map { count -> String in return "\(count)" }
observation.extent = .databaseLifetime
// Start observation
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["1", "2", "3"])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["1", "2", "3"])
}
try test(makeDatabaseQueue())
@@ -59,12 +59,10 @@ class ValueObservationMapTests: GRDBTestCase {
func testMapPreservesConfiguration() {
var observation = ValueObservation.tracking(DatabaseRegion(), fetch: { _ in })
observation.extent = .nextTransaction
observation.requiresWriteAccess = true
observation.scheduling = .unsafe(startImmediately: true)
let mappedObservation = observation.map { _ in }
XCTAssertEqual(mappedObservation.extent, observation.extent)
XCTAssertEqual(mappedObservation.requiresWriteAccess, observation.requiresWriteAccess)
switch mappedObservation.scheduling {
case .unsafe:
@@ -23,21 +23,21 @@ class ValueObservationReadonlyTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 2
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
})
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { count in
let observer = try observation.start(in: dbQueue) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbQueue.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbQueue.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
func testWriteObservationFailsByDefault() throws {
@@ -79,19 +79,19 @@ class ValueObservationReadonlyTests: GRDBTestCase {
try db.execute(sql: "DROP TABLE temp")
return result
})
observation.extent = .databaseLifetime
observation.requiresWriteAccess = true
_ = try observation.start(in: dbQueue) { count in
let observer = try observation.start(in: dbQueue) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbQueue.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbQueue.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
func testWriteObservationIsWrappedInSavepoint() throws {
@@ -36,31 +36,31 @@ class ValueObservationRecordTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 4
var observation = ValueObservation.trackingAll(SQLRequest<Player>(sql: "SELECT * FROM t ORDER BY id"))
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { players in
let observation = ValueObservation.trackingAll(SQLRequest<Player>(sql: "SELECT * FROM t ORDER BY id"))
let observer = try observation.start(in: dbQueue) { players in
results.append(players)
notificationExpectation.fulfill()
}
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.row }}, [
[],
[["id":1, "name":"foo"]],
[["id":1, "name":"foo"], ["id":2, "name":"bar"]],
[["id":2, "name":"bar"]]])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.row }}, [
[],
[["id":1, "name":"foo"]],
[["id":1, "name":"foo"], ["id":2, "name":"bar"]],
[["id":2, "name":"bar"]]])
}
func testOne() throws {
@@ -72,30 +72,30 @@ class ValueObservationRecordTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 4
var observation = ValueObservation.trackingOne(SQLRequest<Player>(sql: "SELECT * FROM t ORDER BY id DESC"))
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { player in
let observation = ValueObservation.trackingOne(SQLRequest<Player>(sql: "SELECT * FROM t ORDER BY id DESC"))
let observer = try observation.start(in: dbQueue) { player in
results.append(player)
notificationExpectation.fulfill()
}
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t") // -1
}
try db.execute(sql: "DELETE FROM t") // -1
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.row }}, [
nil,
["id":1, "name":"foo"],
["id":2, "name":"bar"],
nil])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results.map { $0.map { $0.row }}, [
nil,
["id":1, "name":"foo"],
["id":2, "name":"bar"],
nil])
}
}
+107 -107
View File
@@ -58,11 +58,10 @@ class ValueObservationReducerTests: GRDBTestCase {
// Create an observation
let request = SQLRequest<Void>(sql: "SELECT * FROM t")
var observation = ValueObservation.tracking(request, reducer: { _ in reducer })
observation.extent = .databaseLifetime
let observation = ValueObservation.tracking(request, reducer: { _ in reducer })
// Start observation
_ = try observation.start(
let observer = try observation.start(
in: dbWriter,
onError: {
errors.append($0)
@@ -72,53 +71,53 @@ class ValueObservationReducerTests: GRDBTestCase {
changes.append($0)
notificationExpectation.fulfill()
})
// Test that default config synchronously notifies initial value
XCTAssertEqual(fetchCount, 1)
XCTAssertEqual(reduceCount, 1)
XCTAssertEqual(errors.count, 0)
XCTAssertEqual(changes, ["0"])
try dbWriter.writeWithoutTransaction { db in
// Test a 1st notified transaction
try db.inTransaction {
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
return .commit
try withExtendedLifetime(observer) {
// Test that default config synchronously notifies initial value
XCTAssertEqual(fetchCount, 1)
XCTAssertEqual(reduceCount, 1)
XCTAssertEqual(errors.count, 0)
XCTAssertEqual(changes, ["0"])
try dbWriter.writeWithoutTransaction { db in
// Test a 1st notified transaction
try db.inTransaction {
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
return .commit
}
// Test an untracked transaction
try db.inTransaction {
try db.execute(sql: "CREATE TABLE ignored(a)")
return .commit
}
// Test a dropped transaction
try db.inTransaction {
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
return .commit
}
// Test a rollbacked transaction
try db.inTransaction {
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
return .rollback
}
// Test a 2nd notified transaction
try db.inTransaction {
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
return .commit
}
}
// Test an untracked transaction
try db.inTransaction {
try db.execute(sql: "CREATE TABLE ignored(a)")
return .commit
}
// Test a dropped transaction
try db.inTransaction {
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
return .commit
}
// Test a rollbacked transaction
try db.inTransaction {
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
return .rollback
}
// Test a 2nd notified transaction
try db.inTransaction {
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
return .commit
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(fetchCount, 4)
XCTAssertEqual(reduceCount, 4)
XCTAssertEqual(errors.count, 0)
XCTAssertEqual(changes, ["0", "1", "5"])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(fetchCount, 4)
XCTAssertEqual(reduceCount, 4)
XCTAssertEqual(errors.count, 0)
XCTAssertEqual(changes, ["0", "1", "5"])
}
try test(makeDatabaseQueue())
@@ -174,11 +173,10 @@ class ValueObservationReducerTests: GRDBTestCase {
value: { $0 })
// Create an observation
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .databaseLifetime
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
// Start observation
_ = try observation.start(
let observer = try observation.start(
in: dbWriter,
onError: {
errors.append($0)
@@ -189,16 +187,18 @@ class ValueObservationReducerTests: GRDBTestCase {
notificationExpectation.fulfill()
})
struct TestError: Error { }
nextError = TestError()
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
struct TestError: Error { }
nextError = TestError()
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(errors.count, 1)
XCTAssertEqual(changes.count, 2)
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(errors.count, 1)
XCTAssertEqual(changes.count, 2)
}
try test(makeDatabaseQueue())
@@ -225,21 +225,21 @@ class ValueObservationReducerTests: GRDBTestCase {
value: { _ -> Int? in
defer { count += 1 }
return count })
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer})
observation.extent = .databaseLifetime
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer})
// Start observation
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.write { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.write { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
try test(makeDatabaseQueue())
@@ -272,22 +272,22 @@ class ValueObservationReducerTests: GRDBTestCase {
}
// Create an observation
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .databaseLifetime
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
// Start observation
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["1", "2", "3"])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["1", "2", "3"])
}
try test(makeDatabaseQueue())
@@ -321,22 +321,22 @@ class ValueObservationReducerTests: GRDBTestCase {
}
// Create an observation
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .databaseLifetime
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
// Start observation
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["1", "3"])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["1", "3"])
}
try test(makeDatabaseQueue())
@@ -353,22 +353,22 @@ class ValueObservationReducerTests: GRDBTestCase {
notificationExpectation.expectedFulfillmentCount = 3
struct T: TableRecord { }
var observation = ValueObservation
let observation = ValueObservation
.trackingCount(T.all())
.map { "\($0)" }
observation.extent = .databaseLifetime
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.writeWithoutTransaction { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["0", "1", "2"])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, ["0", "1", "2"])
}
try test(makeDatabaseQueue())
@@ -392,16 +392,16 @@ class ValueObservationReducerTests: GRDBTestCase {
}
reduceExpectation.fulfill()
})
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .databaseLifetime
_ = try observation.start(in: dbWriter, onChange: { _ in })
try dbWriter.write { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
let observer = try observation.start(in: dbWriter, onChange: { _ in })
try withExtendedLifetime(observer) {
try dbWriter.write { db in
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(labels, expectedLabels)
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(labels, expectedLabels)
}
do {
// dbQueue with default label
+33 -33
View File
@@ -22,31 +22,31 @@ class ValueObservationRowTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 4
var observation = ValueObservation.trackingAll(SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id"))
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { rows in
let observation = ValueObservation.trackingAll(SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id"))
let observer = try observation.start(in: dbQueue) { rows in
results.append(rows)
notificationExpectation.fulfill()
}
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
}
try db.execute(sql: "DELETE FROM t WHERE id = 1") // -1
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results, [
[],
[["id":1, "name":"foo"]],
[["id":1, "name":"foo"], ["id":2, "name":"bar"]],
[["id":2, "name":"bar"]]])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(results, [
[],
[["id":1, "name":"foo"]],
[["id":1, "name":"foo"], ["id":2, "name":"bar"]],
[["id":2, "name":"bar"]]])
}
func testOne() throws {
@@ -58,23 +58,23 @@ class ValueObservationRowTests: GRDBTestCase {
notificationExpectation.assertForOverFulfill = true
notificationExpectation.expectedFulfillmentCount = 4
var observation = ValueObservation.trackingOne(SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id DESC"))
observation.extent = .databaseLifetime
_ = try observation.start(in: dbQueue) { row in
let observation = ValueObservation.trackingOne(SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id DESC"))
let observer = try observation.start(in: dbQueue) { row in
results.append(row)
notificationExpectation.fulfill()
}
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
try withExtendedLifetime(observer) {
try dbQueue.inDatabase { db in
try db.execute(sql: "INSERT INTO t (id, name) VALUES (1, 'foo')") // +1
try db.execute(sql: "UPDATE t SET name = 'foo' WHERE id = 1") // =
try db.inTransaction { // +1
try db.execute(sql: "INSERT INTO t (id, name) VALUES (2, 'bar')")
try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'baz')")
try db.execute(sql: "DELETE FROM t WHERE id = 3")
return .commit
}
try db.execute(sql: "DELETE FROM t") // -1
}
try db.execute(sql: "DELETE FROM t") // -1
}
waitForExpectations(timeout: 1, handler: nil)
@@ -26,12 +26,10 @@ class ValueObservationSchedulingTests: GRDBTestCase {
let key = DispatchSpecificKey<()>()
DispatchQueue.main.setSpecific(key: key, value: ())
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
})
observation.extent = .databaseLifetime
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
XCTAssertNotNil(DispatchQueue.getSpecific(key: key))
counts.append(count)
notificationExpectation.fulfill()
@@ -40,12 +38,14 @@ class ValueObservationSchedulingTests: GRDBTestCase {
// dispatched when observation is started from the main queue
XCTAssertEqual(counts, [0])
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
try test(makeDatabaseQueue())
@@ -66,25 +66,25 @@ class ValueObservationSchedulingTests: GRDBTestCase {
let key = DispatchSpecificKey<()>()
DispatchQueue.main.setSpecific(key: key, value: ())
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
})
observation.extent = .databaseLifetime
var observer: TransactionObserver?
DispatchQueue.global(qos: .default).async {
_ = try! observation.start(in: dbWriter) { count in
observer = try! observation.start(in: dbWriter) { count in
XCTAssertNotNil(DispatchQueue.getSpecific(key: key))
counts.append(count)
notificationExpectation.fulfill()
}
try! dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
withExtendedLifetime(observer) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
}
try test(makeDatabaseQueue())
@@ -113,10 +113,9 @@ class ValueObservationSchedulingTests: GRDBTestCase {
}
},
value: { $0 })
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .databaseLifetime
let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
_ = try observation.start(
let observer = try observation.start(
in: dbWriter,
onError: { error in
XCTAssertNotNil(DispatchQueue.getSpecific(key: key))
@@ -127,15 +126,16 @@ class ValueObservationSchedulingTests: GRDBTestCase {
XCTAssertNotNil(DispatchQueue.getSpecific(key: key))
notificationExpectation.fulfill()
})
struct TestError: Error { }
nextError = TestError()
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
struct TestError: Error { }
nextError = TestError()
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(errorCount, 1)
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(errorCount, 1)
}
try test(makeDatabaseQueue())
@@ -159,21 +159,21 @@ class ValueObservationSchedulingTests: GRDBTestCase {
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
})
observation.extent = .databaseLifetime
observation.scheduling = .async(onQueue: queue, startImmediately: true)
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
XCTAssertNotNil(DispatchQueue.getSpecific(key: key))
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
try test(makeDatabaseQueue())
@@ -197,21 +197,21 @@ class ValueObservationSchedulingTests: GRDBTestCase {
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
})
observation.extent = .databaseLifetime
observation.scheduling = .async(onQueue: queue, startImmediately: false)
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
XCTAssertNotNil(DispatchQueue.getSpecific(key: key))
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [1])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [1])
}
try test(makeDatabaseQueue())
@@ -237,10 +237,9 @@ class ValueObservationSchedulingTests: GRDBTestCase {
fetch: { _ in throw TestError() },
value: { $0 })
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .databaseLifetime
observation.scheduling = .async(onQueue: queue, startImmediately: false)
_ = try observation.start(
let observer = try observation.start(
in: dbWriter,
onError: { error in
XCTAssertNotNil(DispatchQueue.getSpecific(key: key))
@@ -248,13 +247,14 @@ class ValueObservationSchedulingTests: GRDBTestCase {
notificationExpectation.fulfill()
},
onChange: { _ in fatalError() })
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(errorCount, 1)
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(errorCount, 1)
}
try test(makeDatabaseQueue())
@@ -277,10 +277,9 @@ class ValueObservationSchedulingTests: GRDBTestCase {
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
})
observation.extent = .databaseLifetime
observation.scheduling = .unsafe(startImmediately: true)
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
if counts.isEmpty {
// require main queue on first element
XCTAssertNotNil(DispatchQueue.getSpecific(key: key))
@@ -288,13 +287,14 @@ class ValueObservationSchedulingTests: GRDBTestCase {
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
try test(makeDatabaseQueue())
@@ -318,11 +318,10 @@ class ValueObservationSchedulingTests: GRDBTestCase {
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
})
observation.extent = .databaseLifetime
observation.scheduling = .unsafe(startImmediately: true)
var observer: TransactionObserver?
queue.async {
_ = try! observation.start(in: dbWriter) { count in
observer = try! observation.start(in: dbWriter) { count in
if counts.isEmpty {
// require custom queue on first notification
XCTAssertNotNil(DispatchQueue.getSpecific(key: key))
@@ -330,14 +329,15 @@ class ValueObservationSchedulingTests: GRDBTestCase {
counts.append(count)
notificationExpectation.fulfill()
}
try! dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
withExtendedLifetime(observer) {
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [0, 1])
}
}
try test(makeDatabaseQueue())
@@ -357,20 +357,20 @@ class ValueObservationSchedulingTests: GRDBTestCase {
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: {
try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")!
})
observation.extent = .databaseLifetime
observation.scheduling = .unsafe(startImmediately: false)
_ = try observation.start(in: dbWriter) { count in
let observer = try observation.start(in: dbWriter) { count in
counts.append(count)
notificationExpectation.fulfill()
}
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [1])
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(counts, [1])
}
try test(makeDatabaseQueue())
@@ -392,23 +392,23 @@ class ValueObservationSchedulingTests: GRDBTestCase {
fetch: { _ in throw TestError() },
value: { $0 })
var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, reducer: { _ in reducer })
observation.extent = .databaseLifetime
observation.scheduling = .unsafe(startImmediately: false)
_ = try observation.start(
let observer = try observation.start(
in: dbWriter,
onError: { error in
errorCount += 1
notificationExpectation.fulfill()
},
onChange: { _ in fatalError() })
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
try withExtendedLifetime(observer) {
try dbWriter.write {
try $0.execute(sql: "INSERT INTO t DEFAULT VALUES")
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(errorCount, 1)
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(errorCount, 1)
}
try test(makeDatabaseQueue())