From c3d4838655cb574e608fa271bd61e4f9b70f171b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 5 Nov 2023 14:04:43 +0100 Subject: [PATCH] Fix test concurrency errors for ValueObservation fetch closures --- .../SharedValueObservationTests.swift | 22 +++++--- .../ValueObservationPrintTests.swift | 18 ++++-- Tests/GRDBTests/ValueObservationTests.swift | 56 +++++++++---------- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/Tests/GRDBTests/SharedValueObservationTests.swift b/Tests/GRDBTests/SharedValueObservationTests.swift index ca53c61d9..ff0fe213f 100644 --- a/Tests/GRDBTests/SharedValueObservationTests.swift +++ b/Tests/GRDBTests/SharedValueObservationTests.swift @@ -523,10 +523,13 @@ class SharedValueObservationTests: GRDBTestCase { } let log = Log() - var fetchError: Error? = nil + class Context { + var fetchError: Error? = nil + } + let context = Context() let publisher = ValueObservation .tracking { db -> Int in - if let error = fetchError { throw error } + if let error = context.fetchError { throw error } return try Table("player").fetchCount(db) } .print(to: log) @@ -540,7 +543,7 @@ class SharedValueObservationTests: GRDBTestCase { try XCTAssertEqual(wait(for: recorder1.next(), timeout: 1), 0) try XCTAssertEqual(wait(for: recorder2.next(), timeout: 1), 0) - fetchError = TestError() + context.fetchError = TestError() try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES")} if case .finished = try wait(for: recorder1.completion, timeout: 1) { XCTFail("Expected error") } @@ -557,7 +560,7 @@ class SharedValueObservationTests: GRDBTestCase { } do { - fetchError = nil + context.fetchError = nil let recorder = publisher.record() if case .finished = try wait(for: recorder.completion, timeout: 1) { XCTFail("Expected error") } XCTAssertEqual(log.flush(), []) @@ -579,10 +582,13 @@ class SharedValueObservationTests: GRDBTestCase { } let log = Log() - var fetchError: Error? = nil + class Context { + var fetchError: Error? = nil + } + let context = Context() let publisher = ValueObservation .tracking { db -> Int in - if let error = fetchError { throw error } + if let error = context.fetchError { throw error } return try Table("player").fetchCount(db) } .print(to: log) @@ -596,7 +602,7 @@ class SharedValueObservationTests: GRDBTestCase { try XCTAssertEqual(wait(for: recorder1.next(), timeout: 1), 0) try XCTAssertEqual(wait(for: recorder2.next(), timeout: 1), 0) - fetchError = TestError() + context.fetchError = TestError() try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES")} if case .finished = try wait(for: recorder1.completion, timeout: 1) { XCTFail("Expected error") } @@ -613,7 +619,7 @@ class SharedValueObservationTests: GRDBTestCase { } do { - fetchError = nil + context.fetchError = nil let recorder = publisher.record() try XCTAssertEqual(wait(for: recorder.next(), timeout: 1), 1) XCTAssertEqual(log.flush(), ["start", "fetch", "tracked region: player(*)", "value: 1"]) diff --git a/Tests/GRDBTests/ValueObservationPrintTests.swift b/Tests/GRDBTests/ValueObservationPrintTests.swift index 51f57f5b6..1da1fad3e 100644 --- a/Tests/GRDBTests/ValueObservationPrintTests.swift +++ b/Tests/GRDBTests/ValueObservationPrintTests.swift @@ -410,11 +410,14 @@ class ValueObservationPrintTests: GRDBTestCase { // Force DatabasePool to perform two initial fetches, because between // its first read access, and its write access that installs the // transaction observer, some write did happen. - var needsChange = true + class Context { + var needsChange = true + } + let context = Context() let observation = ValueObservation .trackingConstantRegion { db -> Int? in - if needsChange { - needsChange = false + if context.needsChange { + context.needsChange = false try dbPool.write { db in try db.execute(sql: """ INSERT INTO player DEFAULT VALUES; @@ -461,11 +464,14 @@ class ValueObservationPrintTests: GRDBTestCase { // Force DatabasePool to perform two initial fetches, because between // its first read access, and its write access that installs the // transaction observer, some write did happen. - var needsChange = true + class Context { + var needsChange = true + } + let context = Context() let observation = ValueObservation .trackingConstantRegion { db -> Int? in - if needsChange { - needsChange = false + if context.needsChange { + context.needsChange = false try dbPool.write { db in try db.execute(sql: """ INSERT INTO player DEFAULT VALUES; diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index 5be326a73..ce1293b31 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -331,10 +331,14 @@ class ValueObservationTests: GRDBTestCase { // Force DatabasePool to perform two initial fetches, because between // its first read access, and its write access that installs the // transaction observer, some write did happen. - var needsChange = true + class Context { + var needsChange = true + var observedCounts: [Int] = [] + } + let context = Context() let observation = ValueObservation.trackingConstantRegion { db -> Int in - if needsChange { - needsChange = false + if context.needsChange { + context.needsChange = false try dbPool.write { db in try db.execute(sql: """ INSERT INTO t DEFAULT VALUES; @@ -347,10 +351,6 @@ class ValueObservationTests: GRDBTestCase { let expectation = self.expectation(description: "") expectation.expectedFulfillmentCount = 2 - class Context { - var observedCounts: [Int] = [] - } - let context = Context() let cancellable = observation.start( in: dbPool, scheduling: .async(onQueue: .main), @@ -374,10 +374,14 @@ class ValueObservationTests: GRDBTestCase { // Force DatabasePool to perform two initial fetches, because between // its first read access, and its write access that installs the // transaction observer, some write did happen. - var needsChange = true + class Context { + var needsChange = true + var observedCounts: [Int] = [] + } + let context = Context() let observation = ValueObservation.trackingConstantRegion { db -> Int in - if needsChange { - needsChange = false + if context.needsChange { + context.needsChange = false try dbPool.write { db in try db.execute(sql: """ INSERT INTO t DEFAULT VALUES; @@ -390,10 +394,6 @@ class ValueObservationTests: GRDBTestCase { let expectation = self.expectation(description: "") expectation.expectedFulfillmentCount = 2 - class Context { - var observedCounts: [Int] = [] - } - let context = Context() let cancellable = observation.start( in: dbPool, scheduling: .immediate, @@ -417,10 +417,14 @@ class ValueObservationTests: GRDBTestCase { // Allow pool to perform a single initial fetch, because between // its first read access, and its write access that installs the // transaction observer, no write did happen. - var needsChange = true + class Context { + var needsChange = true + var observedCounts: [Int] = [] + } + let context = Context() let observation = ValueObservation.trackingConstantRegion { db -> Int in - if needsChange { - needsChange = false + if context.needsChange { + context.needsChange = false DispatchQueue.main.asyncAfter(deadline: .now() + 1) { try! dbPool.write { db in try db.execute(sql: """ @@ -443,10 +447,6 @@ class ValueObservationTests: GRDBTestCase { let expectation = self.expectation(description: "") expectation.expectedFulfillmentCount = expectedCounts.count - class Context { - var observedCounts: [Int] = [] - } - let context = Context() let cancellable = observation.start( in: dbPool, scheduling: .async(onQueue: .main), @@ -470,10 +470,14 @@ class ValueObservationTests: GRDBTestCase { // Allow pool to perform a single initial fetch, because between // its first read access, and its write access that installs the // transaction observer, no write did happen. - var needsChange = true + class Context { + var needsChange = true + var observedCounts: [Int] = [] + } + let context = Context() let observation = ValueObservation.trackingConstantRegion { db -> Int in - if needsChange { - needsChange = false + if context.needsChange { + context.needsChange = false DispatchQueue.main.asyncAfter(deadline: .now() + 1) { try! dbPool.write { db in try db.execute(sql: """ @@ -496,10 +500,6 @@ class ValueObservationTests: GRDBTestCase { let expectation = self.expectation(description: "") expectation.expectedFulfillmentCount = expectedCounts.count - class Context { - var observedCounts: [Int] = [] - } - let context = Context() let cancellable = observation.start( in: dbPool, scheduling: .immediate,