@@ -43,54 +43,52 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
43
43
44
44
#if os(iOS)
45
45
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 ( )
53
53
}
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 )
67
66
}
68
67
}
69
68
70
69
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 ( )
80
79
}
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 )
94
92
}
95
93
}
96
94
0 commit comments