diff --git a/Tests/GRDBTests/ValueObservationCountTests.swift b/Tests/GRDBTests/ValueObservationCountTests.swift index 9b85be278..810403f1e 100644 --- a/Tests/GRDBTests/ValueObservationCountTests.swift +++ b/Tests/GRDBTests/ValueObservationCountTests.swift @@ -31,7 +31,11 @@ class ValueObservationCountTests: GRDBTestCase { } let expectedValues = [0, 1, 2, 3, 2] - let values = try wait(for: recorder.prefix(expectedValues.count + 1).inverted, timeout: 0.5) + let values = try wait( + for: recorder + .prefix(expectedValues.count + 1 /* deduplication: don't expect more than expectedValues */) + .inverted, + timeout: 0.5) try assertValueObservationRecordingMatch( recorded: values, expected: expectedValues, diff --git a/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift b/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift index 9d457d286..4d2fa844a 100644 --- a/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift +++ b/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift @@ -1,15 +1,15 @@ import XCTest #if GRDBCUSTOMSQLITE - @testable import GRDBCustomSQLite +@testable import GRDBCustomSQLite #else - #if GRDBCIPHER - import SQLCipher - #elseif SWIFT_PACKAGE - import CSQLite - #else - import SQLite3 - #endif - @testable import GRDB +#if GRDBCIPHER +import SQLCipher +#elseif SWIFT_PACKAGE +import CSQLite +#else +import SQLite3 +#endif +@testable import GRDB #endif private struct Name: DatabaseValueConvertible, Equatable { @@ -49,7 +49,11 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase { ["foo"], ["foo", "bar"], ["bar"]] - let values = try wait(for: recorder.prefix(expectedValues.count + 1).inverted, timeout: 0.5) + let values = try wait( + for: recorder + .prefix(expectedValues.count + 1 /* deduplication: don't expect more than expectedValues */) + .inverted, + timeout: 0.5) try assertValueObservationRecordingMatch( recorded: values.map { $0.map(\.rawValue) }, expected: expectedValues, @@ -103,7 +107,11 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase { "baz", nil, "qux"] - let values = try wait(for: recorder.prefix(expectedValues.count + 1).inverted, timeout: 0.5) + let values = try wait( + for: recorder + .prefix(expectedValues.count + 1 /* deduplication: don't expect more than expectedValues */) + .inverted, + timeout: 0.5) try assertValueObservationRecordingMatch( recorded: values.map { $0.map(\.rawValue) }, expected: expectedValues, @@ -149,7 +157,11 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase { ["foo"], ["foo", nil], [nil]] - let values = try wait(for: recorder.prefix(expectedValues.count + 1).inverted, timeout: 0.5) + let values = try wait( + for: recorder + .prefix(expectedValues.count + 1 /* deduplication: don't expect more than expectedValues */) + .inverted, + timeout: 0.5) try assertValueObservationRecordingMatch( recorded: values.map { $0.map { $0?.rawValue }}, expected: expectedValues, @@ -203,7 +215,11 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase { "baz", nil, "qux"] - let values = try wait(for: recorder.prefix(expectedValues.count + 1).inverted, timeout: 0.5) + let values = try wait( + for: recorder + .prefix(expectedValues.count + 1 /* deduplication: don't expect more than expectedValues */) + .inverted, + timeout: 0.5) try assertValueObservationRecordingMatch( recorded: values.map { $0.map(\.rawValue)}, expected: expectedValues, diff --git a/Tests/GRDBTests/ValueObservationFetchTests.swift b/Tests/GRDBTests/ValueObservationFetchTests.swift index 4deb31679..a8fe8ccf7 100644 --- a/Tests/GRDBTests/ValueObservationFetchTests.swift +++ b/Tests/GRDBTests/ValueObservationFetchTests.swift @@ -21,72 +21,86 @@ class ValueObservationFetchTests: GRDBTestCase { } func testFetch() throws { - func test(_ dbWriter: DatabaseWriter) throws { - try dbWriter.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + func test(writer: DatabaseWriter, observation: ValueObservation>) throws { + try writer.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } - var counts: [Int] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 4 + let recorder = observation.record(in: writer) - let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { - try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")! - }) - let observer = observation.start( - in: dbWriter, - onError: { error in XCTFail("Unexpected error: \(error)") }, - onChange: { count in - counts.append(count) - notificationExpectation.fulfill() - }) - 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]) + try writer.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") } + + let expectedValues = [0, 1, 1, 2] + let values = try wait( + for: recorder + .prefix(expectedValues.count + 2 /* async pool may perform double initial fetch */) + .inverted, + timeout: 0.5) + try assertValueObservationRecordingMatch( + recorded: values, + expected: expectedValues, + "\(type(of: writer)), \(observation.scheduling)") } - try test(makeDatabaseQueue()) - try test(makeDatabasePool()) + let schedulings: [ValueObservationScheduling] = [ + .mainQueue, + .async(onQueue: .main), + .unsafe + ] + + for scheduling in schedulings { + var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { + try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")! + }) + observation.scheduling = scheduling + + try test(writer: DatabaseQueue(), observation: observation) + try test(writer: makeDatabaseQueue(), observation: observation) + try test(writer: makeDatabasePool(), observation: observation) + } } func testRemoveDuplicated() throws { - func test(_ dbWriter: DatabaseWriter) throws { - try dbWriter.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + func test(writer: DatabaseWriter, observation: ValueObservation>>) throws { + try writer.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } - var counts: [Int] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 3 + let recorder = observation.record(in: writer) - let observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { - try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")! - }).removeDuplicates() - let observer = observation.start( - in: dbWriter, - onError: { error in XCTFail("Unexpected error: \(error)") }, - onChange: { count in - counts.append(count) - notificationExpectation.fulfill() - }) - 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]) + try writer.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") } + + let expectedValues = [0, 1, 2] + let values = try wait( + for: recorder + .prefix(expectedValues.count + 1 /* deduplication: don't expect more than expectedValues */) + .inverted, + timeout: 0.5) + try assertValueObservationRecordingMatch( + recorded: values, + expected: expectedValues, + "\(type(of: writer)), \(observation.scheduling)") } - try test(makeDatabaseQueue()) - try test(makeDatabasePool()) + let schedulings: [ValueObservationScheduling] = [ + .mainQueue, + .async(onQueue: .main), + .unsafe + ] + + for scheduling in schedulings { + var observation = ValueObservation.tracking(DatabaseRegion.fullDatabase, fetch: { + try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")! + }).removeDuplicates() + observation.scheduling = scheduling + + try test(writer: DatabaseQueue(), observation: observation) + try test(writer: makeDatabaseQueue(), observation: observation) + try test(writer: makeDatabasePool(), observation: observation) + } } }