From 31e6ddeac30b5a90045c8efbd5e5f8f89f24749d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 28 Oct 2018 17:32:31 +0100 Subject: [PATCH] ValueObservation: test scheduling --- .../ValueObservationExtentTests.swift | 5 + .../ValueObservationFetchTests.swift | 4 +- .../ValueObservationReadonlyTests.swift | 4 +- .../ValueObservationSchedulingTests.swift | 223 ++++++++++++++++++ 4 files changed, 232 insertions(+), 4 deletions(-) diff --git a/Tests/GRDBTests/ValueObservationExtentTests.swift b/Tests/GRDBTests/ValueObservationExtentTests.swift index ec14a8750..7b8e5fdf8 100644 --- a/Tests/GRDBTests/ValueObservationExtentTests.swift +++ b/Tests/GRDBTests/ValueObservationExtentTests.swift @@ -13,6 +13,11 @@ import XCTest #endif class ValueObservationExtentTests: GRDBTestCase { + func testDefaultExtentIsObserverLifetime() { + let observation = ValueObservation.observing(DatabaseRegion.fullDatabase, fetch: { _ in }) + XCTAssertEqual(observation.extent, .observerLifetime) + } + func testExtentDatabaseLifetime() throws { // We need something to change let dbQueue = try makeDatabaseQueue() diff --git a/Tests/GRDBTests/ValueObservationFetchTests.swift b/Tests/GRDBTests/ValueObservationFetchTests.swift index a85129f1c..855981cdb 100644 --- a/Tests/GRDBTests/ValueObservationFetchTests.swift +++ b/Tests/GRDBTests/ValueObservationFetchTests.swift @@ -27,8 +27,8 @@ class ValueObservationFetchTests: GRDBTestCase { }) observation.extent = .databaseLifetime _ = try dbQueue.add(observation: observation) { count in - notificationExpectation.fulfill() counts.append(count) + notificationExpectation.fulfill() } try dbQueue.write { @@ -59,8 +59,8 @@ class ValueObservationFetchTests: GRDBTestCase { }) observation.extent = .databaseLifetime _ = try dbQueue.add(observation: observation) { count in - notificationExpectation.fulfill() counts.append(count) + notificationExpectation.fulfill() } try dbQueue.write { diff --git a/Tests/GRDBTests/ValueObservationReadonlyTests.swift b/Tests/GRDBTests/ValueObservationReadonlyTests.swift index 384644843..f94f24b1a 100644 --- a/Tests/GRDBTests/ValueObservationReadonlyTests.swift +++ b/Tests/GRDBTests/ValueObservationReadonlyTests.swift @@ -28,8 +28,8 @@ class ValueObservationReadonlyTests: GRDBTestCase { }) observation.extent = .databaseLifetime _ = try dbQueue.add(observation: observation) { count in - notificationExpectation.fulfill() counts.append(count) + notificationExpectation.fulfill() } try dbQueue.write { @@ -81,8 +81,8 @@ class ValueObservationReadonlyTests: GRDBTestCase { observation.extent = .databaseLifetime observation.isReadOnly = false _ = try dbQueue.add(observation: observation) { count in - notificationExpectation.fulfill() counts.append(count) + notificationExpectation.fulfill() } try dbQueue.write { diff --git a/Tests/GRDBTests/ValueObservationSchedulingTests.swift b/Tests/GRDBTests/ValueObservationSchedulingTests.swift index a90fb3808..b45434c9a 100644 --- a/Tests/GRDBTests/ValueObservationSchedulingTests.swift +++ b/Tests/GRDBTests/ValueObservationSchedulingTests.swift @@ -13,4 +13,227 @@ import XCTest #endif class ValueObservationSchedulingTests: GRDBTestCase { + func testMainQueueObservationStartedFromMainQueue() throws { + // We need something to change + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + var counts: [Int] = [] + let notificationExpectation = expectation(description: "notification") + notificationExpectation.assertForOverFulfill = true + notificationExpectation.expectedFulfillmentCount = 2 + + let key = DispatchSpecificKey<()>() + DispatchQueue.main.setSpecific(key: key, value: ()) + + var observation = ValueObservation.observing(DatabaseRegion.fullDatabase, fetch: { + try Int.fetchOne($0, "SELECT COUNT(*) FROM t")! + }) + observation.extent = .databaseLifetime + + _ = try dbQueue.add(observation: observation) { count in + XCTAssertNotNil(DispatchQueue.getSpecific(key: key)) + counts.append(count) + notificationExpectation.fulfill() + } + // .mainQueue scheduling: initial value MUST be synchronously + // dispatched when observation is started from the main queue + XCTAssertEqual(counts, [0]) + + try dbQueue.write { + try $0.execute("INSERT INTO t DEFAULT VALUES") + } + + waitForExpectations(timeout: 1, handler: nil) + XCTAssertEqual(counts, [0, 1]) + } + + @available(OSX 10.10, *) // DispatchQueue qos + func testMainQueueObservationStartedFromAnotherQueue() throws { + // We need something to change + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + var counts: [Int] = [] + let notificationExpectation = expectation(description: "notification") + notificationExpectation.assertForOverFulfill = true + notificationExpectation.expectedFulfillmentCount = 2 + + let key = DispatchSpecificKey<()>() + DispatchQueue.main.setSpecific(key: key, value: ()) + + var observation = ValueObservation.observing(DatabaseRegion.fullDatabase, fetch: { + try Int.fetchOne($0, "SELECT COUNT(*) FROM t")! + }) + observation.extent = .databaseLifetime + + DispatchQueue.global(qos: .default).async { + _ = try! dbQueue.add(observation: observation) { count in + XCTAssertNotNil(DispatchQueue.getSpecific(key: key)) + counts.append(count) + notificationExpectation.fulfill() + } + + try! dbQueue.write { + try $0.execute("INSERT INTO t DEFAULT VALUES") + } + } + + waitForExpectations(timeout: 1, handler: nil) + XCTAssertEqual(counts, [0, 1]) + } + + func testMainQueueError() throws { + // We need something to change + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + var errorCount = 0 + let notificationExpectation = expectation(description: "notification") + notificationExpectation.assertForOverFulfill = true + notificationExpectation.expectedFulfillmentCount = 2 + + let key = DispatchSpecificKey<()>() + DispatchQueue.main.setSpecific(key: key, value: ()) + + var nextError: Error? = nil // If not null, reducer throws an error + let reducer = AnyValueReducer( + fetch: { _ -> Void in + if let error = nextError { + nextError = nil + throw error + } + }, + value: { $0 }) + var observation = ValueObservation.observing(DatabaseRegion.fullDatabase, reducer: reducer) + observation.extent = .databaseLifetime + + _ = try dbQueue.add( + observation: observation, + onError: { error in + XCTAssertNotNil(DispatchQueue.getSpecific(key: key)) + errorCount += 1 + notificationExpectation.fulfill() + }, + onChange: { _ in + XCTAssertNotNil(DispatchQueue.getSpecific(key: key)) + notificationExpectation.fulfill() + }) + + struct TestError: Error { } + nextError = TestError() + try dbQueue.write { + try $0.execute("INSERT INTO t DEFAULT VALUES") + } + + waitForExpectations(timeout: 1, handler: nil) + XCTAssertEqual(errorCount, 1) + } + + func testCustomQueueStartImmediately() throws { + // We need something to change + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + var counts: [Int] = [] + let notificationExpectation = expectation(description: "notification") + notificationExpectation.assertForOverFulfill = true + notificationExpectation.expectedFulfillmentCount = 2 + + let queue = DispatchQueue(label: "") + let key = DispatchSpecificKey<()>() + queue.setSpecific(key: key, value: ()) + + var observation = ValueObservation.observing(DatabaseRegion.fullDatabase, fetch: { + try Int.fetchOne($0, "SELECT COUNT(*) FROM t")! + }) + observation.extent = .databaseLifetime + observation.scheduling = .onQueue(queue, startImmediately: true) + + _ = try dbQueue.add(observation: observation) { count in + XCTAssertNotNil(DispatchQueue.getSpecific(key: key)) + counts.append(count) + notificationExpectation.fulfill() + } + + try dbQueue.write { + try $0.execute("INSERT INTO t DEFAULT VALUES") + } + + waitForExpectations(timeout: 1, handler: nil) + XCTAssertEqual(counts, [0, 1]) + } + + func testCustomQueueDontStartImmediately() throws { + // We need something to change + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + var counts: [Int] = [] + let notificationExpectation = expectation(description: "notification") + notificationExpectation.assertForOverFulfill = true + notificationExpectation.expectedFulfillmentCount = 1 + + let queue = DispatchQueue(label: "") + let key = DispatchSpecificKey<()>() + queue.setSpecific(key: key, value: ()) + + var observation = ValueObservation.observing(DatabaseRegion.fullDatabase, fetch: { + try Int.fetchOne($0, "SELECT COUNT(*) FROM t")! + }) + observation.extent = .databaseLifetime + observation.scheduling = .onQueue(queue, startImmediately: false) + + _ = try dbQueue.add(observation: observation) { count in + XCTAssertNotNil(DispatchQueue.getSpecific(key: key)) + counts.append(count) + notificationExpectation.fulfill() + } + + try dbQueue.write { + try $0.execute("INSERT INTO t DEFAULT VALUES") + } + + waitForExpectations(timeout: 1, handler: nil) + XCTAssertEqual(counts, [1]) + } + + func testCustomQueueError() throws { + // We need something to change + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } + + var errorCount = 0 + let notificationExpectation = expectation(description: "notification") + notificationExpectation.assertForOverFulfill = true + notificationExpectation.expectedFulfillmentCount = 1 + + let queue = DispatchQueue(label: "") + let key = DispatchSpecificKey<()>() + queue.setSpecific(key: key, value: ()) + + struct TestError: Error { } + let reducer = AnyValueReducer( + fetch: { _ in throw TestError() }, + value: { $0 }) + var observation = ValueObservation.observing(DatabaseRegion.fullDatabase, reducer: reducer) + observation.extent = .databaseLifetime + observation.scheduling = .onQueue(queue, startImmediately: false) + + _ = try dbQueue.add( + observation: observation, + onError: { error in + XCTAssertNotNil(DispatchQueue.getSpecific(key: key)) + errorCount += 1 + notificationExpectation.fulfill() + }, + onChange: { _ in fatalError() }) + + try dbQueue.write { + try $0.execute("INSERT INTO t DEFAULT VALUES") + } + + waitForExpectations(timeout: 1, handler: nil) + XCTAssertEqual(errorCount, 1) + } }