mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
DatabasePool.barrierWriteWithoutTransaction tests
This commit is contained in:
1 parent
e3efcc0d87
commit
5b4a225dc3
1 file changed
+83
@@ -1186,4 +1186,87 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// MARK: - Barrier
|
||||
|
||||
func testBarrierLocksReads() throws {
|
||||
if #available(OSX 10.10, *) {
|
||||
let expectation = self.expectation(description: "lock")
|
||||
expectation.isInverted = true
|
||||
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
}
|
||||
var fetchedValue: Int?
|
||||
let s1 = DispatchSemaphore(value: 0)
|
||||
let s2 = DispatchSemaphore(value: 0)
|
||||
let s3 = DispatchSemaphore(value: 0)
|
||||
|
||||
DispatchQueue.global().async {
|
||||
try! dbPool.barrierWriteWithoutTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)")
|
||||
s1.signal()
|
||||
s2.wait()
|
||||
}
|
||||
}
|
||||
|
||||
DispatchQueue.global().async {
|
||||
// Wait for barrier to start
|
||||
s1.wait()
|
||||
|
||||
fetchedValue = try! dbPool.read { db in
|
||||
try Int.fetchOne(db, sql: "SELECT id FROM items")!
|
||||
}
|
||||
expectation.fulfill()
|
||||
s3.signal()
|
||||
}
|
||||
|
||||
// Assert that read is blocked
|
||||
waitForExpectations(timeout: 1)
|
||||
|
||||
// Release barrier
|
||||
s2.signal()
|
||||
|
||||
// Wait for read to complete
|
||||
s3.wait()
|
||||
XCTAssertEqual(fetchedValue, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func testBarrierIsLockedByOneUnfinishedRead() throws {
|
||||
if #available(OSX 10.10, *) {
|
||||
let expectation = self.expectation(description: "lock")
|
||||
expectation.isInverted = true
|
||||
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
}
|
||||
|
||||
// Read
|
||||
let s1 = DispatchSemaphore(value: 0)
|
||||
let s2 = DispatchSemaphore(value: 0)
|
||||
dbPool.asyncRead { _ in
|
||||
s1.signal()
|
||||
s2.wait()
|
||||
}
|
||||
|
||||
let s3 = DispatchSemaphore(value: 0)
|
||||
DispatchQueue.global().async {
|
||||
dbPool.barrierWriteWithoutTransaction { _ in }
|
||||
expectation.fulfill()
|
||||
s3.signal()
|
||||
}
|
||||
|
||||
// Assert that barrier is blocked
|
||||
waitForExpectations(timeout: 1)
|
||||
|
||||
// Release read
|
||||
s2.signal()
|
||||
|
||||
// Wait for barrier to complete
|
||||
s3.wait()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user