Skip to content

Commit 07eaeee

Browse files
committed
automaticMemoryManagement tests check for actual connection closing
1 parent 0bfb4de commit 07eaeee

File tree

2 files changed

+40
-60
lines changed

2 files changed

+40
-60
lines changed

GRDB/Core/DatabasePool.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,6 @@ public final class DatabasePool: DatabaseWriter {
156156

157157
return configuration
158158
}
159-
160-
/// Blocks the current thread until all database connections have
161-
/// executed the *body* block.
162-
fileprivate func forEachConnection(_ body: (Database) -> Void) {
163-
writer.sync(body)
164-
readerPool?.forEach { $0.sync(body) }
165-
}
166-
167-
var numberOfReaders: Int {
168-
guard let readerPool = readerPool else {
169-
return 0
170-
}
171-
var count = 0
172-
readerPool.forEach { _ in
173-
count += 1
174-
}
175-
return count
176-
}
177159
}
178160

179161
#if swift(>=5.6) && canImport(_Concurrency)

Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,54 +43,52 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
4343

4444
#if os(iOS)
4545
func testDatabasePoolReleasesMemoryOnPressureEvent() throws {
46-
do {
47-
// Create a database pool.
48-
let dbPool = try makeDatabasePool()
49-
50-
// Write and read it to ensure readers exist.
51-
try dbPool.write { db in
52-
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
46+
// Create a database pool, and expect a reader connection to be closed
47+
let expectation = self.expectation(description: "Reader connection closed")
48+
49+
var configuration = Configuration()
50+
configuration.SQLiteConnectionWillClose = { conn in
51+
if sqlite3_db_readonly(conn, nil) != 0 {
52+
expectation.fulfill()
5353
}
54-
55-
// Precondition: there are readers.
56-
try dbPool.read { _ in }
57-
XCTAssertNotEqual(0, dbPool.numberOfReaders)
58-
59-
// Simulate memory warning.
60-
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UIApplicationDidReceiveMemoryWarningNotification"),
61-
object: nil)
62-
// Block until memory is released, which happens on a global queue.
63-
dbPool.barrierWriteWithoutTransaction { _ in }
64-
65-
// Postcondition: readers removed.
66-
XCTAssertEqual(0, dbPool.numberOfReaders)
54+
}
55+
let dbPool = try makeDatabasePool(configuration: configuration)
56+
57+
// Precondition: there is one reader.
58+
try dbPool.read { _ in }
59+
60+
// Simulate memory warning.
61+
NotificationCenter.default.post(name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
62+
63+
// Postcondition: reader connection was closed
64+
withExtendedLifetime(dbPool) { _ in
65+
waitForExpectations(timeout: 0.5)
6766
}
6867
}
6968

7069
func testDatabasePoolDoesNotReleaseMemoryOnPressureEventIfDisabled() throws {
71-
do {
72-
// Create a database pool without automatic memory management.
73-
var configuration = dbConfiguration!
74-
configuration.automaticMemoryManagement = false
75-
let dbPool = try makeDatabasePool(configuration: configuration)
76-
77-
// Write and read it to ensure readers exist.
78-
try dbPool.write { db in
79-
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
70+
// Create a database pool, and do not expect any reader connection to be closed
71+
let expectation = self.expectation(description: "Reader connection closed")
72+
expectation.isInverted = true
73+
74+
var configuration = Configuration()
75+
configuration.automaticMemoryManagement = false
76+
configuration.SQLiteConnectionWillClose = { conn in
77+
if sqlite3_db_readonly(conn, nil) != 0 {
78+
expectation.fulfill()
8079
}
81-
82-
// Precondition: there are readers.
83-
try dbPool.read { _ in }
84-
XCTAssertNotEqual(0, dbPool.numberOfReaders)
85-
86-
// Simulate memory warning.
87-
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UIApplicationDidReceiveMemoryWarningNotification"),
88-
object: nil)
89-
// Block until memory would be released, which would happen on a global queue.
90-
dbPool.barrierWriteWithoutTransaction { _ in }
91-
92-
// Postcondition: there are readers.
93-
XCTAssertNotEqual(0, dbPool.numberOfReaders)
80+
}
81+
let dbPool = try makeDatabasePool(configuration: configuration)
82+
83+
// Precondition: there is one reader.
84+
try dbPool.read { _ in }
85+
86+
// Simulate memory warning.
87+
NotificationCenter.default.post(name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
88+
89+
// Postcondition: no reader connection was closed
90+
withExtendedLifetime(dbPool) { _ in
91+
waitForExpectations(timeout: 0.5)
9492
}
9593
}
9694

0 commit comments

Comments
 (0)