Configuration is Sendable

This commit is contained in:
Gwendal Roué committed 2024-07-27 16:37:32 +02:00
1 parent f7dc72a5f3
commit 54ffb21fb8
9 files changed
+123 -131

No files matched your search

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