Test and fix unsafe and async DatabaseSnapshotPool

This commit is contained in:
Gwendal Roué committed 2022-11-14 19:10:52 +01:00
1 parent 9ac8c0703d
commit c4c0b1ce90
2 files changed
+55 -2

No files matched your search

+2 -1
View File
@@ -117,6 +117,7 @@ extension DatabaseSnapshotPool: DatabaseReader {
dbPool.interrupt()
}
@_disfavoredOverload // SR-15150 Async overloading in protocol implementation fails
public func read<T>(_ value: (Database) throws -> T) throws -> T {
try dbPool.read { db in
try db.read(from: self) {
@@ -145,7 +146,7 @@ extension DatabaseSnapshotPool: DatabaseReader {
public func asyncUnsafeRead(_ value: @escaping (Result<Database, Error>) -> Void) {
/// There is no unsafe access to a snapshot.
dbPool.asyncRead(value)
asyncRead(value)
}
public func unsafeReentrantRead<T>(_ value: (Database) throws -> T) throws -> T {
@@ -39,7 +39,7 @@ final class DatabaseSnapshotPoolTests: GRDBTestCase {
} catch DatabaseError.SQLITE_ERROR { }
}
func testSnapshotPool() throws {
func testSnapshotPool_read() throws {
let dbPool = try makeDatabasePool()
let counter = try Counter(dbPool: dbPool)
@@ -53,6 +53,58 @@ final class DatabaseSnapshotPoolTests: GRDBTestCase {
try XCTAssertEqual(dbPool.read(counter.value), 2)
}
func testSnapshotPool_unsafeRead() throws {
let dbPool = try makeDatabasePool()
let counter = try Counter(dbPool: dbPool)
try dbPool.write(counter.increment)
let snapshot = try dbPool.makeSnapshotPool()
try dbPool.write(counter.increment)
try XCTAssertEqual(dbPool.read(counter.value), 2)
try XCTAssertEqual(snapshot.unsafeRead(counter.value), 1)
// Reuse the last connection
try XCTAssertEqual(dbPool.read(counter.value), 2)
}
func testSnapshotPool_unsafeReentrantRead() throws {
let dbPool = try makeDatabasePool()
let counter = try Counter(dbPool: dbPool)
try dbPool.write(counter.increment)
let snapshot = try dbPool.makeSnapshotPool()
try dbPool.write(counter.increment)
try XCTAssertEqual(dbPool.read(counter.value), 2)
try XCTAssertEqual(snapshot.unsafeReentrantRead { _ in try snapshot.unsafeReentrantRead(counter.value) }, 1)
// Reuse the last connection
try XCTAssertEqual(dbPool.read(counter.value), 2)
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func testSnapshotPool_read_async() async throws {
let dbPool = try makeDatabasePool()
let counter = try Counter(dbPool: dbPool)
try await dbPool.write { try counter.increment($0) }
let snapshot = try dbPool.makeSnapshotPool()
try await dbPool.write { try counter.increment($0) }
do {
let count = try await dbPool.read { try counter.value($0) }
XCTAssertEqual(count, 2)
}
do {
let count = try await snapshot.read { try counter.value($0) }
XCTAssertEqual(count, 1)
}
do {
// Reuse the last connection
let count = try await dbPool.read { try counter.value($0) }
XCTAssertEqual(count, 2)
}
}
func testPassiveCheckpointDoesNotInvalidateSnapshotPool() throws {
let dbPool = try makeDatabasePool()
let counter = try Counter(dbPool: dbPool)