mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
DatabaseGenerator retain the database connection. Tests for various opportunities of memory leaking, and misuse of weak references.
This commit is contained in:
1 parent
1df41fbd29
commit
84e5e488a9
4 files changed
+401
-5
No files matched your search
@@ -320,7 +320,7 @@ public struct DatabaseSequence<T>: SequenceType {
|
||||
// DatabaseSequence can be restarted
|
||||
try statement.reset()
|
||||
|
||||
return DatabaseGenerator {
|
||||
return DatabaseGenerator(database: statement.database) {
|
||||
// Check that generator is used on a valid queue.
|
||||
preconditionValidQueue()
|
||||
|
||||
@@ -344,7 +344,7 @@ public struct DatabaseSequence<T>: SequenceType {
|
||||
return DatabaseSequence() {
|
||||
// Check that generator is built on a valid queue.
|
||||
preconditionValidQueue()
|
||||
return DatabaseGenerator {
|
||||
return DatabaseGenerator(database: database) {
|
||||
// Check that generator is used on a valid queue.
|
||||
preconditionValidQueue()
|
||||
return nil
|
||||
@@ -365,6 +365,7 @@ public struct DatabaseSequence<T>: SequenceType {
|
||||
|
||||
/// A generator of elements fetched from the database.
|
||||
public struct DatabaseGenerator<T>: GeneratorType {
|
||||
private let database: Database // Generators retain the database
|
||||
private let element: () throws -> T?
|
||||
|
||||
@warn_unused_result
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
- [ ] Test that dbPool.releaseMemory() closes all reader connections
|
||||
- [ ] Check that dbQueue/Pool = nil waits for current operations to complete before closing the connection.
|
||||
- [ ] Check that statements retain the database until they are completed.
|
||||
- [ ] Query builder
|
||||
- [ ] SELECT readers.*, books.* FROM ... JOIN ...
|
||||
- [ ] date functions
|
||||
|
||||
@@ -120,4 +120,203 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
||||
XCTAssertEqual(openConnectionCount, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func testBlocksRetainConnection() {
|
||||
assertNoError {
|
||||
let countQueue = dispatch_queue_create(nil, nil)
|
||||
var openConnectionCount = 0
|
||||
var totalOpenConnectionCount = 0
|
||||
|
||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
||||
dispatch_sync(countQueue) {
|
||||
print("open")
|
||||
totalOpenConnectionCount += 1
|
||||
openConnectionCount += 1
|
||||
}
|
||||
}
|
||||
|
||||
dbConfiguration.SQLiteConnectionDidClose = {
|
||||
dispatch_sync(countQueue) {
|
||||
print("close")
|
||||
openConnectionCount -= 1
|
||||
}
|
||||
}
|
||||
|
||||
try self.dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
|
||||
// Block 1 Block 2
|
||||
// read {
|
||||
// >
|
||||
let s1 = dispatch_semaphore_create(0)
|
||||
// dbPool = nil
|
||||
// >
|
||||
let s2 = dispatch_semaphore_create(0)
|
||||
// use database
|
||||
// }
|
||||
|
||||
let (block1, block2) = { () -> (() -> (), () -> ()) in
|
||||
let block1 = { () in
|
||||
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
|
||||
self.dbPool = nil
|
||||
dispatch_semaphore_signal(s2)
|
||||
}
|
||||
let dbPool = self.dbPool
|
||||
let block2 = { [weak dbPool] () in
|
||||
if let dbPool = dbPool {
|
||||
dbPool.read { db in
|
||||
dispatch_semaphore_signal(s1)
|
||||
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items"), 0)
|
||||
}
|
||||
} else {
|
||||
XCTFail("expect non nil dbPool")
|
||||
}
|
||||
}
|
||||
return (block1, block2)
|
||||
}()
|
||||
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
|
||||
let blocks = [block1, block2]
|
||||
dispatch_apply(blocks.count, queue) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
|
||||
// one writer, one reader
|
||||
XCTAssertEqual(totalOpenConnectionCount, 2)
|
||||
|
||||
// All connections are closed
|
||||
XCTAssertEqual(openConnectionCount, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseGeneratorRetainConnection() {
|
||||
assertNoError {
|
||||
let countQueue = dispatch_queue_create(nil, nil)
|
||||
var openConnectionCount = 0
|
||||
var totalOpenConnectionCount = 0
|
||||
|
||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
||||
dispatch_sync(countQueue) {
|
||||
print("open")
|
||||
totalOpenConnectionCount += 1
|
||||
openConnectionCount += 1
|
||||
}
|
||||
}
|
||||
|
||||
dbConfiguration.SQLiteConnectionDidClose = {
|
||||
dispatch_sync(countQueue) {
|
||||
print("close")
|
||||
openConnectionCount -= 1
|
||||
}
|
||||
}
|
||||
|
||||
try self.dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
try self.dbPool.execute("INSERT INTO items (id) VALUES (NULL)")
|
||||
try self.dbPool.execute("INSERT INTO items (id) VALUES (NULL)")
|
||||
|
||||
// Block 1 Block 2
|
||||
// write {
|
||||
// SELECT
|
||||
// step
|
||||
// >
|
||||
let s1 = dispatch_semaphore_create(0)
|
||||
// dbPool = nil
|
||||
// >
|
||||
let s2 = dispatch_semaphore_create(0)
|
||||
// release generator
|
||||
// }
|
||||
|
||||
let (block1, block2) = { () -> (() -> (), () -> ()) in
|
||||
let block1 = { () in
|
||||
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
|
||||
self.dbPool = nil
|
||||
dispatch_semaphore_signal(s2)
|
||||
}
|
||||
let dbPool = self.dbPool
|
||||
let block2 = { [weak dbPool] () in
|
||||
weak var connection: Database? = nil
|
||||
var generator: DatabaseGenerator<Int>? = nil
|
||||
do {
|
||||
if let dbPool = dbPool {
|
||||
dbPool.write { db in
|
||||
connection = db
|
||||
generator = Int.fetch(db, "SELECT id FROM items").generate()
|
||||
XCTAssertTrue(generator!.next() != nil)
|
||||
dispatch_semaphore_signal(s1)
|
||||
}
|
||||
} else {
|
||||
XCTFail("expect non nil dbPool")
|
||||
}
|
||||
}
|
||||
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
|
||||
do {
|
||||
XCTAssertTrue(dbPool == nil)
|
||||
XCTAssertTrue(connection != nil)
|
||||
XCTAssertTrue(generator != nil)
|
||||
generator = nil
|
||||
XCTAssertTrue(connection == nil)
|
||||
}
|
||||
}
|
||||
return (block1, block2)
|
||||
}()
|
||||
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
|
||||
let blocks = [block1, block2]
|
||||
dispatch_apply(blocks.count, queue) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
|
||||
// one writer
|
||||
XCTAssertEqual(totalOpenConnectionCount, 1)
|
||||
|
||||
// All connections are closed
|
||||
XCTAssertEqual(openConnectionCount, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testStatementDoNotRetainDatabaseConnection() {
|
||||
assertNoError {
|
||||
// Block 1 Block 2
|
||||
// create statement INSERT
|
||||
// >
|
||||
let s1 = dispatch_semaphore_create(0)
|
||||
// dbPool = nil
|
||||
// >
|
||||
let s2 = dispatch_semaphore_create(0)
|
||||
// dbPool is nil
|
||||
|
||||
let (block1, block2) = { () -> (() -> (), () -> ()) in
|
||||
let block1 = { () in
|
||||
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
|
||||
self.dbPool = nil
|
||||
dispatch_semaphore_signal(s2)
|
||||
}
|
||||
let dbPool = self.dbPool
|
||||
let block2 = { [weak dbPool] () in
|
||||
var statement: UpdateStatement? = nil
|
||||
do {
|
||||
if let dbPool = dbPool {
|
||||
do {
|
||||
try dbPool.write { db in
|
||||
statement = try db.updateStatement("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
dispatch_semaphore_signal(s1)
|
||||
}
|
||||
} catch {
|
||||
XCTFail("error: \(error)")
|
||||
}
|
||||
} else {
|
||||
XCTFail("expect non nil dbPool")
|
||||
}
|
||||
}
|
||||
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
|
||||
XCTAssertTrue(statement != nil)
|
||||
XCTAssertTrue(dbPool == nil)
|
||||
}
|
||||
return (block1, block2)
|
||||
}()
|
||||
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
|
||||
let blocks = [block1, block2]
|
||||
dispatch_apply(blocks.count, queue) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,4 +37,203 @@ class DatabaseQueueuReleaseMemoryTests: GRDBTestCase {
|
||||
XCTAssertEqual(openConnectionCount, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testBlocksRetainConnection() {
|
||||
assertNoError {
|
||||
let countQueue = dispatch_queue_create(nil, nil)
|
||||
var openConnectionCount = 0
|
||||
var totalOpenConnectionCount = 0
|
||||
|
||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
||||
dispatch_sync(countQueue) {
|
||||
print("open")
|
||||
totalOpenConnectionCount += 1
|
||||
openConnectionCount += 1
|
||||
}
|
||||
}
|
||||
|
||||
dbConfiguration.SQLiteConnectionDidClose = {
|
||||
dispatch_sync(countQueue) {
|
||||
print("close")
|
||||
openConnectionCount -= 1
|
||||
}
|
||||
}
|
||||
|
||||
try self.dbQueue.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
|
||||
// Block 1 Block 2
|
||||
// inDatabase {
|
||||
// >
|
||||
let s1 = dispatch_semaphore_create(0)
|
||||
// dbQueue = nil
|
||||
// >
|
||||
let s2 = dispatch_semaphore_create(0)
|
||||
// use database
|
||||
// }
|
||||
|
||||
let (block1, block2) = { () -> (() -> (), () -> ()) in
|
||||
let block1 = { () in
|
||||
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
|
||||
self.dbQueue = nil
|
||||
dispatch_semaphore_signal(s2)
|
||||
}
|
||||
let dbQueue = self.dbQueue
|
||||
let block2 = { [weak dbQueue] () in
|
||||
if let dbQueue = dbQueue {
|
||||
dbQueue.write { db in
|
||||
dispatch_semaphore_signal(s1)
|
||||
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items"), 0)
|
||||
}
|
||||
} else {
|
||||
XCTFail("expect non nil dbQueue")
|
||||
}
|
||||
}
|
||||
return (block1, block2)
|
||||
}()
|
||||
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
|
||||
let blocks = [block1, block2]
|
||||
dispatch_apply(blocks.count, queue) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
|
||||
// one writer
|
||||
XCTAssertEqual(totalOpenConnectionCount, 1)
|
||||
|
||||
// All connections are closed
|
||||
XCTAssertEqual(openConnectionCount, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseGeneratorRetainConnection() {
|
||||
assertNoError {
|
||||
let countQueue = dispatch_queue_create(nil, nil)
|
||||
var openConnectionCount = 0
|
||||
var totalOpenConnectionCount = 0
|
||||
|
||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
||||
dispatch_sync(countQueue) {
|
||||
print("open")
|
||||
totalOpenConnectionCount += 1
|
||||
openConnectionCount += 1
|
||||
}
|
||||
}
|
||||
|
||||
dbConfiguration.SQLiteConnectionDidClose = {
|
||||
dispatch_sync(countQueue) {
|
||||
print("close")
|
||||
openConnectionCount -= 1
|
||||
}
|
||||
}
|
||||
|
||||
try self.dbQueue.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
try self.dbQueue.execute("INSERT INTO items (id) VALUES (NULL)")
|
||||
try self.dbQueue.execute("INSERT INTO items (id) VALUES (NULL)")
|
||||
|
||||
// Block 1 Block 2
|
||||
// write {
|
||||
// SELECT
|
||||
// step
|
||||
// >
|
||||
let s1 = dispatch_semaphore_create(0)
|
||||
// dbQueue = nil
|
||||
// >
|
||||
let s2 = dispatch_semaphore_create(0)
|
||||
// release generator
|
||||
// }
|
||||
|
||||
let (block1, block2) = { () -> (() -> (), () -> ()) in
|
||||
let block1 = { () in
|
||||
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
|
||||
self.dbQueue = nil
|
||||
dispatch_semaphore_signal(s2)
|
||||
}
|
||||
let dbQueue = self.dbQueue
|
||||
let block2 = { [weak dbQueue] () in
|
||||
weak var connection: Database? = nil
|
||||
var generator: DatabaseGenerator<Int>? = nil
|
||||
do {
|
||||
if let dbQueue = dbQueue {
|
||||
dbQueue.write { db in
|
||||
connection = db
|
||||
generator = Int.fetch(db, "SELECT id FROM items").generate()
|
||||
XCTAssertTrue(generator!.next() != nil)
|
||||
dispatch_semaphore_signal(s1)
|
||||
}
|
||||
} else {
|
||||
XCTFail("expect non nil dbQueue")
|
||||
}
|
||||
}
|
||||
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
|
||||
do {
|
||||
XCTAssertTrue(dbQueue == nil)
|
||||
XCTAssertTrue(connection != nil)
|
||||
XCTAssertTrue(generator != nil)
|
||||
generator = nil
|
||||
XCTAssertTrue(connection == nil)
|
||||
}
|
||||
}
|
||||
return (block1, block2)
|
||||
}()
|
||||
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
|
||||
let blocks = [block1, block2]
|
||||
dispatch_apply(blocks.count, queue) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
|
||||
// one writer
|
||||
XCTAssertEqual(totalOpenConnectionCount, 1)
|
||||
|
||||
// All connections are closed
|
||||
XCTAssertEqual(openConnectionCount, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testStatementDoNotRetainDatabaseConnection() {
|
||||
assertNoError {
|
||||
// Block 1 Block 2
|
||||
// create statement INSERT
|
||||
// >
|
||||
let s1 = dispatch_semaphore_create(0)
|
||||
// dbQueue = nil
|
||||
// >
|
||||
let s2 = dispatch_semaphore_create(0)
|
||||
// dbQueue is nil
|
||||
|
||||
let (block1, block2) = { () -> (() -> (), () -> ()) in
|
||||
let block1 = { () in
|
||||
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
|
||||
self.dbQueue = nil
|
||||
dispatch_semaphore_signal(s2)
|
||||
}
|
||||
let dbQueue = self.dbQueue
|
||||
let block2 = { [weak dbQueue] () in
|
||||
var statement: UpdateStatement? = nil
|
||||
do {
|
||||
if let dbQueue = dbQueue {
|
||||
do {
|
||||
try dbQueue.write { db in
|
||||
statement = try db.updateStatement("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
dispatch_semaphore_signal(s1)
|
||||
}
|
||||
} catch {
|
||||
XCTFail("error: \(error)")
|
||||
}
|
||||
} else {
|
||||
XCTFail("expect non nil dbQueue")
|
||||
}
|
||||
}
|
||||
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
|
||||
XCTAssertTrue(statement != nil)
|
||||
XCTAssertTrue(dbQueue == nil)
|
||||
}
|
||||
return (block1, block2)
|
||||
}()
|
||||
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
|
||||
let blocks = [block1, block2]
|
||||
dispatch_apply(blocks.count, queue) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user