From c4c0b1ce908cdf83d4c0a82fb6bd217848bed8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 14 Nov 2022 19:10:52 +0100 Subject: [PATCH] Test and fix unsafe and async DatabaseSnapshotPool --- GRDB/Core/DatabaseSnapshotPool.swift | 3 +- .../GRDBTests/DatabaseSnapshotPoolTests.swift | 54 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/GRDB/Core/DatabaseSnapshotPool.swift b/GRDB/Core/DatabaseSnapshotPool.swift index 9adeeff77..d49663d8e 100644 --- a/GRDB/Core/DatabaseSnapshotPool.swift +++ b/GRDB/Core/DatabaseSnapshotPool.swift @@ -117,6 +117,7 @@ extension DatabaseSnapshotPool: DatabaseReader { dbPool.interrupt() } + @_disfavoredOverload // SR-15150 Async overloading in protocol implementation fails public func read(_ 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) -> Void) { /// There is no unsafe access to a snapshot. - dbPool.asyncRead(value) + asyncRead(value) } public func unsafeReentrantRead(_ value: (Database) throws -> T) throws -> T { diff --git a/Tests/GRDBTests/DatabaseSnapshotPoolTests.swift b/Tests/GRDBTests/DatabaseSnapshotPoolTests.swift index 7cea62e75..12fc35e20 100644 --- a/Tests/GRDBTests/DatabaseSnapshotPoolTests.swift +++ b/Tests/GRDBTests/DatabaseSnapshotPoolTests.swift @@ -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)