From 2fee6799d02ecd3e910a0c5b3dc5e9dec896b9db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 12 Sep 2015 13:22:01 +0200 Subject: [PATCH] =?UTF-8?q?DatabaseQueue.inTransaction()=20and=20DatabaseQ?= =?UTF-8?q?ueue.inDatabase()=20are=20no=20longer=20reentrant,=20since=20we?= =?UTF-8?q?=20don=E2=80=99t=20(yet=3F)=20implement=20it=20in=20a=20robust?= =?UTF-8?q?=20way.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Public/Core/Database/DatabaseTests.swift | 46 ++++++++++--------- GRDB/Core/DatabaseQueue.swift | 7 +-- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/GRDB Tests/Public/Core/Database/DatabaseTests.swift b/GRDB Tests/Public/Core/Database/DatabaseTests.swift index 1f833dfee..853543ae0 100644 --- a/GRDB Tests/Public/Core/Database/DatabaseTests.swift +++ b/GRDB Tests/Public/Core/Database/DatabaseTests.swift @@ -382,29 +382,31 @@ class DatabaseTests : GRDBTestCase { } } - func testInDatabaseIsReentrant() { - var success = false - dbQueue.inDatabase { db in - self.dbQueue.inDatabase { db in - success = true - } - } - XCTAssertTrue(success) - } - - func testInTransactionInsideInDatabaseIsReentrant() { - var success = false - assertNoError { - try dbQueue.inDatabase { db in - try self.dbQueue.inTransaction { db in - success = true - return .Commit - } - } - } - XCTAssertTrue(success) - } + // CRASH TEST: this test must crash +// func testInDatabaseIsNotReentrant() { +// var success = false +// dbQueue.inDatabase { db in +// self.dbQueue.inDatabase { db in +// success = true +// } +// } +// XCTAssertTrue(success) +// } + // CRASH TEST: this test must crash +// func testInTransactionInsideInDatabaseIsNotReentrant() { +// var success = false +// assertNoError { +// try dbQueue.inDatabase { db in +// try self.dbQueue.inTransaction { db in +// success = true +// return .Commit +// } +// } +// } +// XCTAssertTrue(success) +// } + // CRASH TEST: this test must crash // func testInTransactionIsNotReentrant() { // dbQueue.inTransaction { db in diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 0bea547ba..8c83ae8f4 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -208,11 +208,8 @@ public final class DatabaseQueue { } func inQueue(block: () throws -> R) rethrows -> R { - if databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) { - return try block() - } else { - return try DatabaseQueue.dispatchSync(queue, block: block) - } + assert(databaseQueueID != dispatch_get_specific(DatabaseQueue.databaseQueueIDKey), "DatabaseQueue.inDatabase(_:) or DatabaseQueue.inTransaction(_:) was called reentrantly, which would lead to a deadlock.") + return try DatabaseQueue.dispatchSync(queue, block: block) } // A function declared as rethrows that synchronously executes a throwing