From 07eaeee1239d389192d558210345ee165f7a0061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 9 Jul 2022 13:37:55 +0200 Subject: [PATCH] automaticMemoryManagement tests check for actual connection closing --- GRDB/Core/DatabasePool.swift | 18 ---- .../DatabasePoolReleaseMemoryTests.swift | 82 +++++++++---------- 2 files changed, 40 insertions(+), 60 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 018cf2699..cad7014dd 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -156,24 +156,6 @@ public final class DatabasePool: DatabaseWriter { return configuration } - - /// Blocks the current thread until all database connections have - /// executed the *body* block. - fileprivate func forEachConnection(_ body: (Database) -> Void) { - writer.sync(body) - readerPool?.forEach { $0.sync(body) } - } - - var numberOfReaders: Int { - guard let readerPool = readerPool else { - return 0 - } - var count = 0 - readerPool.forEach { _ in - count += 1 - } - return count - } } #if swift(>=5.6) && canImport(_Concurrency) diff --git a/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift b/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift index 0b988c147..058e450af 100644 --- a/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift +++ b/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift @@ -43,54 +43,52 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { #if os(iOS) func testDatabasePoolReleasesMemoryOnPressureEvent() throws { - do { - // Create a database pool. - let dbPool = try makeDatabasePool() - - // Write and read it to ensure readers exist. - try dbPool.write { db in - try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)") + // Create a database pool, and expect a reader connection to be closed + let expectation = self.expectation(description: "Reader connection closed") + + var configuration = Configuration() + configuration.SQLiteConnectionWillClose = { conn in + if sqlite3_db_readonly(conn, nil) != 0 { + expectation.fulfill() } - - // Precondition: there are readers. - try dbPool.read { _ in } - XCTAssertNotEqual(0, dbPool.numberOfReaders) - - // Simulate memory warning. - NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UIApplicationDidReceiveMemoryWarningNotification"), - object: nil) - // Block until memory is released, which happens on a global queue. - dbPool.barrierWriteWithoutTransaction { _ in } - - // Postcondition: readers removed. - XCTAssertEqual(0, dbPool.numberOfReaders) + } + let dbPool = try makeDatabasePool(configuration: configuration) + + // Precondition: there is one reader. + try dbPool.read { _ in } + + // Simulate memory warning. + NotificationCenter.default.post(name: UIApplication.didReceiveMemoryWarningNotification, object: nil) + + // Postcondition: reader connection was closed + withExtendedLifetime(dbPool) { _ in + waitForExpectations(timeout: 0.5) } } func testDatabasePoolDoesNotReleaseMemoryOnPressureEventIfDisabled() throws { - do { - // Create a database pool without automatic memory management. - var configuration = dbConfiguration! - configuration.automaticMemoryManagement = false - let dbPool = try makeDatabasePool(configuration: configuration) - - // Write and read it to ensure readers exist. - try dbPool.write { db in - try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)") + // Create a database pool, and do not expect any reader connection to be closed + let expectation = self.expectation(description: "Reader connection closed") + expectation.isInverted = true + + var configuration = Configuration() + configuration.automaticMemoryManagement = false + configuration.SQLiteConnectionWillClose = { conn in + if sqlite3_db_readonly(conn, nil) != 0 { + expectation.fulfill() } - - // Precondition: there are readers. - try dbPool.read { _ in } - XCTAssertNotEqual(0, dbPool.numberOfReaders) - - // Simulate memory warning. - NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UIApplicationDidReceiveMemoryWarningNotification"), - object: nil) - // Block until memory would be released, which would happen on a global queue. - dbPool.barrierWriteWithoutTransaction { _ in } - - // Postcondition: there are readers. - XCTAssertNotEqual(0, dbPool.numberOfReaders) + } + let dbPool = try makeDatabasePool(configuration: configuration) + + // Precondition: there is one reader. + try dbPool.read { _ in } + + // Simulate memory warning. + NotificationCenter.default.post(name: UIApplication.didReceiveMemoryWarningNotification, object: nil) + + // Postcondition: no reader connection was closed + withExtendedLifetime(dbPool) { _ in + waitForExpectations(timeout: 0.5) } }