mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
More tests and documentation for DatabaseScheduler
This commit is contained in:
1 parent
9d00189ac0
commit
5e4923b5bc
2 files changed
+62
-7
No files matched your search
@@ -1,13 +1,21 @@
|
||||
/// DatabaseScheduler makes sure that databases are used on correct dispatch
|
||||
/// queues.
|
||||
/// DatabaseScheduler makes sure that databases connections are used on correct
|
||||
/// dispatch queues, and warns the user with a fatal error whenever she misuses
|
||||
/// a database connection.
|
||||
///
|
||||
/// It has three entry points:
|
||||
/// Generally speaking, each connection has its own dispatch queue. But it's not
|
||||
/// enough: users need to use two database connections at the same time:
|
||||
/// https://github.com/groue/GRDB.swift/issues/55. To support this use case, a
|
||||
/// single dispatch queue can be temporarily shared by two or more connections.
|
||||
///
|
||||
/// Managing this queue sharing is the job of the DatabaseScheduler class.
|
||||
///
|
||||
/// Three entry points:
|
||||
///
|
||||
/// - DatabaseScheduler.makeSerializedQueueAllowing(database:) creates a
|
||||
/// dispatch queue that allows one database.
|
||||
///
|
||||
/// It does so by registering one instance of DatabaseScheduler as a specific
|
||||
/// of the dispatch queue, a DatabaseScheduler that allows that database.
|
||||
/// of the dispatch queue, a DatabaseScheduler that allows that database only.
|
||||
///
|
||||
/// - The dispatchSync() function helps using several databases in the same
|
||||
/// dispatch queue. It does so by temporarily extending the allowed databases
|
||||
@@ -117,7 +125,11 @@ class DatabaseScheduler {
|
||||
}
|
||||
|
||||
static func preconditionValidQueue(db: Database, @autoclosure _ message: () -> String = "Database was not used on the correct thread.", file: StaticString = #file, line: UInt = #line) {
|
||||
GRDBPrecondition(currentScheduler()?.allows(db) ?? false, message, file: file, line: line)
|
||||
GRDBPrecondition(allows(db), message, file: file, line: line)
|
||||
}
|
||||
|
||||
static func allows(db: Database) -> Bool {
|
||||
return currentScheduler()?.allows(db) ?? false
|
||||
}
|
||||
|
||||
private func allows(db: Database) -> Bool {
|
||||
|
||||
@@ -1,12 +1,55 @@
|
||||
import XCTest
|
||||
#if SQLITE_HAS_CODEC
|
||||
import GRDBCipher
|
||||
@testable import GRDBCipher // @testable so that we can test DatabaseScheduler
|
||||
#else
|
||||
import GRDB
|
||||
@testable import GRDB // @testable so that we can test DatabaseScheduler
|
||||
#endif
|
||||
|
||||
class DatabaseSchedulerTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseScheduler() {
|
||||
assertNoError {
|
||||
let dbQueue1 = try makeDatabaseQueue("db1")
|
||||
let dbQueue2 = try makeDatabaseQueue("db2")
|
||||
let dbQueue3 = try makeDatabaseQueue("db3")
|
||||
|
||||
var db1: Database! = nil
|
||||
var db2: Database! = nil
|
||||
var db3: Database! = nil
|
||||
dbQueue1.inDatabase { db1 = $0 }
|
||||
dbQueue2.inDatabase { db2 = $0 }
|
||||
dbQueue3.inDatabase { db3 = $0 }
|
||||
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db1))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db2))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db3))
|
||||
dbQueue1.inDatabase { _ in
|
||||
XCTAssertTrue(DatabaseScheduler.allows(db1))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db2))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db3))
|
||||
dbQueue2.inDatabase { _ in
|
||||
XCTAssertTrue(DatabaseScheduler.allows(db1))
|
||||
XCTAssertTrue(DatabaseScheduler.allows(db2))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db3))
|
||||
dbQueue3.inDatabase { _ in
|
||||
XCTAssertTrue(DatabaseScheduler.allows(db1))
|
||||
XCTAssertTrue(DatabaseScheduler.allows(db2))
|
||||
XCTAssertTrue(DatabaseScheduler.allows(db3))
|
||||
}
|
||||
XCTAssertTrue(DatabaseScheduler.allows(db1))
|
||||
XCTAssertTrue(DatabaseScheduler.allows(db2))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db3))
|
||||
}
|
||||
XCTAssertTrue(DatabaseScheduler.allows(db1))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db3))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db2))
|
||||
}
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db1))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db2))
|
||||
XCTAssertFalse(DatabaseScheduler.allows(db3))
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseQueueFromDatabaseQueue() {
|
||||
assertNoError {
|
||||
let dbQueue1 = try makeDatabaseQueue("db1")
|
||||
|
||||
Reference in new issue
Block a user