diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 7d40c9b3e..5ced11e8d 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -320,7 +320,7 @@ public struct DatabaseSequence: 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: 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: SequenceType { /// A generator of elements fetched from the database. public struct DatabaseGenerator: GeneratorType { + private let database: Database // Generators retain the database private let element: () throws -> T? @warn_unused_result diff --git a/TODO.md b/TODO.md index e3a7661c2..8a50e4995 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/Tests/Private/DatabasePoolReleaseMemoryTests.swift b/Tests/Private/DatabasePoolReleaseMemoryTests.swift index 553672ca1..94846e3f2 100644 --- a/Tests/Private/DatabasePoolReleaseMemoryTests.swift +++ b/Tests/Private/DatabasePoolReleaseMemoryTests.swift @@ -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? = 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]() + } + } + } } diff --git a/Tests/Private/DatabaseQueueReleaseMemoryTests.swift b/Tests/Private/DatabaseQueueReleaseMemoryTests.swift index f54d7f634..c70e6b35f 100644 --- a/Tests/Private/DatabaseQueueReleaseMemoryTests.swift +++ b/Tests/Private/DatabaseQueueReleaseMemoryTests.swift @@ -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? = 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]() + } + } + } }