mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
Simplify Issue1838Tests
... and add it to test targets
This commit is contained in:
1 parent
3ee256e7e8
commit
e281149a2d
3 files changed
+27
-134
No files matched your search
@@ -292,6 +292,7 @@
|
||||
56BB6EA91D3009B100A1CA52 /* SchedulingWatchdog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56BB6EA81D3009B100A1CA52 /* SchedulingWatchdog.swift */; };
|
||||
56BCA2622E6C28F800E4F08D /* DatabaseSchemaSourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56BCA2612E6C28EF00E4F08D /* DatabaseSchemaSourceTests.swift */; };
|
||||
56BF2282241781C5003D86EB /* UtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56BF2281241781C5003D86EB /* UtilsTests.swift */; };
|
||||
56C7979B2EE99BD000AA265D /* Issue1838Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C7979A2EE99BD000AA265D /* Issue1838Tests.swift */; };
|
||||
56CC922C201DFFB900CB597E /* DropWhileCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC922B201DFFB900CB597E /* DropWhileCursorTests.swift */; };
|
||||
56CC9243201E034D00CB597E /* PrefixWhileCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC9242201E034D00CB597E /* PrefixWhileCursorTests.swift */; };
|
||||
56CC9246201E058100CB597E /* DropFirstCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC9245201E058000CB597E /* DropFirstCursorTests.swift */; };
|
||||
@@ -802,6 +803,7 @@
|
||||
56C3F7521CF9F12400F6A361 /* DatabaseSavepointTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSavepointTests.swift; sourceTree = "<group>"; };
|
||||
56C48E731C9A9923005DF1D9 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
|
||||
56C494401ED7255500CC72AF /* GRDBDeploymentTarget.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = GRDBDeploymentTarget.xcconfig; sourceTree = "<group>"; };
|
||||
56C7979A2EE99BD000AA265D /* Issue1838Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Issue1838Tests.swift; sourceTree = "<group>"; };
|
||||
56CC922B201DFFB900CB597E /* DropWhileCursorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropWhileCursorTests.swift; sourceTree = "<group>"; };
|
||||
56CC9242201E034D00CB597E /* PrefixWhileCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrefixWhileCursorTests.swift; sourceTree = "<group>"; };
|
||||
56CC9245201E058000CB597E /* DropFirstCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DropFirstCursorTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1448,6 +1450,7 @@
|
||||
56B964C21DA521450002DA19 /* FTS5TableBuilderTests.swift */,
|
||||
5698ACCA1DA62A2D0056AF8C /* FTS5TokenizerTests.swift */,
|
||||
56ED8A7E1DAB8D6800BD0ABC /* FTS5WrapperTokenizerTests.swift */,
|
||||
56C7979A2EE99BD000AA265D /* Issue1838Tests.swift */,
|
||||
);
|
||||
name = FTS;
|
||||
sourceTree = "<group>";
|
||||
@@ -2130,6 +2133,7 @@
|
||||
56D496C11D81373A008276D7 /* DatabaseQueueBackupTests.swift in Sources */,
|
||||
562393721DEE104400A6B01F /* MapCursorTests.swift in Sources */,
|
||||
56D496571D81303E008276D7 /* FoundationDateComponentsTests.swift in Sources */,
|
||||
56C7979B2EE99BD000AA265D /* Issue1838Tests.swift in Sources */,
|
||||
568C3F7A2A5AB2C300A2309D /* ForeignKeyDefinitionTests.swift in Sources */,
|
||||
56D496701D81309E008276D7 /* RecordPrimaryKeyRowIDTests.swift in Sources */,
|
||||
5622060C1E420EB3005860AC /* DatabaseQueueConcurrencyTests.swift in Sources */,
|
||||
|
||||
@@ -287,6 +287,7 @@
|
||||
56C0539722ACEECD0029D27D /* ValueReducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0538C22ACEECD0029D27D /* ValueReducer.swift */; };
|
||||
56C0539B22ACEECD0029D27D /* Map.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0538E22ACEECD0029D27D /* Map.swift */; };
|
||||
56C0539D22ACEECD0029D27D /* RemoveDuplicates.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0538F22ACEECD0029D27D /* RemoveDuplicates.swift */; };
|
||||
56C7979D2EE99BE900AA265D /* Issue1838Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C7979C2EE99BE900AA265D /* Issue1838Tests.swift */; };
|
||||
56C7A6AE1D2DFF6100EFB0C2 /* FoundationNSDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98E1B6001C600A2F135 /* FoundationNSDateTests.swift */; };
|
||||
56C9EB9B2495324700FE5D55 /* grdb_config.h in Headers */ = {isa = PBXBuildFile; fileRef = 56C9EB9A2495324700FE5D55 /* grdb_config.h */; settings = {ATTRIBUTES = (Private, ); }; };
|
||||
56CC9234201E009000CB597E /* DropWhileCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC9231201DFFF600CB597E /* DropWhileCursorTests.swift */; };
|
||||
@@ -814,6 +815,7 @@
|
||||
56C0538F22ACEECD0029D27D /* RemoveDuplicates.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoveDuplicates.swift; sourceTree = "<group>"; };
|
||||
56C3F7521CF9F12400F6A361 /* DatabaseSavepointTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSavepointTests.swift; sourceTree = "<group>"; };
|
||||
56C494421ED726C500CC72AF /* GRDBDeploymentTarget.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = GRDBDeploymentTarget.xcconfig; path = SQLiteCustom/GRDBDeploymentTarget.xcconfig; sourceTree = "<group>"; };
|
||||
56C7979C2EE99BE900AA265D /* Issue1838Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Issue1838Tests.swift; sourceTree = "<group>"; };
|
||||
56C9EB9A2495324700FE5D55 /* grdb_config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = grdb_config.h; path = SQLiteCustom/grdb_config.h; sourceTree = "<group>"; };
|
||||
56CC9231201DFFF600CB597E /* DropWhileCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DropWhileCursorTests.swift; sourceTree = "<group>"; };
|
||||
56CC923A201E033400CB597E /* PrefixWhileCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrefixWhileCursorTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1441,6 +1443,7 @@
|
||||
5698AC3E1DA2BEBB0056AF8C /* FTS */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56C7979C2EE99BE900AA265D /* Issue1838Tests.swift */,
|
||||
5698AC3F1DA2BED90056AF8C /* FTS3PatternTests.swift */,
|
||||
5698AC481DA2D48A0056AF8C /* FTS3RecordTests.swift */,
|
||||
5698AC881DA389380056AF8C /* FTS3TableBuilderTests.swift */,
|
||||
@@ -2374,6 +2377,7 @@
|
||||
562205FE1E420EA2005860AC /* DatabasePoolConcurrencyTests.swift in Sources */,
|
||||
565EFAF11D0436CE00A8FA9D /* NumericOverflowTests.swift in Sources */,
|
||||
56DF0017228DDB8300D611F3 /* AssociationPrefetchingRowTests.swift in Sources */,
|
||||
56C7979D2EE99BE900AA265D /* Issue1838Tests.swift in Sources */,
|
||||
F3BA812E1CFB3064003DC1BA /* RecordPrimaryKeyMultipleTests.swift in Sources */,
|
||||
F3BA81021CFB3032003DC1BA /* CGFloatTests.swift in Sources */,
|
||||
5676FBAA22F5CEB9004717D9 /* ValueObservationRegionRecordingTests.swift in Sources */,
|
||||
|
||||
@@ -1,142 +1,27 @@
|
||||
import Foundation
|
||||
import GRDB
|
||||
import os
|
||||
import Testing
|
||||
import XCTest
|
||||
|
||||
private struct Person: Codable, TableRecord, FetchableRecord, PersistableRecord {
|
||||
static let databaseTableName = "person"
|
||||
|
||||
var id: Int
|
||||
var name: String
|
||||
|
||||
enum Column: String, CodingKey, ColumnExpression {
|
||||
case id
|
||||
case name
|
||||
}
|
||||
typealias CodingKeys = Column
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
private struct PersonText: Codable, TableRecord, FetchableRecord, PersistableRecord {
|
||||
static let databaseTableName = "person_text"
|
||||
|
||||
let id: Int
|
||||
let content: String
|
||||
|
||||
enum Column: String, CodingKey, ColumnExpression {
|
||||
case id
|
||||
case content
|
||||
}
|
||||
typealias CodingKeys = Column
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
private final class SQLiteStore: Sendable {
|
||||
private let queue: DatabaseQueue
|
||||
|
||||
init(queue: DatabaseQueue) {
|
||||
self.queue = queue
|
||||
}
|
||||
|
||||
convenience init() throws {
|
||||
var configuration = Configuration()
|
||||
configuration.prepareDatabase { db in
|
||||
db.trace { print("SQL", $0) }
|
||||
}
|
||||
let queue = try DatabaseQueue(configuration: configuration)
|
||||
self.init(queue: queue)
|
||||
class Issue1838Tests: GRDBTestCase {
|
||||
/// Regression test for <https://github.com/groue/GRDB.swift/issues/1838>.
|
||||
///
|
||||
/// This test passes since <https://github.com/groue/GRDB.swift/pull/1839>
|
||||
/// which workarounds the SQLite bug described at
|
||||
/// <https://sqlite.org/forum/forumpost/95413eb410>.
|
||||
func test_interrupted_rollback() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("Person/v1") { db in
|
||||
try db.create(table: Person.databaseTableName) { t in
|
||||
t.column(Person.Column.id.name, .integer).notNull().primaryKey()
|
||||
t.column(Person.Column.name.name, .text).notNull()
|
||||
}
|
||||
try db.create(virtualTable: PersonText.databaseTableName, using: FTS5()) { t in
|
||||
t.tokenizer = .porter(wrapping: .unicode61())
|
||||
t.column(PersonText.Column.id.name).notIndexed()
|
||||
t.column(PersonText.Column.content.name)
|
||||
}
|
||||
}
|
||||
try migrator.migrate(queue)
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
class Reader {
|
||||
let database: Database
|
||||
|
||||
init(database: Database) {
|
||||
self.database = database
|
||||
}
|
||||
func fetchPersons() throws -> [Person] {
|
||||
return try Person
|
||||
.fetchAll(database)
|
||||
}
|
||||
func personExists(id: Int) throws -> Bool {
|
||||
return try Person
|
||||
.filter(Person.Column.id == id)
|
||||
.fetchCount(database) > 0
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
final class Writer: Reader {
|
||||
func insert(_ person: Person) throws {
|
||||
try person
|
||||
.insert(database)
|
||||
try PersonText(id: person.id, content: person.name)
|
||||
.insert(database)
|
||||
}
|
||||
func ensure(_ id: Int, inserting person: @autoclosure () -> Person) throws {
|
||||
if try !personExists(id: id) {
|
||||
try insert(person())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
func read<T>(_ body: @Sendable @escaping (Reader) throws -> T) async throws -> T where T: Sendable {
|
||||
return try await queue.read {
|
||||
try body(Reader(database: $0))
|
||||
}
|
||||
}
|
||||
|
||||
func write<T>(_ body: @Sendable @escaping (Writer) throws -> T) async throws -> T where T: Sendable {
|
||||
return try await queue.write {
|
||||
try body(Writer(database: $0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
struct Issue1838Tests {
|
||||
@Test func cancelWrite() async throws {
|
||||
let store = try SQLiteStore()
|
||||
|
||||
let task = Task {
|
||||
do {
|
||||
try await store.write {
|
||||
for i in 1... {
|
||||
try $0.ensure(i, inserting: Person(id: i, name: "person \(i)"))
|
||||
Thread.sleep(forTimeInterval: 0.1)
|
||||
try Task.checkCancellation()
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
print("caught \(error)")
|
||||
throw error
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE VIRTUAL TABLE documents USING FTS5(content)")
|
||||
try db.inTransaction {
|
||||
try db.execute(sql: "INSERT INTO documents(content) VALUES ('Document')")
|
||||
dbQueue.interrupt()
|
||||
return .rollback
|
||||
}
|
||||
}
|
||||
|
||||
try await Task.sleep(for: .seconds(1))
|
||||
|
||||
task.cancel()
|
||||
let _ = await task.result
|
||||
let documentCount = try dbQueue.read { db in
|
||||
try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM documents")!
|
||||
}
|
||||
XCTAssertEqual(documentCount, 0)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user