mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Configuration is Sendable
This commit is contained in:
1 parent
f7dc72a5f3
commit
54ffb21fb8
9 files changed
+123
-131
No files matched your search
@@ -10,7 +10,8 @@ import SQLite3
|
|||||||
import Dispatch
|
import Dispatch
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public struct Configuration {
|
// TODO: remove @unchecked when DispatchQoS is Sendable
|
||||||
|
public struct Configuration: @unchecked Sendable {
|
||||||
|
|
||||||
// MARK: - Misc options
|
// MARK: - Misc options
|
||||||
|
|
||||||
@@ -195,7 +196,7 @@ public struct Configuration {
|
|||||||
|
|
||||||
// MARK: - Managing SQLite Connections
|
// MARK: - Managing SQLite Connections
|
||||||
|
|
||||||
private var setups: [(Database) throws -> Void] = []
|
private var setups: [@Sendable (Database) throws -> Void] = []
|
||||||
|
|
||||||
/// Defines a function to run whenever an SQLite connection is opened.
|
/// Defines a function to run whenever an SQLite connection is opened.
|
||||||
///
|
///
|
||||||
@@ -232,7 +233,7 @@ public struct Configuration {
|
|||||||
///
|
///
|
||||||
/// On newly created databases files, ``DatabasePool`` activates the WAL
|
/// On newly created databases files, ``DatabasePool`` activates the WAL
|
||||||
/// mode after the preparation functions have run.
|
/// mode after the preparation functions have run.
|
||||||
public mutating func prepareDatabase(_ setup: @escaping (Database) throws -> Void) {
|
public mutating func prepareDatabase(_ setup: @escaping @Sendable (Database) throws -> Void) {
|
||||||
setups.append(setup)
|
setups.append(setup)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -432,9 +433,25 @@ public struct Configuration {
|
|||||||
/// through a `SerializedDatabase`.
|
/// through a `SerializedDatabase`.
|
||||||
var threadingMode = Database.ThreadingMode.default
|
var threadingMode = Database.ThreadingMode.default
|
||||||
|
|
||||||
var SQLiteConnectionDidOpen: (() -> Void)?
|
private(set) var SQLiteConnectionDidOpen: (@Sendable () -> Void)?
|
||||||
var SQLiteConnectionWillClose: ((SQLiteConnection) -> Void)?
|
private(set) var SQLiteConnectionWillClose: (@Sendable (SQLiteConnection) -> Void)?
|
||||||
var SQLiteConnectionDidClose: (() -> Void)?
|
private(set) var SQLiteConnectionDidClose: (@Sendable () -> Void)?
|
||||||
|
|
||||||
|
// Workaround https://github.com/apple/swift/issues/72727
|
||||||
|
mutating func onConnectionDidOpen(_ callback: @escaping @Sendable () -> Void) {
|
||||||
|
SQLiteConnectionDidOpen = callback
|
||||||
|
}
|
||||||
|
|
||||||
|
// Workaround https://github.com/apple/swift/issues/72727
|
||||||
|
mutating func onConnectionWillClose(_ callback: @escaping @Sendable (SQLiteConnection) -> Void) {
|
||||||
|
SQLiteConnectionWillClose = callback
|
||||||
|
}
|
||||||
|
|
||||||
|
// Workaround https://github.com/apple/swift/issues/72727
|
||||||
|
mutating func onConnectionDidClose(_ callback: @escaping @Sendable () -> Void) {
|
||||||
|
SQLiteConnectionDidClose = callback
|
||||||
|
}
|
||||||
|
|
||||||
var SQLiteOpenFlags: CInt {
|
var SQLiteOpenFlags: CInt {
|
||||||
var flags = readonly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE)
|
var flags = readonly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE)
|
||||||
if sqlite3_libversion_number() >= 3037000 {
|
if sqlite3_libversion_number() >= 3037000 {
|
||||||
|
|||||||
@@ -109,7 +109,7 @@
|
|||||||
- [ ] belongstoassociation
|
- [ ] belongstoassociation
|
||||||
- [ ] columnexpression
|
- [ ] columnexpression
|
||||||
- [ ] commontableexpression
|
- [ ] commontableexpression
|
||||||
- [ ] configuration
|
- [X] configuration
|
||||||
- [X] customtransactionclock
|
- [X] customtransactionclock
|
||||||
- [X] database/busymode
|
- [X] database/busymode
|
||||||
- [ ] databaseaggregate
|
- [ ] databaseaggregate
|
||||||
|
|||||||
@@ -15,40 +15,40 @@ class DatabaseConfigurationTests: GRDBTestCase {
|
|||||||
|
|
||||||
func testPrepareDatabase() throws {
|
func testPrepareDatabase() throws {
|
||||||
// prepareDatabase is called when connection opens
|
// prepareDatabase is called when connection opens
|
||||||
var connectionCount = 0
|
let connectionCountMutex = Mutex(0)
|
||||||
var configuration = Configuration()
|
var configuration = Configuration()
|
||||||
configuration.prepareDatabase { db in
|
configuration.prepareDatabase { db in
|
||||||
connectionCount += 1
|
connectionCountMutex.increment()
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = try DatabaseQueue(configuration: configuration)
|
_ = try DatabaseQueue(configuration: configuration)
|
||||||
XCTAssertEqual(connectionCount, 1)
|
XCTAssertEqual(connectionCountMutex.value, 1)
|
||||||
|
|
||||||
_ = try makeDatabaseQueue(configuration: configuration)
|
_ = try makeDatabaseQueue(configuration: configuration)
|
||||||
XCTAssertEqual(connectionCount, 2)
|
XCTAssertEqual(connectionCountMutex.value, 2)
|
||||||
|
|
||||||
let pool = try makeDatabasePool(configuration: configuration)
|
let pool = try makeDatabasePool(configuration: configuration)
|
||||||
XCTAssertEqual(connectionCount, 3)
|
XCTAssertEqual(connectionCountMutex.value, 3)
|
||||||
|
|
||||||
try pool.read { _ in }
|
try pool.read { _ in }
|
||||||
XCTAssertEqual(connectionCount, 4)
|
XCTAssertEqual(connectionCountMutex.value, 4)
|
||||||
|
|
||||||
try pool.makeSnapshot().read { _ in }
|
try pool.makeSnapshot().read { _ in }
|
||||||
XCTAssertEqual(connectionCount, 5)
|
XCTAssertEqual(connectionCountMutex.value, 5)
|
||||||
|
|
||||||
#if SQLITE_ENABLE_SNAPSHOT || (!GRDBCUSTOMSQLITE && !GRDBCIPHER)
|
#if SQLITE_ENABLE_SNAPSHOT || (!GRDBCUSTOMSQLITE && !GRDBCIPHER)
|
||||||
try pool.makeSnapshotPool().read { _ in }
|
try pool.makeSnapshotPool().read { _ in }
|
||||||
XCTAssertEqual(connectionCount, 6)
|
XCTAssertEqual(connectionCountMutex.value, 6)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPrepareDatabaseError() throws {
|
func testPrepareDatabaseError() throws {
|
||||||
struct TestError: Error { }
|
struct TestError: Error { }
|
||||||
var error: TestError?
|
let errorMutex: Mutex<TestError?> = Mutex(nil)
|
||||||
|
|
||||||
var configuration = Configuration()
|
var configuration = Configuration()
|
||||||
configuration.prepareDatabase { db in
|
configuration.prepareDatabase { db in
|
||||||
if let error {
|
if let error = errorMutex.value {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,36 +56,36 @@ class DatabaseConfigurationTests: GRDBTestCase {
|
|||||||
// TODO: what about in-memory DatabaseQueue???
|
// TODO: what about in-memory DatabaseQueue???
|
||||||
|
|
||||||
do {
|
do {
|
||||||
error = TestError()
|
errorMutex.value = TestError()
|
||||||
_ = try makeDatabaseQueue(configuration: configuration)
|
_ = try makeDatabaseQueue(configuration: configuration)
|
||||||
XCTFail("Expected TestError")
|
XCTFail("Expected TestError")
|
||||||
} catch is TestError { }
|
} catch is TestError { }
|
||||||
|
|
||||||
do {
|
do {
|
||||||
error = TestError()
|
errorMutex.value = TestError()
|
||||||
_ = try makeDatabasePool(configuration: configuration)
|
_ = try makeDatabasePool(configuration: configuration)
|
||||||
XCTFail("Expected TestError")
|
XCTFail("Expected TestError")
|
||||||
} catch is TestError { }
|
} catch is TestError { }
|
||||||
|
|
||||||
do {
|
do {
|
||||||
error = nil
|
errorMutex.value = nil
|
||||||
let pool = try makeDatabasePool(configuration: configuration)
|
let pool = try makeDatabasePool(configuration: configuration)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
error = TestError()
|
errorMutex.value = TestError()
|
||||||
try pool.read { _ in }
|
try pool.read { _ in }
|
||||||
XCTFail("Expected TestError")
|
XCTFail("Expected TestError")
|
||||||
} catch is TestError { }
|
} catch is TestError { }
|
||||||
|
|
||||||
do {
|
do {
|
||||||
error = TestError()
|
errorMutex.value = TestError()
|
||||||
_ = try pool.makeSnapshot()
|
_ = try pool.makeSnapshot()
|
||||||
XCTFail("Expected TestError")
|
XCTFail("Expected TestError")
|
||||||
} catch is TestError { }
|
} catch is TestError { }
|
||||||
|
|
||||||
#if SQLITE_ENABLE_SNAPSHOT || (!GRDBCUSTOMSQLITE && !GRDBCIPHER)
|
#if SQLITE_ENABLE_SNAPSHOT || (!GRDBCUSTOMSQLITE && !GRDBCIPHER)
|
||||||
do {
|
do {
|
||||||
error = TestError()
|
errorMutex.value = TestError()
|
||||||
_ = try pool.makeSnapshotPool()
|
_ = try pool.makeSnapshotPool()
|
||||||
XCTFail("Expected TestError")
|
XCTFail("Expected TestError")
|
||||||
} catch is TestError { }
|
} catch is TestError { }
|
||||||
|
|||||||
@@ -13,21 +13,16 @@ import XCTest
|
|||||||
class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
||||||
|
|
||||||
func testDatabasePoolDeinitClosesAllConnections() throws {
|
func testDatabasePoolDeinitClosesAllConnections() throws {
|
||||||
let countQueue = DispatchQueue(label: "GRDB")
|
let openConnectionCountMutex = Mutex(0)
|
||||||
var openConnectionCount = 0
|
let totalOpenConnectionCountMutex = Mutex(0)
|
||||||
var totalOpenConnectionCount = 0
|
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
dbConfiguration.onConnectionDidOpen {
|
||||||
countQueue.sync {
|
totalOpenConnectionCountMutex.increment()
|
||||||
totalOpenConnectionCount += 1
|
openConnectionCountMutex.increment()
|
||||||
openConnectionCount += 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidClose = {
|
dbConfiguration.onConnectionDidClose {
|
||||||
countQueue.sync {
|
openConnectionCountMutex.decrement()
|
||||||
openConnectionCount -= 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write & read
|
// write & read
|
||||||
@@ -44,19 +39,19 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// One reader, one writer
|
// One reader, one writer
|
||||||
XCTAssertEqual(totalOpenConnectionCount, 2)
|
XCTAssertEqual(totalOpenConnectionCountMutex.value, 2)
|
||||||
|
|
||||||
// All connections are closed
|
// All connections are closed
|
||||||
XCTAssertEqual(openConnectionCount, 0)
|
XCTAssertEqual(openConnectionCountMutex.value, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
func testDatabasePoolReleasesMemoryOnPressureEvent() throws {
|
func testDatabasePoolReleasesMemoryOnPressureEvent() throws {
|
||||||
// Create a database pool, and expect a reader connection to be closed
|
// Create a database pool, and expect a reader connection to be closed
|
||||||
let expectation = self.expectation(description: "Reader connection closed")
|
let expectation = self.expectation(description: "Reader connection closed")
|
||||||
|
|
||||||
var configuration = Configuration()
|
var configuration = Configuration()
|
||||||
configuration.SQLiteConnectionWillClose = { conn in
|
configuration.onConnectionWillClose { conn in
|
||||||
if sqlite3_db_readonly(conn, nil) != 0 {
|
if sqlite3_db_readonly(conn, nil) != 0 {
|
||||||
expectation.fulfill()
|
expectation.fulfill()
|
||||||
}
|
}
|
||||||
@@ -76,7 +71,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
|||||||
waitForExpectations(timeout: 0.5)
|
waitForExpectations(timeout: 0.5)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDatabasePoolDoesNotReleaseMemoryOnPressureEventIfDisabled() throws {
|
func testDatabasePoolDoesNotReleaseMemoryOnPressureEventIfDisabled() throws {
|
||||||
// Create a database pool, and do not expect any reader connection to be closed
|
// Create a database pool, and do not expect any reader connection to be closed
|
||||||
let expectation = self.expectation(description: "Reader connection closed")
|
let expectation = self.expectation(description: "Reader connection closed")
|
||||||
@@ -84,7 +79,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
|||||||
|
|
||||||
var configuration = Configuration()
|
var configuration = Configuration()
|
||||||
configuration.automaticMemoryManagement = false
|
configuration.automaticMemoryManagement = false
|
||||||
configuration.SQLiteConnectionWillClose = { conn in
|
configuration.onConnectionWillClose { conn in
|
||||||
if sqlite3_db_readonly(conn, nil) != 0 {
|
if sqlite3_db_readonly(conn, nil) != 0 {
|
||||||
expectation.fulfill()
|
expectation.fulfill()
|
||||||
}
|
}
|
||||||
@@ -126,26 +121,21 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
|||||||
// Cleanup
|
// Cleanup
|
||||||
semaphore.signal()
|
semaphore.signal()
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
func test_DatabasePool_releaseMemory_closes_reader_connections() throws {
|
func test_DatabasePool_releaseMemory_closes_reader_connections() throws {
|
||||||
// A complicated test setup that opens multiple reader connections.
|
// A complicated test setup that opens multiple reader connections.
|
||||||
let countQueue = DispatchQueue(label: "GRDB")
|
let openConnectionCountMutex = Mutex(0)
|
||||||
var openConnectionCount = 0
|
let totalOpenConnectionCountMutex = Mutex(0)
|
||||||
var totalOpenConnectionCount = 0
|
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
dbConfiguration.onConnectionDidOpen {
|
||||||
countQueue.sync {
|
totalOpenConnectionCountMutex.increment()
|
||||||
totalOpenConnectionCount += 1
|
openConnectionCountMutex.increment()
|
||||||
openConnectionCount += 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidClose = {
|
dbConfiguration.onConnectionDidClose {
|
||||||
countQueue.sync {
|
openConnectionCountMutex.decrement()
|
||||||
openConnectionCount -= 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let dbPool = try makeDatabasePool()
|
let dbPool = try makeDatabasePool()
|
||||||
@@ -201,74 +191,69 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Two readers, one writer
|
// Two readers, one writer
|
||||||
XCTAssertEqual(totalOpenConnectionCount, 3)
|
XCTAssertEqual(totalOpenConnectionCountMutex.value, 3)
|
||||||
|
|
||||||
// Writer is still open
|
// Writer is still open
|
||||||
XCTAssertEqual(openConnectionCount, 1)
|
XCTAssertEqual(openConnectionCountMutex.value, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_DatabasePool_releaseMemory_closes_reader_connections_when_persistentReadOnlyConnections_is_false() throws {
|
func test_DatabasePool_releaseMemory_closes_reader_connections_when_persistentReadOnlyConnections_is_false() throws {
|
||||||
var persistentConnectionCount = 0
|
let persistentConnectionCountMutex = Mutex(0)
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
dbConfiguration.onConnectionDidOpen {
|
||||||
persistentConnectionCount += 1
|
persistentConnectionCountMutex.increment()
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidClose = {
|
dbConfiguration.onConnectionDidClose {
|
||||||
persistentConnectionCount -= 1
|
persistentConnectionCountMutex.decrement()
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfiguration.persistentReadOnlyConnections = false
|
dbConfiguration.persistentReadOnlyConnections = false
|
||||||
|
|
||||||
let dbPool = try makeDatabasePool()
|
let dbPool = try makeDatabasePool()
|
||||||
XCTAssertEqual(persistentConnectionCount, 1) // writer
|
XCTAssertEqual(persistentConnectionCountMutex.value, 1) // writer
|
||||||
|
|
||||||
try dbPool.read { _ in }
|
try dbPool.read { _ in }
|
||||||
XCTAssertEqual(persistentConnectionCount, 2) // writer + reader
|
XCTAssertEqual(persistentConnectionCountMutex.value, 2) // writer + reader
|
||||||
|
|
||||||
dbPool.releaseMemory()
|
dbPool.releaseMemory()
|
||||||
XCTAssertEqual(persistentConnectionCount, 1) // writer
|
XCTAssertEqual(persistentConnectionCountMutex.value, 1) // writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_DatabasePool_releaseMemory_does_not_close_reader_connections_when_persistentReadOnlyConnections_is_true() throws {
|
func test_DatabasePool_releaseMemory_does_not_close_reader_connections_when_persistentReadOnlyConnections_is_true() throws {
|
||||||
var persistentConnectionCount = 0
|
let persistentConnectionCountMutex = Mutex(0)
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
dbConfiguration.onConnectionDidOpen {
|
||||||
persistentConnectionCount += 1
|
persistentConnectionCountMutex.increment()
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidClose = {
|
dbConfiguration.onConnectionDidClose {
|
||||||
persistentConnectionCount -= 1
|
persistentConnectionCountMutex.decrement()
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfiguration.persistentReadOnlyConnections = true
|
dbConfiguration.persistentReadOnlyConnections = true
|
||||||
|
|
||||||
let dbPool = try makeDatabasePool()
|
let dbPool = try makeDatabasePool()
|
||||||
XCTAssertEqual(persistentConnectionCount, 1) // writer
|
XCTAssertEqual(persistentConnectionCountMutex.value, 1) // writer
|
||||||
|
|
||||||
try dbPool.read { _ in }
|
try dbPool.read { _ in }
|
||||||
XCTAssertEqual(persistentConnectionCount, 2) // writer + reader
|
XCTAssertEqual(persistentConnectionCountMutex.value, 2) // writer + reader
|
||||||
|
|
||||||
dbPool.releaseMemory()
|
dbPool.releaseMemory()
|
||||||
XCTAssertEqual(persistentConnectionCount, 2) // writer + reader
|
XCTAssertEqual(persistentConnectionCountMutex.value, 2) // writer + reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func testBlocksRetainConnection() throws {
|
func testBlocksRetainConnection() throws {
|
||||||
let countQueue = DispatchQueue(label: "GRDB")
|
let openConnectionCountMutex = Mutex(0)
|
||||||
var openConnectionCount = 0
|
let totalOpenConnectionCountMutex = Mutex(0)
|
||||||
var totalOpenConnectionCount = 0
|
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
dbConfiguration.onConnectionDidOpen {
|
||||||
countQueue.sync {
|
totalOpenConnectionCountMutex.increment()
|
||||||
totalOpenConnectionCount += 1
|
openConnectionCountMutex.increment()
|
||||||
openConnectionCount += 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidClose = {
|
dbConfiguration.onConnectionDidClose {
|
||||||
countQueue.sync {
|
openConnectionCountMutex.decrement()
|
||||||
openConnectionCount -= 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block 1 Block 2
|
// Block 1 Block 2
|
||||||
@@ -311,10 +296,10 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// one writer, one reader
|
// one writer, one reader
|
||||||
XCTAssertEqual(totalOpenConnectionCount, 2)
|
XCTAssertEqual(totalOpenConnectionCountMutex.value, 2)
|
||||||
|
|
||||||
// All connections are closed
|
// All connections are closed
|
||||||
XCTAssertEqual(openConnectionCount, 0)
|
XCTAssertEqual(openConnectionCountMutex.value, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testStatementDoNotRetainDatabaseConnection() throws {
|
func testStatementDoNotRetainDatabaseConnection() throws {
|
||||||
|
|||||||
@@ -4,21 +4,16 @@ import XCTest
|
|||||||
class DatabaseQueueReleaseMemoryTests: GRDBTestCase {
|
class DatabaseQueueReleaseMemoryTests: GRDBTestCase {
|
||||||
|
|
||||||
func testDatabaseQueueDeinitClosesConnection() throws {
|
func testDatabaseQueueDeinitClosesConnection() throws {
|
||||||
let countQueue = DispatchQueue(label: "GRDB")
|
let openConnectionCountMutex = Mutex(0)
|
||||||
var openConnectionCount = 0
|
let totalOpenConnectionCountMutex = Mutex(0)
|
||||||
var totalOpenConnectionCount = 0
|
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
dbConfiguration.onConnectionDidOpen {
|
||||||
countQueue.sync {
|
totalOpenConnectionCountMutex.increment()
|
||||||
totalOpenConnectionCount += 1
|
openConnectionCountMutex.increment()
|
||||||
openConnectionCount += 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidClose = {
|
dbConfiguration.onConnectionDidClose {
|
||||||
countQueue.sync {
|
openConnectionCountMutex.decrement()
|
||||||
openConnectionCount -= 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -27,28 +22,23 @@ class DatabaseQueueReleaseMemoryTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// One reader, one writer
|
// One reader, one writer
|
||||||
XCTAssertEqual(totalOpenConnectionCount, 1)
|
XCTAssertEqual(totalOpenConnectionCountMutex.value, 1)
|
||||||
|
|
||||||
// All connections are closed
|
// All connections are closed
|
||||||
XCTAssertEqual(openConnectionCount, 0)
|
XCTAssertEqual(openConnectionCountMutex.value, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testBlocksRetainConnection() throws {
|
func testBlocksRetainConnection() throws {
|
||||||
let countQueue = DispatchQueue(label: "GRDB")
|
let openConnectionCountMutex = Mutex(0)
|
||||||
var openConnectionCount = 0
|
let totalOpenConnectionCountMutex = Mutex(0)
|
||||||
var totalOpenConnectionCount = 0
|
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidOpen = {
|
dbConfiguration.onConnectionDidOpen {
|
||||||
countQueue.sync {
|
totalOpenConnectionCountMutex.increment()
|
||||||
totalOpenConnectionCount += 1
|
openConnectionCountMutex.increment()
|
||||||
openConnectionCount += 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfiguration.SQLiteConnectionDidClose = {
|
dbConfiguration.onConnectionDidClose {
|
||||||
countQueue.sync {
|
openConnectionCountMutex.decrement()
|
||||||
openConnectionCount -= 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block 1 Block 2
|
// Block 1 Block 2
|
||||||
@@ -91,10 +81,10 @@ class DatabaseQueueReleaseMemoryTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// one writer
|
// one writer
|
||||||
XCTAssertEqual(totalOpenConnectionCount, 1)
|
XCTAssertEqual(totalOpenConnectionCountMutex.value, 1)
|
||||||
|
|
||||||
// All connections are closed
|
// All connections are closed
|
||||||
XCTAssertEqual(openConnectionCount, 0)
|
XCTAssertEqual(openConnectionCountMutex.value, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testStatementDoNotRetainDatabaseConnection() throws {
|
func testStatementDoNotRetainDatabaseConnection() throws {
|
||||||
|
|||||||
@@ -378,7 +378,7 @@ class DatabaseQueueTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let parallelWritesCount = 50
|
let parallelWritesCount = 50
|
||||||
DispatchQueue.concurrentPerform(iterations: parallelWritesCount) { index in
|
DispatchQueue.concurrentPerform(iterations: parallelWritesCount) { [configuration] index in
|
||||||
let dbQueue = try! makeDatabaseQueue(filename: "test", configuration: configuration)
|
let dbQueue = try! makeDatabaseQueue(filename: "test", configuration: configuration)
|
||||||
try! dbQueue.write { db in
|
try! dbQueue.write { db in
|
||||||
_ = try Table("test").fetchCount(db)
|
_ = try Table("test").fetchCount(db)
|
||||||
|
|||||||
@@ -114,11 +114,11 @@ class DatabaseTraceTests : GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testTraceFromConfigurationWithDefaultOptions() throws {
|
func testTraceFromConfigurationWithDefaultOptions() throws {
|
||||||
var events: [String] = []
|
let eventsMutex: Mutex<[String]> = Mutex([])
|
||||||
var configuration = Configuration()
|
var configuration = Configuration()
|
||||||
configuration.prepareDatabase { db in
|
configuration.prepareDatabase { db in
|
||||||
db.trace { event in
|
db.trace { event in
|
||||||
events.append("SQL: \(event)")
|
eventsMutex.withLock { $0.append("SQL: \(event)") }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let dbQueue = try makeDatabaseQueue(configuration: configuration)
|
let dbQueue = try makeDatabaseQueue(configuration: configuration)
|
||||||
@@ -127,19 +127,19 @@ class DatabaseTraceTests : GRDBTestCase {
|
|||||||
CREATE table t(a);
|
CREATE table t(a);
|
||||||
INSERT INTO t (a) VALUES (?)
|
INSERT INTO t (a) VALUES (?)
|
||||||
""", arguments: [1])
|
""", arguments: [1])
|
||||||
XCTAssertEqual(events.suffix(2), [
|
XCTAssertEqual(eventsMutex.value.suffix(2), [
|
||||||
"SQL: CREATE table t(a)",
|
"SQL: CREATE table t(a)",
|
||||||
"SQL: INSERT INTO t (a) VALUES (?)"])
|
"SQL: INSERT INTO t (a) VALUES (?)"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTraceFromConfigurationWithPublicStatementArguments() throws {
|
func testTraceFromConfigurationWithPublicStatementArguments() throws {
|
||||||
var events: [String] = []
|
let eventsMutex: Mutex<[String]> = Mutex([])
|
||||||
var configuration = Configuration()
|
var configuration = Configuration()
|
||||||
configuration.publicStatementArguments = true
|
configuration.publicStatementArguments = true
|
||||||
configuration.prepareDatabase { db in
|
configuration.prepareDatabase { db in
|
||||||
db.trace { event in
|
db.trace { event in
|
||||||
events.append("SQL: \(event)")
|
eventsMutex.withLock { $0.append("SQL: \(event)") }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let dbQueue = try makeDatabaseQueue(configuration: configuration)
|
let dbQueue = try makeDatabaseQueue(configuration: configuration)
|
||||||
@@ -148,7 +148,7 @@ class DatabaseTraceTests : GRDBTestCase {
|
|||||||
CREATE table t(a);
|
CREATE table t(a);
|
||||||
INSERT INTO t (a) VALUES (?)
|
INSERT INTO t (a) VALUES (?)
|
||||||
""", arguments: [1])
|
""", arguments: [1])
|
||||||
XCTAssertEqual(events.suffix(2), [
|
XCTAssertEqual(eventsMutex.value.suffix(2), [
|
||||||
"SQL: CREATE table t(a)",
|
"SQL: CREATE table t(a)",
|
||||||
"SQL: INSERT INTO t (a) VALUES (1)"])
|
"SQL: INSERT INTO t (a) VALUES (1)"])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -272,16 +272,16 @@ class FTS4TableBuilderTests: GRDBTestCase {
|
|||||||
|
|
||||||
func testFTS4Compression() throws {
|
func testFTS4Compression() throws {
|
||||||
// Based on https://github.com/groue/GRDB.swift/issues/369
|
// Based on https://github.com/groue/GRDB.swift/issues/369
|
||||||
var compressCalled = false
|
let compressCalledMutex = Mutex(false)
|
||||||
var uncompressCalled = false
|
let uncompressCalledMutex = Mutex(false)
|
||||||
|
|
||||||
dbConfiguration.prepareDatabase { db in
|
dbConfiguration.prepareDatabase { db in
|
||||||
db.add(function: DatabaseFunction("zipit", argumentCount: 1, pure: true, function: { dbValues in
|
db.add(function: DatabaseFunction("zipit", argumentCount: 1, pure: true, function: { dbValues in
|
||||||
compressCalled = true
|
compressCalledMutex.value = true
|
||||||
return dbValues[0]
|
return dbValues[0]
|
||||||
}))
|
}))
|
||||||
db.add(function: DatabaseFunction("unzipit", argumentCount: 1, pure: true, function: { dbValues in
|
db.add(function: DatabaseFunction("unzipit", argumentCount: 1, pure: true, function: { dbValues in
|
||||||
uncompressCalled = true
|
uncompressCalledMutex.value = true
|
||||||
return dbValues[0]
|
return dbValues[0]
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@@ -296,12 +296,12 @@ class FTS4TableBuilderTests: GRDBTestCase {
|
|||||||
assertDidExecute(sql: "CREATE VIRTUAL TABLE \"documents\" USING fts4(content, compress=\"zipit\", uncompress=\"unzipit\")")
|
assertDidExecute(sql: "CREATE VIRTUAL TABLE \"documents\" USING fts4(content, compress=\"zipit\", uncompress=\"unzipit\")")
|
||||||
|
|
||||||
try db.execute(sql: "INSERT INTO documents (content) VALUES (?)", arguments: ["abc"])
|
try db.execute(sql: "INSERT INTO documents (content) VALUES (?)", arguments: ["abc"])
|
||||||
XCTAssertTrue(compressCalled)
|
XCTAssertTrue(compressCalledMutex.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
try dbPool.read { db in
|
try dbPool.read { db in
|
||||||
_ = try Row.fetchOne(db, sql: "SELECT * FROM documents")
|
_ = try Row.fetchOne(db, sql: "SELECT * FROM documents")
|
||||||
XCTAssertTrue(uncompressCalled)
|
XCTAssertTrue(uncompressCalledMutex.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ class GRDBTestCase: XCTestCase {
|
|||||||
dbConfiguration = Configuration()
|
dbConfiguration = Configuration()
|
||||||
|
|
||||||
// Test that database are deallocated in a clean state
|
// Test that database are deallocated in a clean state
|
||||||
dbConfiguration.SQLiteConnectionWillClose = { sqliteConnection in
|
dbConfiguration.onConnectionWillClose { sqliteConnection in
|
||||||
// https://www.sqlite.org/capi3ref.html#sqlite3_close:
|
// https://www.sqlite.org/capi3ref.html#sqlite3_close:
|
||||||
// > If sqlite3_close_v2() is called on a database connection that still
|
// > If sqlite3_close_v2() is called on a database connection that still
|
||||||
// > has outstanding prepared statements, BLOB handles, and/or
|
// > has outstanding prepared statements, BLOB handles, and/or
|
||||||
|
|||||||
Reference in new issue
Block a user