DatabasePool concurrency tests

This commit is contained in:
Gwendal Roué committed 2016-03-09 19:29:45 +01:00
1 parent 526e065e7d
commit e0031ddc42
4 files changed
+163 -14

No files matched your search

+13 -5
View File
@@ -1,6 +1,6 @@
public final class DatabasePool {
public convenience init(path: String, configuration: Configuration = Configuration(), maxReaderCount: Int = 5) throws {
precondition(maxReaderCount > 1, "maxReaderCount must be at least 1")
public convenience init(path: String, configuration: Configuration = Configuration(), maximumReaderCount: Int = 5) throws {
precondition(maximumReaderCount > 1, "maximumReaderCount must be at least 1")
// Shared database schema cache
let schemaCache = SharedDatabaseSchemaCache()
@@ -8,7 +8,7 @@ public final class DatabasePool {
// Readers
var readConfig = configuration
readConfig.readonly = true
let readerPool = Pool(size: maxReaderCount) {
let readerPool = Pool(maximumCount: maximumReaderCount) {
try! SerializedDatabase(path: path, configuration: readConfig, schemaCache: schemaCache)
}
@@ -50,11 +50,11 @@ public final class DatabasePool {
}
}
public func checkpoint() throws {
public func checkpoint(kind: CheckpointKind = .Passive) throws {
try writer.inDatabase { db in
// TODO: read https://www.sqlite.org/c3ref/wal_checkpoint_v2.html and
// check whether we need a busy handler on writer and/or readers.
let code = sqlite3_wal_checkpoint_v2(db.sqliteConnection, nil, SQLITE_CHECKPOINT_TRUNCATE, nil, nil)
let code = sqlite3_wal_checkpoint_v2(db.sqliteConnection, nil, kind.rawValue, nil, nil)
guard code == SQLITE_OK else {
throw DatabaseError(code: code, message: db.lastErrorMessage, sql: nil)
}
@@ -69,3 +69,11 @@ public final class DatabasePool {
self.writer = writer
}
}
// See https://www.sqlite.org/c3ref/wal_checkpoint_v2.html
public enum CheckpointKind: Int32 {
case Passive = 0 // SQLITE_CHECKPOINT_PASSIVE
case Full = 1 // SQLITE_CHECKPOINT_FULL
case Restart = 2 // SQLITE_CHECKPOINT_RESTART
case Truncate = 3 // SQLITE_CHECKPOINT_TRUNCATE
}
+3 -3
View File
@@ -165,11 +165,11 @@ final class Pool<T> {
private let queue: dispatch_queue_t // protects availableElements
private let semaphore: dispatch_semaphore_t // limits the number of elements
init(size: Int, makeElement: () -> T) {
precondition(size > 0, "Pool size must be at least 1")
init(maximumCount: Int, makeElement: () -> T) {
precondition(maximumCount > 0, "Pool size must be at least 1")
self.makeElement = makeElement
self.queue = dispatch_queue_create("com.github.groue.GRDB.Pool", nil)
self.semaphore = dispatch_semaphore_create(size)
self.semaphore = dispatch_semaphore_create(maximumCount)
}
func get<U>(@noescape block: (T) throws -> U) rethrows -> U {
-6
View File
@@ -11,13 +11,7 @@
- [ ] REGEXP https://www.sqlite.org/lang_expr.html
- [ ] CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END https://www.sqlite.org/lang_expr.html
- [ ] DatabasePool
- [ ] Tests :-)
- [ ] Forbid transactions and save points in readers
- [ ] DatabasePool.checkpoint() https://www.sqlite.org/c3ref/wal_checkpoint_v2.html
- [ ] Concurrency tests:
- [ ] Iterate rows concurrently.
- [ ] Iterate rows, and concurrently, update the database rows. Is the selection modified?
- [ ] Iterate rows, and concurrently, update the database rows and then checkpoint. Is the selection modified?
Not sure:
@@ -16,4 +16,151 @@ class DatabasePoolTests: GRDBTestCase {
}
}
func testConcurrentRead() {
assertNoError {
try dbPool.write { db in
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
for _ in 0..<3 {
try db.execute("INSERT INTO items (id) VALUES (NULL)")
}
}
// Block 1 Block 2
// SELECT * FROM items SELECT * FROM items
// step step
// >
let s1 = dispatch_semaphore_create(0)
// step
// <
let s2 = dispatch_semaphore_create(0)
// step step
// step end
// end
let block1 = { () in
self.dbPool.read { db in
let generator = Row.fetch(db, "SELECT * FROM items").generate()
XCTAssertTrue(generator.next() != nil)
dispatch_semaphore_signal(s1)
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
XCTAssertTrue(generator.next() != nil)
XCTAssertTrue(generator.next() != nil)
XCTAssertTrue(generator.next() == nil)
}
}
let block2 = { () in
self.dbPool.read { db in
let generator = Row.fetch(db, "SELECT * FROM items").generate()
XCTAssertTrue(generator.next() != nil)
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
XCTAssertTrue(generator.next() != nil)
dispatch_semaphore_signal(s2)
XCTAssertTrue(generator.next() != nil)
XCTAssertTrue(generator.next() == nil)
}
}
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
dispatch_apply(2, queue) { index in
[block1, block2][index]()
}
}
}
func testConcurrentReadWrite() {
assertNoError {
try dbPool.write { db in
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
for _ in 0..<2 {
try db.execute("INSERT INTO items (id) VALUES (NULL)")
}
}
// Block 1 Block 2
// SELECT * FROM items
// step
// >
let s1 = dispatch_semaphore_create(0)
// DELETE * FROM items
// <
let s2 = dispatch_semaphore_create(0)
// step
// end
let block1 = { () in
self.dbPool.read { db in
let generator = Row.fetch(db, "SELECT * FROM items").generate()
XCTAssertTrue(generator.next() != nil)
dispatch_semaphore_signal(s1)
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
XCTAssertTrue(generator.next() != nil)
XCTAssertTrue(generator.next() == nil)
}
}
let block2 = { () in
do {
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
defer { dispatch_semaphore_signal(s2) }
try self.dbPool.write { db in
try db.execute("DELETE FROM items")
}
} catch {
XCTFail("error: \(error)")
}
}
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
dispatch_apply(2, queue) { index in
[block1, block2][index]()
}
}
}
func testConcurrentReadWritePlusCheckpoint() {
assertNoError {
try dbPool.write { db in
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
for _ in 0..<2 {
try db.execute("INSERT INTO items (id) VALUES (NULL)")
}
}
// Block 1 Block 2
// SELECT * FROM items
// step
// >
let s1 = dispatch_semaphore_create(0)
// DELETE * FROM items
// Checkpoint
// <
let s2 = dispatch_semaphore_create(0)
// step
// end
let block1 = { () in
self.dbPool.read { db in
let generator = Row.fetch(db, "SELECT * FROM items").generate()
XCTAssertTrue(generator.next() != nil)
dispatch_semaphore_signal(s1)
dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER)
XCTAssertTrue(generator.next() != nil)
XCTAssertTrue(generator.next() == nil)
}
}
let block2 = { () in
do {
dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER)
defer { dispatch_semaphore_signal(s2) }
try self.dbPool.write { db in
try db.execute("DELETE FROM items")
}
try self.dbPool.checkpoint() // Default checkpoint should not fail if there is a reader (block1)
} catch {
XCTFail("error: \(error)")
}
}
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
dispatch_apply(2, queue) { index in
[block1, block2][index]()
}
}
}
}