Skip to content

Commit

Permalink
Fix Sendable warnings in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Sep 8, 2024
1 parent 4b5ec4f commit 945f398
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 206 deletions.
40 changes: 20 additions & 20 deletions Tests/GRDBTests/DatabaseAfterNextTransactionCommitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DatabaseAfterNextTransactionCommitTests: GRDBTestCase {

let dbQueue = try makeDatabaseQueue()
try dbQueue.writeWithoutTransaction { db in
var commitCount = 0
let commitCountMutex = Mutex(0)
weak var deallocationWitness: Witness? = nil
do {
let witness = Witness()
Expand All @@ -51,19 +51,19 @@ class DatabaseAfterNextTransactionCommitTests: GRDBTestCase {
db.afterNextTransaction { _ in
// use witness
withExtendedLifetime(witness, { })
commitCount += 1
commitCountMutex.increment()
}
}

XCTAssertNotNil(deallocationWitness)
XCTAssertEqual(commitCount, 0)
XCTAssertEqual(commitCountMutex.load(), 0)
try db.execute(sql: startSQL)
try db.execute(sql: endSQL)
switch expectedCompletion {
case .commit:
XCTAssertEqual(commitCount, 1, "\(startSQL); \(endSQL)")
XCTAssertEqual(commitCountMutex.load(), 1, "\(startSQL); \(endSQL)")
case .rollback:
XCTAssertEqual(commitCount, 0, "\(startSQL); \(endSQL)")
XCTAssertEqual(commitCountMutex.load(), 0, "\(startSQL); \(endSQL)")
}
XCTAssertNil(deallocationWitness)

Expand All @@ -73,9 +73,9 @@ class DatabaseAfterNextTransactionCommitTests: GRDBTestCase {
}
switch expectedCompletion {
case .commit:
XCTAssertEqual(commitCount, 1, "\(startSQL); \(endSQL)")
XCTAssertEqual(commitCountMutex.load(), 1, "\(startSQL); \(endSQL)")
case .rollback:
XCTAssertEqual(commitCount, 0, "\(startSQL); \(endSQL)")
XCTAssertEqual(commitCountMutex.load(), 0, "\(startSQL); \(endSQL)")
}
}
}
Expand All @@ -85,8 +85,8 @@ class DatabaseAfterNextTransactionCommitTests: GRDBTestCase {

let dbQueue = try makeDatabaseQueue()
try dbQueue.writeWithoutTransaction { db in
var commitCount = 0
var rollbackCount = 0
let commitCountMutex = Mutex(0)
let rollbackCountMutex = Mutex(0)
try db.execute(sql: startSQL)

weak var deallocationWitness: Witness? = nil
Expand All @@ -98,25 +98,25 @@ class DatabaseAfterNextTransactionCommitTests: GRDBTestCase {
onCommit: { _ in
// use witness
withExtendedLifetime(witness, { })
commitCount += 1
commitCountMutex.increment()
},
onRollback: { _ in
// use witness
withExtendedLifetime(witness, { })
rollbackCount += 1
rollbackCountMutex.increment()
})
}

XCTAssertNotNil(deallocationWitness)
XCTAssertEqual(commitCount, 0)
XCTAssertEqual(commitCountMutex.load(), 0)
try db.execute(sql: endSQL)
switch expectedCompletion {
case .commit:
XCTAssertEqual(commitCount, 1, "\(startSQL); \(endSQL)")
XCTAssertEqual(rollbackCount, 0, "\(startSQL); \(endSQL)")
XCTAssertEqual(commitCountMutex.load(), 1, "\(startSQL); \(endSQL)")
XCTAssertEqual(rollbackCountMutex.load(), 0, "\(startSQL); \(endSQL)")
case .rollback:
XCTAssertEqual(commitCount, 0, "\(startSQL); \(endSQL)")
XCTAssertEqual(rollbackCount, 1, "\(startSQL); \(endSQL)")
XCTAssertEqual(commitCountMutex.load(), 0, "\(startSQL); \(endSQL)")
XCTAssertEqual(rollbackCountMutex.load(), 1, "\(startSQL); \(endSQL)")
}
XCTAssertNil(deallocationWitness)

Expand All @@ -126,11 +126,11 @@ class DatabaseAfterNextTransactionCommitTests: GRDBTestCase {
}
switch expectedCompletion {
case .commit:
XCTAssertEqual(commitCount, 1, "\(startSQL); \(endSQL)")
XCTAssertEqual(rollbackCount, 0, "\(startSQL); \(endSQL)")
XCTAssertEqual(commitCountMutex.load(), 1, "\(startSQL); \(endSQL)")
XCTAssertEqual(rollbackCountMutex.load(), 0, "\(startSQL); \(endSQL)")
case .rollback:
XCTAssertEqual(commitCount, 0, "\(startSQL); \(endSQL)")
XCTAssertEqual(rollbackCount, 1, "\(startSQL); \(endSQL)")
XCTAssertEqual(commitCountMutex.load(), 0, "\(startSQL); \(endSQL)")
XCTAssertEqual(rollbackCountMutex.load(), 1, "\(startSQL); \(endSQL)")
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions Tests/GRDBTests/DatabasePoolConcurrencyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1078,13 +1078,13 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {

func testAsyncConcurrentReadOpensATransaction() throws {
let dbPool = try makeDatabasePool()
var isInsideTransaction: Bool? = nil
let isInsideTransactionMutex: Mutex<Bool?> = Mutex(nil)
let expectation = self.expectation(description: "read")
dbPool.writeWithoutTransaction { db in
dbPool.asyncConcurrentRead { dbResult in
do {
let db = try dbResult.get()
isInsideTransaction = db.isInsideTransaction
isInsideTransactionMutex.store(db.isInsideTransaction)
do {
try db.execute(sql: "BEGIN DEFERRED TRANSACTION")
XCTFail("Expected error")
Expand All @@ -1097,7 +1097,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
}
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(isInsideTransaction, true)
XCTAssertEqual(isInsideTransactionMutex.load(), true)
}

func testAsyncConcurrentReadOutsideOfTransaction() throws {
Expand All @@ -1120,14 +1120,14 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
// <
// }

var count: Int? = nil
let countMutex: Mutex<Int?> = Mutex(nil)
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")!
try countMutex.store(Int.fetchOne(db, sql: "SELECT COUNT(*) FROM persons")!)
} catch {
XCTFail("Unexpected error: \(error)")
}
Expand All @@ -1137,14 +1137,14 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
s1.signal()
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(count, 0)
XCTAssertEqual(countMutex.load(), 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
let readErrorMutex: Mutex<DatabaseError?> = Mutex(nil)
let expectation = self.expectation(description: "read")
try dbPool.writeWithoutTransaction { db in
try db.execute(sql: "PRAGMA locking_mode=EXCLUSIVE")
Expand All @@ -1156,12 +1156,12 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
XCTFail("Unexpected result: \(dbResult)")
return
}
readError = dbError
readErrorMutex.store(dbError)
expectation.fulfill()
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(readError!.resultCode, .SQLITE_BUSY)
XCTAssertEqual(readError!.message!, "database is locked")
XCTAssertEqual(readErrorMutex.load()!.resultCode, .SQLITE_BUSY)
XCTAssertEqual(readErrorMutex.load()!.message!, "database is locked")
}
}

Expand Down
24 changes: 10 additions & 14 deletions Tests/GRDBTests/DatabasePoolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ class DatabasePoolTests: GRDBTestCase {
let group = DispatchGroup()

// The maximum number of threads we could witness
var maxThreadCount: CInt = 0
let lock = NSLock()

let maxThreadCountMutex: Mutex<CInt> = Mutex(0)
for _ in (0..<numberOfConcurrentReads) {
group.enter()
pool.asyncUnsafeRead { result in
Expand All @@ -248,15 +246,15 @@ class DatabasePoolTests: GRDBTestCase {
}

let threadsCount = getThreadsCount()
lock.lock()
maxThreadCount = max(maxThreadCount, threadsCount)
lock.unlock()
maxThreadCountMutex.withLock {
$0 = max($0, threadsCount)
}

group.leave()
}
}
group.wait()
XCTAssert(maxThreadCount < 50)
XCTAssert(maxThreadCountMutex.load() < 50)
#endif
}

Expand All @@ -279,9 +277,7 @@ class DatabasePoolTests: GRDBTestCase {
let group = DispatchGroup()

// The maximum number of threads we could witness
var maxThreadCount: CInt = 0
let lock = NSLock()

let maxThreadCountMutex: Mutex<CInt> = Mutex(0)
for _ in (0..<numberOfConcurrentReads) {
group.enter()
pool.asyncRead { result in
Expand All @@ -290,15 +286,15 @@ class DatabasePoolTests: GRDBTestCase {
}

let threadsCount = getThreadsCount()
lock.lock()
maxThreadCount = max(maxThreadCount, threadsCount)
lock.unlock()
maxThreadCountMutex.withLock {
$0 = max($0, threadsCount)
}

group.leave()
}
}
group.wait()
XCTAssert(maxThreadCount < 50)
XCTAssert(maxThreadCountMutex.load() < 50)
#endif
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/GRDBTests/DatabaseReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ class DatabaseReaderTests : GRDBTestCase {
func test(_ dbReader: some DatabaseReader) throws {
let expectation = self.expectation(description: "updates")
let semaphore = DispatchSemaphore(value: 0)
var count: Int?
let countMutex: Mutex<Int?> = Mutex(nil)
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")
try countMutex.store(Int.fetchOne(dbResult.get(), sql: "SELECT COUNT(*) FROM sqlite_master"))
} catch {
XCTFail("Unexpected error: \(error)")
}
Expand All @@ -238,7 +238,7 @@ class DatabaseReaderTests : GRDBTestCase {
semaphore.signal()

waitForExpectations(timeout: 1, handler: nil)
XCTAssertNotNil(count)
XCTAssertNotNil(countMutex.load())
}

try test(makeDatabaseQueue())
Expand Down
32 changes: 16 additions & 16 deletions Tests/GRDBTests/DatabaseRegionObservationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class DatabaseRegionObservationTests: GRDBTestCase {

let observation = DatabaseRegionObservation(tracking: .fullDatabase)

var count = 0
let countMutex = Mutex(0)
let cancellable = observation.start(
in: dbQueue,
onError: { XCTFail("Unexpected error: \($0)") },
onChange: { db in
count += 1
countMutex.increment()
notificationExpectation.fulfill()
})

Expand All @@ -49,7 +49,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
waitForExpectations(timeout: 1, handler: nil)

XCTAssertEqual(count, 3)
XCTAssertEqual(countMutex.load(), 3)
}
}

Expand Down Expand Up @@ -96,12 +96,12 @@ class DatabaseRegionObservationTests: GRDBTestCase {

let observation = DatabaseRegionObservation(tracking: request1, request2)

var count = 0
let countMutex = Mutex(0)
let cancellable = observation.start(
in: dbQueue,
onError: { XCTFail("Unexpected error: \($0)") },
onChange: { db in
count += 1
countMutex.increment()
notificationExpectation.fulfill()
})

Expand All @@ -118,7 +118,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
waitForExpectations(timeout: 1, handler: nil)

XCTAssertEqual(count, 3)
XCTAssertEqual(countMutex.load(), 3)
}
}

Expand All @@ -138,12 +138,12 @@ class DatabaseRegionObservationTests: GRDBTestCase {

let observation = DatabaseRegionObservation(tracking: [request1, request2])

var count = 0
let countMutex = Mutex(0)
let cancellable = observation.start(
in: dbQueue,
onError: { XCTFail("Unexpected error: \($0)") },
onChange: { db in
count += 1
countMutex.increment()
notificationExpectation.fulfill()
})

Expand All @@ -160,7 +160,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
waitForExpectations(timeout: 1, handler: nil)

XCTAssertEqual(count, 3)
XCTAssertEqual(countMutex.load(), 3)
}
}

Expand All @@ -174,13 +174,13 @@ class DatabaseRegionObservationTests: GRDBTestCase {

let observation = DatabaseRegionObservation(tracking: SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id"))

var count = 0
let countMutex = Mutex(0)
do {
let cancellable = observation.start(
in: dbQueue,
onError: { XCTFail("Unexpected error: \($0)") },
onChange: { db in
count += 1
countMutex.increment()
notificationExpectation.fulfill()
})

Expand All @@ -199,7 +199,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
waitForExpectations(timeout: 1, handler: nil)

XCTAssertEqual(count, 2)
XCTAssertEqual(countMutex.load(), 2)
}

func testDatabaseRegionExtentNextTransaction() throws {
Expand All @@ -212,14 +212,14 @@ class DatabaseRegionObservationTests: GRDBTestCase {

let observation = DatabaseRegionObservation(tracking: SQLRequest<Row>(sql: "SELECT * FROM t ORDER BY id"))

var count = 0
var cancellable: AnyDatabaseCancellable?
let countMutex = Mutex(0)
nonisolated(unsafe) var cancellable: AnyDatabaseCancellable?
cancellable = observation.start(
in: dbQueue,
onError: { XCTFail("Unexpected error: \($0)") },
onChange: { db in
cancellable?.cancel()
count += 1
countMutex.increment()
notificationExpectation.fulfill()
})

Expand All @@ -233,7 +233,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
waitForExpectations(timeout: 1, handler: nil)

XCTAssertEqual(count, 1)
XCTAssertEqual(countMutex.load(), 1)
}
}

Expand Down
Loading

0 comments on commit 945f398

Please sign in to comment.