mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Fix Pool.get when an error prevents a pool item from being created (counting semaphore wasn't signaled). This fixes DatabasePool.read when read connection can not be established. Tests.
This commit is contained in:
1 parent
a5979b2e47
commit
fc2689e207
2 files changed
+89
-25
No files matched your search
+23
-24
@@ -169,8 +169,29 @@ final class Pool<T> {
|
||||
/// Returns a tuple (element, releaseElement())
|
||||
/// Client MUST call releaseElement() after the element has been used.
|
||||
func get() throws -> (T, () -> ()) {
|
||||
let item = try lockItem()
|
||||
return (item.element, { self.unlockItem(item) })
|
||||
var item: PoolItem<T>! = nil
|
||||
_ = semaphore.wait(timeout: .distantFuture)
|
||||
do {
|
||||
try queue.sync {
|
||||
if let availableItem = self.items.first(where: { $0.available }) {
|
||||
item = availableItem
|
||||
item.available = false
|
||||
} else {
|
||||
item = try PoolItem(element: self.makeElement!(), available: false)
|
||||
self.items.append(item)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
semaphore.signal()
|
||||
throw error
|
||||
}
|
||||
let unlock = {
|
||||
self.queue.sync {
|
||||
item.available = true
|
||||
}
|
||||
self.semaphore.signal()
|
||||
}
|
||||
return (item.element, unlock)
|
||||
}
|
||||
|
||||
/// Performs a synchronous block with an element. The element turns
|
||||
@@ -204,28 +225,6 @@ final class Pool<T> {
|
||||
try block()
|
||||
}
|
||||
}
|
||||
|
||||
private func lockItem() throws -> PoolItem<T> {
|
||||
var item: PoolItem<T>! = nil
|
||||
_ = semaphore.wait(timeout: .distantFuture)
|
||||
try queue.sync {
|
||||
if let availableItem = self.items.first(where: { $0.available }) {
|
||||
item = availableItem
|
||||
item.available = false
|
||||
} else {
|
||||
item = try PoolItem(element: self.makeElement!(), available: false)
|
||||
self.items.append(item)
|
||||
}
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
private func unlockItem(_ item: PoolItem<T>) {
|
||||
queue.sync {
|
||||
item.available = true
|
||||
}
|
||||
semaphore.signal()
|
||||
}
|
||||
}
|
||||
|
||||
private class PoolItem<T> {
|
||||
|
||||
@@ -247,11 +247,54 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
do {
|
||||
try db.execute("BEGIN DEFERRED TRANSACTION")
|
||||
XCTFail("Expected error")
|
||||
} catch {
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.code, 1) // SQLITE_ERROR
|
||||
XCTAssertEqual(error.message!, "cannot start a transaction within a transaction")
|
||||
XCTAssertEqual(error.sql!, "BEGIN DEFERRED TRANSACTION")
|
||||
XCTAssertEqual(error.description, "SQLite error 1 with statement `BEGIN DEFERRED TRANSACTION`: cannot start a transaction within a transaction")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReadError() {
|
||||
assertNoError {
|
||||
let dbPool = try makeDatabasePool()
|
||||
|
||||
// Block 1 Block 2
|
||||
// PRAGMA locking_mode=EXCLUSIVE
|
||||
// CREATE TABLE
|
||||
// >
|
||||
let s1 = DispatchSemaphore(value: 0)
|
||||
// dbPool.read // throws
|
||||
|
||||
let block1 = { () in
|
||||
try! dbPool.write { db in
|
||||
try db.execute("PRAGMA locking_mode=EXCLUSIVE")
|
||||
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
s1.signal()
|
||||
}
|
||||
}
|
||||
let block2 = { () in
|
||||
_ = s1.wait(timeout: .distantFuture)
|
||||
do {
|
||||
try dbPool.read { _ in }
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.code, 5) // SQLITE_BUSY
|
||||
XCTAssertEqual(error.message!, "database is locked")
|
||||
XCTAssertTrue(error.sql == nil)
|
||||
XCTAssertEqual(error.description, "SQLite error 5: database is locked")
|
||||
} catch {
|
||||
XCTFail("Expected DatabaseError")
|
||||
}
|
||||
}
|
||||
let blocks = [block1, block2]
|
||||
DispatchQueue.concurrentPerform(iterations: blocks.count) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testConcurrentRead() {
|
||||
assertNoError {
|
||||
@@ -841,6 +884,28 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testReadFromCurrentStateError() {
|
||||
assertNoError {
|
||||
dbConfiguration.trace = { print($0) }
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.write { db in
|
||||
try db.execute("PRAGMA locking_mode=EXCLUSIVE")
|
||||
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
do {
|
||||
try dbPool.readFromCurrentState { db in
|
||||
fatalError("Should not run")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.code, 5) // SQLITE_BUSY
|
||||
XCTAssertEqual(error.message!, "database is locked")
|
||||
XCTAssertTrue(error.sql == nil)
|
||||
XCTAssertEqual(error.description, "SQLite error 5: database is locked")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testIssue80() {
|
||||
// See https://github.com/groue/GRDB.swift/issues/80
|
||||
//
|
||||
|
||||
Reference in new issue
Block a user