diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index d620215b1..3ce475757 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -187,7 +187,9 @@ extension DatabaseQueue { /// /// - parameter block: A block that accesses the database. public func asyncRead(_ block: @escaping (Result) -> Void) { - writer.async { block(.success($0)) } + writer.async { db in + db.readOnly { block(.success(db)) } + } } #endif diff --git a/Tests/GRDBTests/DatabaseReaderTests.swift b/Tests/GRDBTests/DatabaseReaderTests.swift index 7aac58fa6..6d2259949 100644 --- a/Tests/GRDBTests/DatabaseReaderTests.swift +++ b/Tests/GRDBTests/DatabaseReaderTests.swift @@ -72,4 +72,56 @@ class DatabaseReaderTests : GRDBTestCase { let reader: DatabaseReader = DatabaseQueue() let _: DatabaseReader = AnyDatabaseReader(reader) } + + func testAsyncRead() throws { + func test(_ dbReader: DatabaseReader) throws { + let expectation = self.expectation(description: "updates") + let semaphore = DispatchSemaphore(value: 0) + var count: Int? + dbReader.asyncRead { db in + // Make sure this block executes asynchronously + semaphore.wait() + do { + count = try Int.fetchOne(db.get(), sql: "SELECT COUNT(*) FROM sqlite_master") + } catch { + XCTFail("Unexpected error: \(error)") + } + expectation.fulfill() + } + semaphore.signal() + + waitForExpectations(timeout: 1, handler: nil) + XCTAssertNotNil(count) + } + + try test(makeDatabaseQueue()) + try test(makeDatabasePool()) + try test(makeDatabasePool().makeSnapshot()) + } + + func testAsyncReadPreventsDatabaseModification() throws { + func test(_ dbReader: DatabaseReader) throws { + let expectation = self.expectation(description: "updates") + let semaphore = DispatchSemaphore(value: 0) + dbReader.asyncRead { db in + // Make sure this block executes asynchronously + semaphore.wait() + do { + try db.get().execute(sql: "CREATE TABLE testAsyncReadPreventsDatabaseModification (a)") + XCTFail("Expected error") + } catch let error as DatabaseError { + XCTAssertEqual(error.resultCode, .SQLITE_READONLY) + } catch { + XCTFail("Unexpected error: \(error)") + } + expectation.fulfill() + } + semaphore.signal() + waitForExpectations(timeout: 1, handler: nil) + } + + try test(makeDatabaseQueue()) + try test(makeDatabasePool()) + try test(makeDatabasePool().makeSnapshot()) + } } diff --git a/Tests/GRDBTests/DatabaseWriterTests.swift b/Tests/GRDBTests/DatabaseWriterTests.swift index 6357e6e08..0e29153fe 100644 --- a/Tests/GRDBTests/DatabaseWriterTests.swift +++ b/Tests/GRDBTests/DatabaseWriterTests.swift @@ -43,6 +43,105 @@ class DatabaseWriterTests : GRDBTestCase { } } + func testAsyncWriteWithoutTransactionSuccess() throws { + func test(_ dbWriter: DatabaseWriter) throws { + let expectation = self.expectation(description: "updates") + let semaphore = DispatchSemaphore(value: 0) + dbWriter.asyncWriteWithoutTransaction { db in + // Make sure this block executes asynchronously + semaphore.wait() + do { + try db.execute(sql: "CREATE TABLE testAsyncWriteWithoutTransaction (a)") + } catch { + XCTFail("Unexpected error: \(error)") + } + expectation.fulfill() + } + semaphore.signal() + + waitForExpectations(timeout: 1, handler: nil) + let tableExists = try dbWriter.read { try $0.tableExists("testAsyncWriteWithoutTransaction") } + XCTAssertTrue(tableExists) + } + + try test(makeDatabaseQueue()) + try test(makeDatabasePool()) + } + + func testAsyncWriteWithoutTransactionError() throws { + func test(_ dbWriter: DatabaseWriter) throws { + let expectation = self.expectation(description: "updates") + let semaphore = DispatchSemaphore(value: 0) + dbWriter.asyncWriteWithoutTransaction { db in + // Make sure this block executes asynchronously + semaphore.wait() + do { + try db.execute(sql: "This is not SQL") + XCTFail("Expected error") + } catch let error as DatabaseError { + XCTAssertEqual(error.sql, "This is not SQL") + } catch { + XCTFail("Unexpected error: \(error)") + } + expectation.fulfill() + } + semaphore.signal() + waitForExpectations(timeout: 1, handler: nil) + } + + try test(makeDatabaseQueue()) + try test(makeDatabasePool()) + } + + func testAsyncWriteSuccess() throws { + func test(_ dbWriter: DatabaseWriter) throws { + let expectation = self.expectation(description: "updates") + let semaphore = DispatchSemaphore(value: 0) + dbWriter.asyncWrite({ db in + // Make sure this block executes asynchronously + semaphore.wait() + try db.execute(sql: "CREATE TABLE testAsyncWrite (a)") + }, completion: { error in + if let error = error { + XCTFail("Unexpected error: \(error)") + } + expectation.fulfill() + }) + semaphore.signal() + + waitForExpectations(timeout: 1, handler: nil) + let tableExists = try dbWriter.read { try $0.tableExists("testAsyncWrite") } + XCTAssertTrue(tableExists) + } + + try test(makeDatabaseQueue()) + try test(makeDatabasePool()) + } + + func testAsyncWriteError() throws { + func test(_ dbWriter: DatabaseWriter) throws { + let expectation = self.expectation(description: "updates") + let semaphore = DispatchSemaphore(value: 0) + dbWriter.asyncWrite({ db in + // Make sure this block executes asynchronously + semaphore.wait() + try db.execute(sql: "This is not SQL") + }, completion: { error in + guard let error = error as? DatabaseError else { + XCTFail("Unexpected error") + return + } + XCTAssertEqual(error.sql, "This is not SQL") + expectation.fulfill() + }) + semaphore.signal() + waitForExpectations(timeout: 1, handler: nil) + } + + try test(makeDatabaseQueue()) + try test(makeDatabasePool()) + } + func testAnyDatabaseWriter() { // This test passes if this code compiles. let writer: DatabaseWriter = DatabaseQueue()