mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Fix test concurrency errors for ValueObservation fetch closures
This commit is contained in:
1 parent
cd5af9a2eb
commit
c3d4838655
3 files changed
+54
-42
No files matched your search
@@ -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"])
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user