mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Tests for DatabaseQueue/Pool connection closing
This commit is contained in:
1 parent
b5d95fda22
commit
1df41fbd29
7 files changed
+189
-8
No files matched your search
@@ -111,6 +111,10 @@
|
||||
563363C41C942C37000BE133 /* DatabaseWriter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363C31C942C37000BE133 /* DatabaseWriter.swift */; };
|
||||
563363C51C942C37000BE133 /* DatabaseWriter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363C31C942C37000BE133 /* DatabaseWriter.swift */; };
|
||||
563363C61C942C37000BE133 /* DatabaseWriter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363C31C942C37000BE133 /* DatabaseWriter.swift */; };
|
||||
563363D01C943D13000BE133 /* DatabasePoolReleaseMemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363CF1C943D13000BE133 /* DatabasePoolReleaseMemoryTests.swift */; };
|
||||
563363D11C943D13000BE133 /* DatabasePoolReleaseMemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363CF1C943D13000BE133 /* DatabasePoolReleaseMemoryTests.swift */; };
|
||||
563363D51C94484E000BE133 /* DatabaseQueueReleaseMemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363D41C94484E000BE133 /* DatabaseQueueReleaseMemoryTests.swift */; };
|
||||
563363D61C94484E000BE133 /* DatabaseQueueReleaseMemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363D41C94484E000BE133 /* DatabaseQueueReleaseMemoryTests.swift */; };
|
||||
563445051C4A7FD7003D3DC6 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563445041C4A7FD7003D3DC6 /* Utils.swift */; };
|
||||
563445061C4A7FD7003D3DC6 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563445041C4A7FD7003D3DC6 /* Utils.swift */; };
|
||||
564A50C71BFF4B7F00B3A3A2 /* CollationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A50C61BFF4B7F00B3A3A2 /* CollationTests.swift */; };
|
||||
@@ -389,6 +393,8 @@
|
||||
563363BC1C93FD5E000BE133 /* DatabaseQueueConcurrencyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueConcurrencyTests.swift; sourceTree = "<group>"; };
|
||||
563363BF1C942C04000BE133 /* DatabaseReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseReader.swift; sourceTree = "<group>"; };
|
||||
563363C31C942C37000BE133 /* DatabaseWriter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseWriter.swift; sourceTree = "<group>"; };
|
||||
563363CF1C943D13000BE133 /* DatabasePoolReleaseMemoryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePoolReleaseMemoryTests.swift; sourceTree = "<group>"; };
|
||||
563363D41C94484E000BE133 /* DatabaseQueueReleaseMemoryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueReleaseMemoryTests.swift; sourceTree = "<group>"; };
|
||||
563445041C4A7FD7003D3DC6 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
|
||||
564A50C61BFF4B7F00B3A3A2 /* CollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollationTests.swift; sourceTree = "<group>"; };
|
||||
56553C181C3E906C00522B5C /* GRDBOSXCrashTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBOSXCrashTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
@@ -714,8 +720,10 @@
|
||||
569978D31B539038005EBEED /* Private */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */,
|
||||
563363CF1C943D13000BE133 /* DatabasePoolReleaseMemoryTests.swift */,
|
||||
569531281C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift */,
|
||||
563363D41C94484E000BE133 /* DatabaseQueueReleaseMemoryTests.swift */,
|
||||
569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */,
|
||||
56EB0AB11BCD787300A3DC55 /* NSDataMemoryTests.swift */,
|
||||
5605F1861C69111300235C62 /* StatementInformationTests.swift */,
|
||||
);
|
||||
@@ -1495,6 +1503,8 @@
|
||||
56A2385C1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
56A238401B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
|
||||
56A2383C1B9C74A90082EB20 /* DatabaseErrorTests.swift in Sources */,
|
||||
563363D61C94484E000BE133 /* DatabaseQueueReleaseMemoryTests.swift in Sources */,
|
||||
563363D11C943D13000BE133 /* DatabasePoolReleaseMemoryTests.swift in Sources */,
|
||||
56A238521B9C74A90082EB20 /* MinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
56CA21BB1BB3FCF7009A04C5 /* NSDataTests.swift in Sources */,
|
||||
56A238B71B9CA2590082EB20 /* DatabaseTimestampTests.swift in Sources */,
|
||||
@@ -1557,6 +1567,7 @@
|
||||
56A238691B9C74A90082EB20 /* RecordSubClassTests.swift in Sources */,
|
||||
560A37AB1C90085D00949E71 /* DatabasePoolConcurrencyTests.swift in Sources */,
|
||||
5607EFD31BB8254800605DE3 /* TransactionObserverTests.swift in Sources */,
|
||||
563363D01C943D13000BE133 /* DatabasePoolReleaseMemoryTests.swift in Sources */,
|
||||
56A2383D1B9C74A90082EB20 /* DatabaseValueTests.swift in Sources */,
|
||||
563363B91C933FF8000BE133 /* DatabaseQueuePersistableTests.swift in Sources */,
|
||||
565D5D711BBC694D00DC9BD4 /* Row+FoundationTests.swift in Sources */,
|
||||
@@ -1565,6 +1576,7 @@
|
||||
56A238611B9C74A90082EB20 /* RecordEditedTests.swift in Sources */,
|
||||
56A2385B1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
565B0FEF1BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */,
|
||||
563363D51C94484E000BE133 /* DatabaseQueueReleaseMemoryTests.swift in Sources */,
|
||||
56A2383F1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
|
||||
569531291C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift in Sources */,
|
||||
56E8CE0D1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */,
|
||||
|
||||
@@ -21,7 +21,13 @@ public struct Configuration {
|
||||
// MARK: - Concurrency
|
||||
|
||||
public var busyMode: BusyMode = .ImmediateError
|
||||
|
||||
|
||||
// MARK: - Internal
|
||||
|
||||
var threadingMode: ThreadingMode = .Default
|
||||
var SQLiteConnectionDidOpen: (() -> ())?
|
||||
var SQLiteConnectionDidClose: (() -> ())?
|
||||
|
||||
|
||||
// MARK: - Factory Configuration
|
||||
|
||||
@@ -58,6 +58,8 @@ public final class Database {
|
||||
throw DatabaseError(code: code, message: String.fromCString(sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
|
||||
configuration.SQLiteConnectionDidOpen?()
|
||||
|
||||
// Setup trace first, so that all queries, including initialization queries, are traced.
|
||||
setupTrace()
|
||||
|
||||
@@ -66,9 +68,8 @@ public final class Database {
|
||||
}
|
||||
|
||||
deinit {
|
||||
if sqliteConnection != nil {
|
||||
sqlite3_close(sqliteConnection)
|
||||
}
|
||||
configuration.SQLiteConnectionDidClose?()
|
||||
sqlite3_close(sqliteConnection)
|
||||
}
|
||||
|
||||
func releaseMemory() {
|
||||
|
||||
@@ -56,9 +56,7 @@ public class Statement {
|
||||
}
|
||||
|
||||
deinit {
|
||||
if sqliteStatement != nil {
|
||||
sqlite3_finalize(sqliteStatement)
|
||||
}
|
||||
sqlite3_finalize(sqliteStatement)
|
||||
}
|
||||
|
||||
final func reset() throws {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
- [ ] Test that dbQueue/Pool.releaseMemory() blocks the current thread until all database accesses are completed.
|
||||
- [ ] 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
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
import XCTest
|
||||
@testable import GRDB
|
||||
|
||||
class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
||||
|
||||
func testDatabasePoolDeinitClosesAllConnections() {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// write & read
|
||||
|
||||
try dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
for _ in 0..<2 {
|
||||
try dbPool.execute("INSERT INTO items (id) VALUES (NULL)")
|
||||
}
|
||||
_ = Int.fetchOne(dbPool, "SELECT COUNT(*) FROM items")
|
||||
|
||||
// Release
|
||||
dbPool = nil
|
||||
|
||||
// One reader, one writer
|
||||
XCTAssertEqual(totalOpenConnectionCount, 2)
|
||||
|
||||
// All connections are closed
|
||||
XCTAssertEqual(openConnectionCount, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabasePoolReleaseMemoryClosesReaderConnections() {
|
||||
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 dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
for _ in 0..<2 {
|
||||
try dbPool.execute("INSERT INTO items (id) VALUES (NULL)")
|
||||
}
|
||||
|
||||
// Block 1 Block 2 Block3
|
||||
// SELECT * FROM items
|
||||
// step
|
||||
// >
|
||||
let s1 = dispatch_semaphore_create(0)
|
||||
// SELECT * FROM items
|
||||
// step
|
||||
// >
|
||||
let s2 = dispatch_semaphore_create(0)
|
||||
// step step
|
||||
// >
|
||||
let s3 = dispatch_semaphore_create(0)
|
||||
// end end releaseMemory
|
||||
|
||||
let block1 = { () in
|
||||
self.dbPool.read { db in
|
||||
let generator = Row.fetch(db, "SELECT * FROM items").generate()
|
||||
XCTAssertTrue(generator.next() != nil)
|
||||
dispatch_semaphore_signal(s1)
|
||||
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
|
||||
XCTAssertTrue(generator.next() != nil)
|
||||
dispatch_semaphore_signal(s3)
|
||||
XCTAssertTrue(generator.next() == nil)
|
||||
}
|
||||
}
|
||||
let block2 = { () in
|
||||
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
|
||||
self.dbPool.read { db in
|
||||
let generator = Row.fetch(db, "SELECT * FROM items").generate()
|
||||
XCTAssertTrue(generator.next() != nil)
|
||||
dispatch_semaphore_signal(s2)
|
||||
XCTAssertTrue(generator.next() != nil)
|
||||
XCTAssertTrue(generator.next() == nil)
|
||||
}
|
||||
}
|
||||
let block3 = { () in
|
||||
dispatch_semaphore_wait(s3, DISPATCH_TIME_FOREVER)
|
||||
self.dbPool.releaseMemory()
|
||||
}
|
||||
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
|
||||
dispatch_apply(3, queue) { index in
|
||||
[block1, block2, block3][index]()
|
||||
}
|
||||
|
||||
// Two readers, one writer
|
||||
XCTAssertEqual(totalOpenConnectionCount, 3)
|
||||
|
||||
// Writer is still open
|
||||
XCTAssertEqual(openConnectionCount, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import XCTest
|
||||
@testable import GRDB
|
||||
|
||||
class DatabaseQueueuReleaseMemoryTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueuDeinitClosesConnection() {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// Open connection
|
||||
try dbQueue.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
|
||||
// Release
|
||||
dbQueue = nil
|
||||
|
||||
// One reader, one writer
|
||||
XCTAssertEqual(totalOpenConnectionCount, 1)
|
||||
|
||||
// All connections are closed
|
||||
XCTAssertEqual(openConnectionCount, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user