mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Fix test concurrency errors
This commit is contained in:
1 parent
d83718c231
commit
cd5af9a2eb
6 files changed
+121
-77
No files matched your search
@@ -122,7 +122,7 @@ class DatabaseMigratorTests : GRDBTestCase {
|
||||
}
|
||||
|
||||
let expectation = self.expectation(description: "")
|
||||
migrator.asyncMigrate(writer, completion: { dbResult in
|
||||
migrator.asyncMigrate(writer, completion: { [migrator2] dbResult in
|
||||
// No migration error
|
||||
let db = try! dbResult.get()
|
||||
|
||||
|
||||
@@ -1146,13 +1146,16 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
|
||||
func testAsyncConcurrentReadOpensATransaction() throws {
|
||||
let dbPool = try makeDatabasePool()
|
||||
var isInsideTransaction: Bool? = nil
|
||||
class Context {
|
||||
var isInsideTransaction: Bool? = nil
|
||||
}
|
||||
let context = Context()
|
||||
let expectation = self.expectation(description: "read")
|
||||
dbPool.writeWithoutTransaction { db in
|
||||
dbPool.asyncConcurrentRead { dbResult in
|
||||
do {
|
||||
let db = try dbResult.get()
|
||||
isInsideTransaction = db.isInsideTransaction
|
||||
context.isInsideTransaction = db.isInsideTransaction
|
||||
do {
|
||||
try db.execute(sql: "BEGIN DEFERRED TRANSACTION")
|
||||
XCTFail("Expected error")
|
||||
@@ -1165,7 +1168,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(isInsideTransaction, true)
|
||||
XCTAssertEqual(context.isInsideTransaction, true)
|
||||
}
|
||||
|
||||
func testAsyncConcurrentReadOutsideOfTransaction() throws {
|
||||
@@ -1188,14 +1191,17 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
// <
|
||||
// }
|
||||
|
||||
var count: Int? = nil
|
||||
class Context {
|
||||
var count: Int?
|
||||
}
|
||||
let context = Context()
|
||||
let expectation = self.expectation(description: "read")
|
||||
try dbPool.writeWithoutTransaction { db in
|
||||
dbPool.asyncConcurrentRead { dbResult in
|
||||
do {
|
||||
_ = s1.wait(timeout: .distantFuture)
|
||||
let db = try dbResult.get()
|
||||
count = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM persons")!
|
||||
context.count = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM persons")!
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
@@ -1205,14 +1211,17 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
s1.signal()
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(count, 0)
|
||||
XCTAssertEqual(context.count, 0)
|
||||
}
|
||||
|
||||
func testAsyncConcurrentReadError() throws {
|
||||
// Necessary for this test to run as quickly as possible
|
||||
dbConfiguration.readonlyBusyMode = .immediateError
|
||||
let dbPool = try makeDatabasePool()
|
||||
var readError: DatabaseError? = nil
|
||||
class Context {
|
||||
var readError: DatabaseError? = nil
|
||||
}
|
||||
let context = Context()
|
||||
let expectation = self.expectation(description: "read")
|
||||
try dbPool.writeWithoutTransaction { db in
|
||||
try db.execute(sql: "PRAGMA locking_mode=EXCLUSIVE")
|
||||
@@ -1224,12 +1233,12 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
XCTFail("Unexpected result: \(dbResult)")
|
||||
return
|
||||
}
|
||||
readError = dbError
|
||||
context.readError = dbError
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(readError!.resultCode, .SQLITE_BUSY)
|
||||
XCTAssertEqual(readError!.message!, "database is locked")
|
||||
XCTAssertEqual(context.readError!.resultCode, .SQLITE_BUSY)
|
||||
XCTAssertEqual(context.readError!.message!, "database is locked")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -228,7 +228,10 @@ class DatabasePoolTests: GRDBTestCase {
|
||||
let group = DispatchGroup()
|
||||
|
||||
// The maximum number of threads we could witness
|
||||
var maxThreadCount: CInt = 0
|
||||
class Context {
|
||||
var maxThreadCount: CInt = 0
|
||||
}
|
||||
let context = Context()
|
||||
let lock = NSLock()
|
||||
|
||||
for _ in (0..<numberOfConcurrentReads) {
|
||||
@@ -240,14 +243,14 @@ class DatabasePoolTests: GRDBTestCase {
|
||||
|
||||
let threadsCount = getThreadsCount()
|
||||
lock.lock()
|
||||
maxThreadCount = max(maxThreadCount, threadsCount)
|
||||
context.maxThreadCount = max(context.maxThreadCount, threadsCount)
|
||||
lock.unlock()
|
||||
|
||||
group.leave()
|
||||
}
|
||||
}
|
||||
group.wait()
|
||||
XCTAssert(maxThreadCount < 50)
|
||||
XCTAssert(context.maxThreadCount < 50)
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -270,7 +273,10 @@ class DatabasePoolTests: GRDBTestCase {
|
||||
let group = DispatchGroup()
|
||||
|
||||
// The maximum number of threads we could witness
|
||||
var maxThreadCount: CInt = 0
|
||||
class Context {
|
||||
var maxThreadCount: CInt = 0
|
||||
}
|
||||
let context = Context()
|
||||
let lock = NSLock()
|
||||
|
||||
for _ in (0..<numberOfConcurrentReads) {
|
||||
@@ -282,14 +288,14 @@ class DatabasePoolTests: GRDBTestCase {
|
||||
|
||||
let threadsCount = getThreadsCount()
|
||||
lock.lock()
|
||||
maxThreadCount = max(maxThreadCount, threadsCount)
|
||||
context.maxThreadCount = max(context.maxThreadCount, threadsCount)
|
||||
lock.unlock()
|
||||
|
||||
group.leave()
|
||||
}
|
||||
}
|
||||
group.wait()
|
||||
XCTAssert(maxThreadCount < 50)
|
||||
XCTAssert(context.maxThreadCount < 50)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -205,15 +205,19 @@ class DatabaseReaderTests : GRDBTestCase {
|
||||
// MARK: - AsyncRead
|
||||
|
||||
func testAsyncRead() throws {
|
||||
class Context {
|
||||
var count: Int?
|
||||
}
|
||||
|
||||
func test(_ dbReader: some DatabaseReader) throws {
|
||||
let context = Context()
|
||||
let expectation = self.expectation(description: "updates")
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
var count: Int?
|
||||
dbReader.asyncRead { dbResult in
|
||||
// Make sure this block executes asynchronously
|
||||
semaphore.wait()
|
||||
do {
|
||||
count = try Int.fetchOne(dbResult.get(), sql: "SELECT COUNT(*) FROM sqlite_master")
|
||||
context.count = try Int.fetchOne(dbResult.get(), sql: "SELECT COUNT(*) FROM sqlite_master")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
@@ -222,7 +226,7 @@ class DatabaseReaderTests : GRDBTestCase {
|
||||
semaphore.signal()
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertNotNil(count)
|
||||
XCTAssertNotNil(context.count)
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
|
||||
@@ -130,7 +130,10 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
|
||||
var results: [Int] = []
|
||||
class Context {
|
||||
var results: [Int] = []
|
||||
}
|
||||
let context = Context()
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 4
|
||||
@@ -147,7 +150,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
in: dbQueue,
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { count in
|
||||
results.append(count)
|
||||
context.results.append(count)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
|
||||
@@ -162,7 +165,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(results, [0, 1, 2, 3])
|
||||
XCTAssertEqual(context.results, [0, 1, 2, 3])
|
||||
|
||||
XCTAssertEqual(regions.map(\.description), [
|
||||
"a(value),source(name)",
|
||||
@@ -181,7 +184,10 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
|
||||
var results: [Int] = []
|
||||
class Context {
|
||||
var results: [Int] = []
|
||||
}
|
||||
let context = Context()
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 4
|
||||
@@ -199,7 +205,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
scheduling: .async(onQueue: .main),
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { count in
|
||||
results.append(count)
|
||||
context.results.append(count)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
|
||||
@@ -214,7 +220,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(results, [0, 1, 2, 3])
|
||||
XCTAssertEqual(context.results, [0, 1, 2, 3])
|
||||
|
||||
XCTAssertEqual(regions.map(\.description), [
|
||||
"a(value),source(name)",
|
||||
|
||||
@@ -5,19 +5,22 @@ import Dispatch
|
||||
class ValueObservationTests: GRDBTestCase {
|
||||
func testImmediateError() throws {
|
||||
struct TestError: Error { }
|
||||
class Context {
|
||||
var error: TestError?
|
||||
}
|
||||
|
||||
func test(_ dbWriter: some DatabaseWriter) throws {
|
||||
// Create an observation
|
||||
let observation = ValueObservation.trackingConstantRegion { _ in throw TestError() }
|
||||
|
||||
// Start observation
|
||||
var error: TestError?
|
||||
let context = Context()
|
||||
_ = observation.start(
|
||||
in: dbWriter,
|
||||
scheduling: .immediate,
|
||||
onError: { error = $0 as? TestError },
|
||||
onError: { context.error = $0 as? TestError },
|
||||
onChange: { _ in })
|
||||
XCTAssertNotNil(error)
|
||||
XCTAssertNotNil(context.error)
|
||||
}
|
||||
|
||||
try test(makeDatabaseQueue())
|
||||
@@ -26,36 +29,39 @@ class ValueObservationTests: GRDBTestCase {
|
||||
|
||||
func testErrorCompletesTheObservation() throws {
|
||||
struct TestError: Error { }
|
||||
class Context {
|
||||
var nextError: Error? = nil // If not null, observation throws an error
|
||||
var errorCaught = false
|
||||
}
|
||||
|
||||
func test(_ dbWriter: some DatabaseWriter) throws {
|
||||
// We need something to change
|
||||
try dbWriter.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
|
||||
|
||||
// Track reducer process
|
||||
let context = Context()
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 4
|
||||
notificationExpectation.isInverted = true
|
||||
|
||||
var nextError: Error? = nil // If not null, observation throws an error
|
||||
let observation = ValueObservation.trackingConstantRegion {
|
||||
_ = try Int.fetchOne($0, sql: "SELECT COUNT(*) FROM t")
|
||||
if let error = nextError {
|
||||
if let error = context.nextError {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// Start observation
|
||||
var errorCaught = false
|
||||
let cancellable = observation.start(
|
||||
in: dbWriter,
|
||||
onError: { _ in
|
||||
errorCaught = true
|
||||
context.errorCaught = true
|
||||
notificationExpectation.fulfill()
|
||||
},
|
||||
onChange: {
|
||||
XCTAssertFalse(errorCaught)
|
||||
nextError = TestError()
|
||||
XCTAssertFalse(context.errorCaught)
|
||||
context.nextError = TestError()
|
||||
notificationExpectation.fulfill()
|
||||
// Trigger another change
|
||||
try! dbWriter.writeWithoutTransaction { db in
|
||||
@@ -65,7 +71,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
|
||||
withExtendedLifetime(cancellable) {
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
XCTAssertTrue(errorCaught)
|
||||
XCTAssertTrue(context.errorCaught)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,18 +347,21 @@ class ValueObservationTests: GRDBTestCase {
|
||||
|
||||
let expectation = self.expectation(description: "")
|
||||
expectation.expectedFulfillmentCount = 2
|
||||
var observedCounts: [Int] = []
|
||||
class Context {
|
||||
var observedCounts: [Int] = []
|
||||
}
|
||||
let context = Context()
|
||||
let cancellable = observation.start(
|
||||
in: dbPool,
|
||||
scheduling: .async(onQueue: .main),
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { count in
|
||||
observedCounts.append(count)
|
||||
context.observedCounts.append(count)
|
||||
expectation.fulfill()
|
||||
})
|
||||
withExtendedLifetime(cancellable) {
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
XCTAssertEqual(observedCounts, [0, 0])
|
||||
XCTAssertEqual(context.observedCounts, [0, 0])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,18 +390,21 @@ class ValueObservationTests: GRDBTestCase {
|
||||
|
||||
let expectation = self.expectation(description: "")
|
||||
expectation.expectedFulfillmentCount = 2
|
||||
var observedCounts: [Int] = []
|
||||
class Context {
|
||||
var observedCounts: [Int] = []
|
||||
}
|
||||
let context = Context()
|
||||
let cancellable = observation.start(
|
||||
in: dbPool,
|
||||
scheduling: .immediate,
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { count in
|
||||
observedCounts.append(count)
|
||||
context.observedCounts.append(count)
|
||||
expectation.fulfill()
|
||||
})
|
||||
withExtendedLifetime(cancellable) {
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
XCTAssertEqual(observedCounts, [0, 0])
|
||||
XCTAssertEqual(context.observedCounts, [0, 0])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,18 +443,21 @@ class ValueObservationTests: GRDBTestCase {
|
||||
|
||||
let expectation = self.expectation(description: "")
|
||||
expectation.expectedFulfillmentCount = expectedCounts.count
|
||||
var observedCounts: [Int] = []
|
||||
class Context {
|
||||
var observedCounts: [Int] = []
|
||||
}
|
||||
let context = Context()
|
||||
let cancellable = observation.start(
|
||||
in: dbPool,
|
||||
scheduling: .async(onQueue: .main),
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { count in
|
||||
observedCounts.append(count)
|
||||
context.observedCounts.append(count)
|
||||
expectation.fulfill()
|
||||
})
|
||||
withExtendedLifetime(cancellable) {
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
XCTAssertEqual(observedCounts, expectedCounts)
|
||||
XCTAssertEqual(context.observedCounts, expectedCounts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,18 +496,21 @@ class ValueObservationTests: GRDBTestCase {
|
||||
|
||||
let expectation = self.expectation(description: "")
|
||||
expectation.expectedFulfillmentCount = expectedCounts.count
|
||||
var observedCounts: [Int] = []
|
||||
class Context {
|
||||
var observedCounts: [Int] = []
|
||||
}
|
||||
let context = Context()
|
||||
let cancellable = observation.start(
|
||||
in: dbPool,
|
||||
scheduling: .immediate,
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { count in
|
||||
observedCounts.append(count)
|
||||
context.observedCounts.append(count)
|
||||
expectation.fulfill()
|
||||
})
|
||||
withExtendedLifetime(cancellable) {
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
XCTAssertEqual(observedCounts, expectedCounts)
|
||||
XCTAssertEqual(context.observedCounts, expectedCounts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -531,7 +549,11 @@ class ValueObservationTests: GRDBTestCase {
|
||||
try dbQueue.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
|
||||
|
||||
// Track reducer process
|
||||
var changesCount = 0
|
||||
class Context {
|
||||
var changesCount = 0
|
||||
var cancellable: (any DatabaseCancellable)?
|
||||
}
|
||||
let context = Context()
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
@@ -542,14 +564,13 @@ class ValueObservationTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
// Start observation and deallocate cancellable after second change
|
||||
var cancellable: (any DatabaseCancellable)?
|
||||
cancellable = observation.start(
|
||||
context.cancellable = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { _ in
|
||||
changesCount += 1
|
||||
if changesCount == 2 {
|
||||
cancellable = nil
|
||||
context.changesCount += 1
|
||||
if context.changesCount == 2 {
|
||||
context.cancellable = nil
|
||||
}
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
@@ -564,11 +585,8 @@ class ValueObservationTests: GRDBTestCase {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
// Avoid "Variable 'cancellable' was written to, but never read" warning
|
||||
_ = cancellable
|
||||
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
XCTAssertEqual(changesCount, 2)
|
||||
XCTAssertEqual(context.changesCount, 2)
|
||||
}
|
||||
|
||||
func testCancellableExplicitCancellation() throws {
|
||||
@@ -577,7 +595,11 @@ class ValueObservationTests: GRDBTestCase {
|
||||
try dbQueue.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }
|
||||
|
||||
// Track reducer process
|
||||
var changesCount = 0
|
||||
class Context {
|
||||
var changesCount = 0
|
||||
var cancellable: (any DatabaseCancellable)!
|
||||
}
|
||||
let context = Context()
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
@@ -588,32 +610,29 @@ class ValueObservationTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
// Start observation and cancel cancellable after second change
|
||||
var cancellable: (any DatabaseCancellable)!
|
||||
cancellable = observation.start(
|
||||
context.cancellable = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in XCTFail("Unexpected error: \(error)") },
|
||||
onChange: { _ in
|
||||
changesCount += 1
|
||||
if changesCount == 2 {
|
||||
cancellable.cancel()
|
||||
context.changesCount += 1
|
||||
if context.changesCount == 2 {
|
||||
context.cancellable.cancel()
|
||||
}
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
|
||||
try withExtendedLifetime(cancellable) {
|
||||
// notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
// not notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
XCTAssertEqual(changesCount, 2)
|
||||
// notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
// not notified
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
XCTAssertEqual(context.changesCount, 2)
|
||||
}
|
||||
|
||||
func testCancellableInvalidation1() throws {
|
||||
|
||||
Reference in new issue
Block a user