mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
StrictConcurrency/tests: introduce TestDatabaseBuilder that can connect from Sendable closures
This commit is contained in:
1 parent
afcac0102f
commit
2e10969634
3 files changed
+189
-72
No files matched your search
@@ -35,8 +35,9 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
let s0 = DispatchSemaphore(value: 0)
|
||||
let builder = makeDatabaseBuilder()
|
||||
let block1: @Sendable () -> Void = {
|
||||
let dbQueue = try! self.makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try! builder.makeDatabaseQueue(filename: "test.sqlite")
|
||||
s0.signal() // Avoid "database is locked" error: don't open the two databases at the same time
|
||||
try! dbQueue.writeWithoutTransaction { db in
|
||||
try db.execute(sql: "BEGIN DEFERRED TRANSACTION")
|
||||
@@ -51,7 +52,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
let block2: @Sendable () -> Void = {
|
||||
_ = s0.wait(timeout: .distantFuture) // Avoid "database is locked" error: don't open the two databases at the same time
|
||||
let dbQueue = try! self.makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try! builder.makeDatabaseQueue(filename: "test.sqlite")
|
||||
try! dbQueue.writeWithoutTransaction { db in
|
||||
_ = s1.wait(timeout: .distantFuture)
|
||||
try db.execute(sql: "INSERT INTO moves VALUES (1)")
|
||||
@@ -97,8 +98,9 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
let s0 = DispatchSemaphore(value: 0)
|
||||
let builder = makeDatabaseBuilder()
|
||||
let block1: @Sendable () -> Void = {
|
||||
let dbQueue = try! self.makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try! builder.makeDatabaseQueue(filename: "test.sqlite")
|
||||
s0.signal() // Avoid "database is locked" error: don't open the two databases at the same time
|
||||
try! dbQueue.writeWithoutTransaction { db in
|
||||
_ = s1.wait(timeout: .distantFuture)
|
||||
@@ -114,7 +116,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
let block2: @Sendable () -> Void = {
|
||||
_ = s0.wait(timeout: .distantFuture) // Avoid "database is locked" error: don't open the two databases at the same time
|
||||
let dbQueue = try! self.makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try! builder.makeDatabaseQueue(filename: "test.sqlite")
|
||||
try! dbQueue.writeWithoutTransaction { db in
|
||||
try db.execute(sql: "BEGIN DEFERRED TRANSACTION")
|
||||
s1.signal()
|
||||
@@ -162,8 +164,9 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
let s0 = DispatchSemaphore(value: 0)
|
||||
let builder = makeDatabaseBuilder()
|
||||
let block1: @Sendable () -> Void = {
|
||||
let dbQueue = try! self.makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try! builder.makeDatabaseQueue(filename: "test.sqlite")
|
||||
s0.signal() // Avoid "database is locked" error: don't open the two databases at the same time
|
||||
try! dbQueue.writeWithoutTransaction { db in
|
||||
try db.execute(sql: "BEGIN DEFERRED TRANSACTION")
|
||||
@@ -178,7 +181,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
let block2: @Sendable () -> Void = {
|
||||
_ = s0.wait(timeout: .distantFuture) // Avoid "database is locked" error: don't open the two databases at the same time
|
||||
let dbQueue = try! self.makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try! builder.makeDatabaseQueue(filename: "test.sqlite")
|
||||
try! dbQueue.writeWithoutTransaction { db in
|
||||
_ = s1.wait(timeout: .distantFuture)
|
||||
try db.execute(sql: "BEGIN DEFERRED TRANSACTION")
|
||||
@@ -968,9 +971,14 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
|
||||
@MainActor
|
||||
func testTargetQueue() throws {
|
||||
@Sendable func test(targetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.targetQueue = targetQueue
|
||||
let dbPool = try makeDatabasePool()
|
||||
// background queue
|
||||
do {
|
||||
let targetQueue = DispatchQueue.global(qos: .background)
|
||||
|
||||
var config = dbConfiguration!
|
||||
config.targetQueue = targetQueue
|
||||
|
||||
let dbPool = try makeDatabasePool(configuration: config)
|
||||
try dbPool.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
@@ -979,24 +987,41 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// background queue
|
||||
try test(targetQueue: .global(qos: .background))
|
||||
|
||||
// main queue
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
try! test(targetQueue: .main)
|
||||
expectation.fulfill()
|
||||
do {
|
||||
let targetQueue = DispatchQueue.main
|
||||
|
||||
var config = dbConfiguration!
|
||||
config.targetQueue = targetQueue
|
||||
|
||||
let builder = makeDatabaseBuilder(configuration: config)
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
let dbPool = try! builder.makeDatabasePool()
|
||||
try! dbPool.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
try! dbPool.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
}
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func testWriteTargetQueue() throws {
|
||||
@Sendable func test(targetQueue: DispatchQueue, writeTargetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.targetQueue = targetQueue
|
||||
dbConfiguration.writeTargetQueue = writeTargetQueue
|
||||
let dbPool = try makeDatabasePool()
|
||||
// background queue
|
||||
do {
|
||||
let targetQueue = DispatchQueue.global(qos: .background)
|
||||
let writeTargetQueue = DispatchQueue(label: "writer")
|
||||
|
||||
var config = dbConfiguration!
|
||||
config.targetQueue = targetQueue
|
||||
config.writeTargetQueue = writeTargetQueue
|
||||
|
||||
let dbPool = try makeDatabasePool(configuration: config)
|
||||
try dbPool.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(writeTargetQueue))
|
||||
}
|
||||
@@ -1005,16 +1030,29 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// background queue
|
||||
try test(targetQueue: .global(qos: .background), writeTargetQueue: DispatchQueue(label: "writer"))
|
||||
|
||||
// main queue
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
try! test(targetQueue: .main, writeTargetQueue: .main)
|
||||
expectation.fulfill()
|
||||
do {
|
||||
let targetQueue = DispatchQueue.main
|
||||
let writeTargetQueue = DispatchQueue.main
|
||||
|
||||
var config = dbConfiguration!
|
||||
config.targetQueue = targetQueue
|
||||
config.writeTargetQueue = writeTargetQueue
|
||||
|
||||
let builder = makeDatabaseBuilder(configuration: config)
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
let dbPool = try! builder.makeDatabasePool()
|
||||
try! dbPool.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(writeTargetQueue))
|
||||
}
|
||||
try! dbPool.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
}
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
}
|
||||
|
||||
func testWriteTargetQueueReadOnly() throws {
|
||||
|
||||
@@ -153,9 +153,14 @@ class DatabaseQueueTests: GRDBTestCase {
|
||||
|
||||
@MainActor
|
||||
func testTargetQueue() throws {
|
||||
@Sendable func test(targetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.targetQueue = targetQueue
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
// background queue
|
||||
do {
|
||||
let targetQueue = DispatchQueue.global(qos: .background)
|
||||
|
||||
var config = dbConfiguration!
|
||||
config.targetQueue = targetQueue
|
||||
|
||||
let dbQueue = try makeDatabaseQueue(configuration: config)
|
||||
try dbQueue.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
@@ -164,24 +169,41 @@ class DatabaseQueueTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// background queue
|
||||
try test(targetQueue: .global(qos: .background))
|
||||
|
||||
// main queue
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
try! test(targetQueue: .main)
|
||||
expectation.fulfill()
|
||||
do {
|
||||
let targetQueue = DispatchQueue.main
|
||||
|
||||
var config = dbConfiguration!
|
||||
config.targetQueue = targetQueue
|
||||
|
||||
let builder = makeDatabaseBuilder(configuration: config)
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
let dbQueue = try! builder.makeDatabaseQueue()
|
||||
try! dbQueue.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
try! dbQueue.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func testWriteTargetQueue() throws {
|
||||
@Sendable func test(targetQueue: DispatchQueue, writeTargetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.targetQueue = targetQueue // unused
|
||||
dbConfiguration.writeTargetQueue = writeTargetQueue
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
// background queue
|
||||
do {
|
||||
let targetQueue = DispatchQueue.global(qos: .background)
|
||||
let writeTargetQueue = DispatchQueue.global(qos: .background)
|
||||
|
||||
var config = dbConfiguration!
|
||||
config.targetQueue = targetQueue // unused
|
||||
config.writeTargetQueue = writeTargetQueue
|
||||
|
||||
let dbQueue = try makeDatabaseQueue(configuration: config)
|
||||
try dbQueue.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(writeTargetQueue))
|
||||
}
|
||||
@@ -190,16 +212,29 @@ class DatabaseQueueTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// background queue
|
||||
try test(targetQueue: .global(qos: .background), writeTargetQueue: DispatchQueue(label: "writer"))
|
||||
|
||||
// main queue
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
try! test(targetQueue: .main, writeTargetQueue: .main)
|
||||
expectation.fulfill()
|
||||
do {
|
||||
let targetQueue = DispatchQueue.main
|
||||
let writeTargetQueue = DispatchQueue.main
|
||||
|
||||
var config = dbConfiguration!
|
||||
config.targetQueue = targetQueue // unused
|
||||
config.writeTargetQueue = writeTargetQueue
|
||||
|
||||
let builder = makeDatabaseBuilder(configuration: config)
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
let dbQueue = try! builder.makeDatabaseQueue()
|
||||
try! dbQueue.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(writeTargetQueue))
|
||||
}
|
||||
try! dbQueue.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(writeTargetQueue))
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
|
||||
func testWriteTargetQueueReadOnly() throws {
|
||||
@@ -380,8 +415,9 @@ class DatabaseQueueTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
let parallelWritesCount = 50
|
||||
DispatchQueue.concurrentPerform(iterations: parallelWritesCount) { [configuration] index in
|
||||
let dbQueue = try! makeDatabaseQueue(filename: "test", configuration: configuration)
|
||||
let builder = makeDatabaseBuilder(configuration: configuration)
|
||||
DispatchQueue.concurrentPerform(iterations: parallelWritesCount) { index in
|
||||
let dbQueue = try! builder.makeDatabaseQueue(filename: "test")
|
||||
try! dbQueue.write { db in
|
||||
_ = try Table("test").fetchCount(db)
|
||||
try db.execute(sql: "INSERT INTO test VALUES (1)")
|
||||
|
||||
@@ -27,30 +27,44 @@ class GRDBTestCase: XCTestCase {
|
||||
// The default configuration for tests
|
||||
var dbConfiguration: Configuration!
|
||||
|
||||
// Builds a database queue based on dbConfiguration
|
||||
func makeDatabaseQueue(filename: String? = nil) throws -> DatabaseQueue {
|
||||
try makeDatabaseQueue(filename: filename, configuration: dbConfiguration)
|
||||
/// Returns a test database builder.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - configuration: If nil, dbConfiguration is used.
|
||||
func makeDatabaseBuilder(
|
||||
configuration: Configuration? = nil
|
||||
) -> TestDatabaseBuilder {
|
||||
TestDatabaseBuilder(
|
||||
directoryPath: dbDirectoryPath,
|
||||
configuration: configuration ?? dbConfiguration)
|
||||
}
|
||||
|
||||
// Builds a database queue
|
||||
func makeDatabaseQueue(filename: String? = nil, configuration: Configuration) throws -> DatabaseQueue {
|
||||
try FileManager.default.createDirectory(atPath: dbDirectoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
let dbPath = (dbDirectoryPath as NSString).appendingPathComponent(filename ?? ProcessInfo.processInfo.globallyUniqueString)
|
||||
let dbQueue = try DatabaseQueue(path: dbPath, configuration: configuration)
|
||||
/// Builds a database queue, and calls `setup(_:)`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - filename: If nil, a unique file name is used.
|
||||
/// - configuration: If nil, dbConfiguration is used.
|
||||
func makeDatabaseQueue(
|
||||
filename: String? = nil,
|
||||
configuration: Configuration? = nil
|
||||
) throws -> DatabaseQueue {
|
||||
let builder = makeDatabaseBuilder(configuration: configuration ?? dbConfiguration)
|
||||
let dbQueue = try builder.makeDatabaseQueue(filename: filename)
|
||||
try setup(dbQueue)
|
||||
return dbQueue
|
||||
}
|
||||
|
||||
// Builds a database pool based on dbConfiguration
|
||||
func makeDatabasePool(filename: String? = nil) throws -> DatabasePool {
|
||||
try makeDatabasePool(filename: filename, configuration: dbConfiguration)
|
||||
}
|
||||
|
||||
// Builds a database pool
|
||||
func makeDatabasePool(filename: String? = nil, configuration: Configuration) throws -> DatabasePool {
|
||||
try FileManager.default.createDirectory(atPath: dbDirectoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
let dbPath = (dbDirectoryPath as NSString).appendingPathComponent(filename ?? ProcessInfo.processInfo.globallyUniqueString)
|
||||
let dbPool = try DatabasePool(path: dbPath, configuration: configuration)
|
||||
/// Builds a database queue, and calls `setup(_:)`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - filename: If nil, a unique file name is used.
|
||||
/// - configuration: If nil, dbConfiguration is used.
|
||||
func makeDatabasePool(
|
||||
filename: String? = nil,
|
||||
configuration: Configuration? = nil
|
||||
) throws -> DatabasePool {
|
||||
let builder = makeDatabaseBuilder(configuration: configuration ?? dbConfiguration)
|
||||
let dbPool = try builder.makeDatabasePool(filename: filename)
|
||||
try setup(dbPool)
|
||||
return dbPool
|
||||
}
|
||||
@@ -222,6 +236,35 @@ class GRDBTestCase: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
struct TestDatabaseBuilder: Sendable {
|
||||
fileprivate var directoryPath: String
|
||||
fileprivate var configuration: Configuration
|
||||
|
||||
func makeDatabaseQueue(filename: String? = nil) throws -> DatabaseQueue {
|
||||
try FileManager.default.createDirectory(
|
||||
atPath: directoryPath,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil)
|
||||
|
||||
let filename = filename ?? ProcessInfo.processInfo.globallyUniqueString
|
||||
let dbPath = (directoryPath as NSString).appendingPathComponent(filename)
|
||||
|
||||
return try DatabaseQueue(path: dbPath, configuration: configuration)
|
||||
}
|
||||
|
||||
func makeDatabasePool(filename: String? = nil) throws -> DatabasePool {
|
||||
try FileManager.default.createDirectory(
|
||||
atPath: directoryPath,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil)
|
||||
|
||||
let filename = filename ?? ProcessInfo.processInfo.globallyUniqueString
|
||||
let dbPath = (directoryPath as NSString).appendingPathComponent(filename)
|
||||
|
||||
return try DatabasePool(path: dbPath, configuration: configuration)
|
||||
}
|
||||
}
|
||||
|
||||
#if SWIFT_PACKAGE
|
||||
let testBundle = Bundle.module
|
||||
#else
|
||||
|
||||
Reference in new issue
Block a user