From 087989fc8900e796c6c2d2df4dfb510e8ce0e91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 28 Oct 2018 15:57:27 +0100 Subject: [PATCH] More ValueObseervation tests: extent --- .../ValueObservationExtentTests.swift | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/Tests/GRDBTests/ValueObservationExtentTests.swift b/Tests/GRDBTests/ValueObservationExtentTests.swift index 776231334..5b9028aef 100644 --- a/Tests/GRDBTests/ValueObservationExtentTests.swift +++ b/Tests/GRDBTests/ValueObservationExtentTests.swift @@ -13,4 +13,134 @@ import XCTest #endif class ValueObservationExtentTests: GRDBTestCase { + func testExtentDatabaseLifetime() throws { + // We need something to change + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + // Track reducer proceess + let notificationExpectation = expectation(description: "notification") + notificationExpectation.assertForOverFulfill = true + notificationExpectation.expectedFulfillmentCount = 3 + + // A reducer + let reducer = AnyValueReducer( + fetch: { _ in }, + value: { $0 }) + + // Create an observation + let request = SQLRequest("SELECT * FROM t") + var observation = ValueObservation.observing(request, reducer: reducer) + observation.extent = .databaseLifetime + + // Start observation + _ = try dbQueue.add( + observation: observation, + onError: nil, + onChange: { notificationExpectation.fulfill() }) + + // notified + try dbQueue.write { db in + try db.execute("INSERT INTO t DEFAULT VALUES") + } + + // notified + try dbQueue.write { db in + try db.execute("INSERT INTO t DEFAULT VALUES") + } + + waitForExpectations(timeout: 1, handler: nil) + } + + func testExtentObserverLifetime() throws { + // We need something to change + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + // Track reducer proceess + var changesCount = 0 + let notificationExpectation = expectation(description: "notification") + notificationExpectation.assertForOverFulfill = true + notificationExpectation.expectedFulfillmentCount = 2 + + // A reducer + let reducer = AnyValueReducer( + fetch: { _ in }, + value: { $0 }) + + // Create an observation + let request = SQLRequest("SELECT * FROM t") + var observation = ValueObservation.observing(request, reducer: reducer) + observation.extent = .observerLifetime + + // Start observation and deallocate observer after second change + var observer: TransactionObserver? + observer = try dbQueue.add( + observation: observation, + onError: nil, + onChange: { + changesCount += 1 + if changesCount == 2 { + observer = nil + } + notificationExpectation.fulfill() + }) + + // notified + try dbQueue.write { db in + try db.execute("INSERT INTO t DEFAULT VALUES") + } + + // not notified + try dbQueue.write { db in + try db.execute("INSERT INTO t DEFAULT VALUES") + } + + // Avoid "Variable 'observer' was written to, but never read" warning + _ = observer + + 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("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + // Track reducer proceess + let notificationExpectation = expectation(description: "notification") + notificationExpectation.assertForOverFulfill = true + notificationExpectation.expectedFulfillmentCount = 2 + + // A reducer + let reducer = AnyValueReducer( + fetch: { _ in }, + value: { $0 }) + + // Create an observation + let request = SQLRequest("SELECT * FROM t") + var observation = ValueObservation.observing(request, reducer: reducer) + observation.extent = .nextTransaction + + // Start observation + let observer = try dbQueue.add( + observation: observation, + onError: nil, + onChange: { notificationExpectation.fulfill() }) + + try withExtendedLifetime(observer) { + // notified + try dbQueue.write { db in + try db.execute("INSERT INTO t DEFAULT VALUES") + } + + // not notified + try dbQueue.write { db in + try db.execute("INSERT INTO t DEFAULT VALUES") + } + + waitForExpectations(timeout: 1, handler: nil) + } + } }