mirror of
https://github.com/groue/GRDB.swift
synced 2026-12-04 05:08:44 +00:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd5822e8f2 | ||
|
|
d6d6ba2032 | ||
|
|
9688ebb447 | ||
|
|
099b59a7df | ||
|
|
4a1bf09e62 | ||
|
|
f0c0dbf0de | ||
|
|
f2b403e8e8 |
No files matched your search
@@ -152,6 +152,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
|
||||
- **New**: Bump custom SQLite build to version 3.54.1
|
||||
- **New**: SQLCipher package trait by [@GeorgeElsham](https://github.com/GeorgeElsham) and [@groue](https://github.com/groue) in [#1902](https://github.com/groue/GRDB.swift/pull/1902)
|
||||
- **New**: `sum(_:orderBy:filter:)` and `total(_:orderBy:filter:)` are available with the system SQLite, from iOS 26, macOS 26, tvOS 26, watchOS 26, and visionOS 26.
|
||||
- **New**: Async database accesses accept `sending` closures and return `sending` results, instead of requiring `Sendable`.
|
||||
- **Breaking Change**: Raise deployment targets for Xcode 27 compatibility: iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 9.0
|
||||
- **Breaking Change**: Raise minimal Swift version to 6.4 (Xcode 27.0)
|
||||
- **Breaking Change**: Raise minimal SQLite version to 3.36.0
|
||||
|
||||
@@ -351,30 +351,30 @@ extension DatabasePool: DatabaseReader {
|
||||
}
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func read<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
|
||||
return try await readerPool.get { reader in
|
||||
try await reader.execute { db in
|
||||
defer {
|
||||
// Commit or rollback, but make sure we leave the read-only transaction
|
||||
// (commit may fail with a CancellationError).
|
||||
do {
|
||||
try db.commit()
|
||||
} catch {
|
||||
try? db.rollback()
|
||||
}
|
||||
assert(!db.isInsideTransaction)
|
||||
}
|
||||
// The block isolation comes from the DEFERRED transaction.
|
||||
try db.beginTransaction(.deferred)
|
||||
try db.clearSchemaCacheIfNeeded()
|
||||
return try value(db)
|
||||
}
|
||||
let (reader, releaseReader) = try await readerPool.get()
|
||||
defer { releaseReader(.reuse) }
|
||||
return try await reader.execute { db in
|
||||
defer {
|
||||
// Commit or rollback, but make sure we leave the read-only transaction
|
||||
// (commit may fail with a CancellationError).
|
||||
do {
|
||||
try db.commit()
|
||||
} catch {
|
||||
try? db.rollback()
|
||||
}
|
||||
assert(!db.isInsideTransaction)
|
||||
}
|
||||
// The block isolation comes from the DEFERRED transaction.
|
||||
try db.beginTransaction(.deferred)
|
||||
try db.clearSchemaCacheIfNeeded()
|
||||
return try value(db)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -430,18 +430,18 @@ extension DatabasePool: DatabaseReader {
|
||||
}
|
||||
}
|
||||
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func unsafeRead<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
|
||||
return try await readerPool.get { reader in
|
||||
try await reader.execute { db in
|
||||
try db.clearSchemaCacheIfNeeded()
|
||||
return try value(db)
|
||||
}
|
||||
let (reader, releaseReader) = try await readerPool.get()
|
||||
defer { releaseReader(.reuse) }
|
||||
return try await reader.execute { db in
|
||||
try db.clearSchemaCacheIfNeeded()
|
||||
return try value(db)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -787,9 +787,9 @@ extension DatabasePool: DatabaseWriter {
|
||||
try writer.sync(updates)
|
||||
}
|
||||
|
||||
public func writeWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func writeWithoutTransaction<T>(
|
||||
_ updates: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await writer.execute(updates)
|
||||
}
|
||||
|
||||
@@ -802,31 +802,28 @@ extension DatabasePool: DatabaseWriter {
|
||||
}
|
||||
}
|
||||
|
||||
public func barrierWriteWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func barrierWriteWithoutTransaction<T>(
|
||||
_ updates: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
|
||||
// Pool.barrier does not support async calls (yet?).
|
||||
// So we perform cancellation checks just as in
|
||||
// the async version of SerializedDatabase.execute().
|
||||
let cancelMutex = Mutex<(@Sendable () -> Void)?>(nil)
|
||||
return try await withTaskCancellationHandler {
|
||||
try Task.checkCancellation()
|
||||
return try await readerPool.barrier {
|
||||
try Task.checkCancellation()
|
||||
return try await readerPool.barrier {
|
||||
try Task.checkCancellation()
|
||||
return try writer.sync { db in
|
||||
defer {
|
||||
cancelMutex.store(nil)
|
||||
db.uncancel()
|
||||
}
|
||||
cancelMutex.store(db.cancel)
|
||||
try Task.checkCancellation()
|
||||
return try updates(db)
|
||||
var result = Disconnected<T?>(nil)
|
||||
try writer.sync { db in
|
||||
defer {
|
||||
cancelMutex.store(nil)
|
||||
db.uncancel()
|
||||
}
|
||||
cancelMutex.store(db.cancel)
|
||||
try Task.checkCancellation()
|
||||
try result.exchange(newValue: updates(db))
|
||||
}
|
||||
return result.consume()!
|
||||
} onCancel: {
|
||||
cancelMutex.withLock { $0?() }
|
||||
}
|
||||
|
||||
@@ -232,13 +232,15 @@ extension DatabaseQueue: DatabaseReader {
|
||||
}
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func read<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await writer.execute { db in
|
||||
var result = Disconnected<T?>(nil)
|
||||
try db.isolated(readOnly: true) {
|
||||
try value(db)
|
||||
_ = try result.exchange(newValue: value(db))
|
||||
}
|
||||
return result.consume()!
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,9 +278,9 @@ extension DatabaseQueue: DatabaseReader {
|
||||
try writer.sync(value)
|
||||
}
|
||||
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func unsafeRead<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await writer.execute(value)
|
||||
}
|
||||
|
||||
@@ -395,9 +397,9 @@ extension DatabaseQueue: DatabaseWriter {
|
||||
try writer.sync(updates)
|
||||
}
|
||||
|
||||
public func writeWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func writeWithoutTransaction<T>(
|
||||
_ updates: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await writer.execute(updates)
|
||||
}
|
||||
|
||||
@@ -405,9 +407,9 @@ extension DatabaseQueue: DatabaseWriter {
|
||||
try writer.sync(updates)
|
||||
}
|
||||
|
||||
public func barrierWriteWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func barrierWriteWithoutTransaction<T>(
|
||||
_ updates: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await writer.execute(updates)
|
||||
}
|
||||
|
||||
|
||||
@@ -213,9 +213,9 @@ public protocol DatabaseReader: AnyObject, Sendable {
|
||||
/// - throws: Any ``DatabaseError`` that happens while establishing the
|
||||
/// database access, or the error thrown by `value`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T
|
||||
func read<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T
|
||||
|
||||
/// Schedules read-only database operations for execution, and
|
||||
/// returns immediately.
|
||||
@@ -316,9 +316,9 @@ public protocol DatabaseReader: AnyObject, Sendable {
|
||||
/// - throws: Any ``DatabaseError`` that happens while establishing the
|
||||
/// database access, or the error thrown by `value`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T
|
||||
func unsafeRead<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T
|
||||
|
||||
/// Schedules database operations for execution, and returns immediately.
|
||||
///
|
||||
@@ -648,9 +648,9 @@ extension AnyDatabaseReader: DatabaseReader {
|
||||
try base.read(value)
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func read<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await base.read(value)
|
||||
}
|
||||
|
||||
@@ -664,9 +664,9 @@ extension AnyDatabaseReader: DatabaseReader {
|
||||
try base.unsafeRead(value)
|
||||
}
|
||||
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func unsafeRead<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await base.unsafeRead(value)
|
||||
}
|
||||
|
||||
|
||||
@@ -157,9 +157,9 @@ extension DatabaseSnapshot: DatabaseSnapshotReader {
|
||||
try reader.sync(block)
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func read<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await reader.execute(value)
|
||||
}
|
||||
|
||||
@@ -177,9 +177,9 @@ extension DatabaseSnapshot: DatabaseSnapshotReader {
|
||||
// We can't provide this as a default implementation in
|
||||
// `DatabaseSnapshotReader`, because of
|
||||
// <https://github.com/apple/swift/issues/74469>.
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func unsafeRead<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await read(value)
|
||||
}
|
||||
|
||||
|
||||
@@ -297,9 +297,9 @@ extension DatabaseSnapshotPool: DatabaseSnapshotReader {
|
||||
}
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func read<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
@@ -311,12 +311,22 @@ extension DatabaseSnapshotPool: DatabaseSnapshotReader {
|
||||
releaseReader(readerCompletion ?? .reuse)
|
||||
}
|
||||
let (result, completion) = try await reader.execute { db in
|
||||
let result = Result { try value(db) }
|
||||
let result: Result<T, any Error>
|
||||
do {
|
||||
result = try .success(value(db))
|
||||
} catch {
|
||||
result = .failure(error)
|
||||
}
|
||||
let completion = poolCompletion(db)
|
||||
return (result, completion)
|
||||
}
|
||||
readerCompletion = completion
|
||||
return try result.get()
|
||||
switch result {
|
||||
case let .success(output):
|
||||
return output
|
||||
case let .failure(error):
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
public func asyncRead(
|
||||
@@ -345,9 +355,9 @@ extension DatabaseSnapshotPool: DatabaseSnapshotReader {
|
||||
// We can't provide this as a default implementation in
|
||||
// `DatabaseSnapshotReader`, because of
|
||||
// <https://github.com/apple/swift/issues/74469>.
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func unsafeRead<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await read(value)
|
||||
}
|
||||
|
||||
|
||||
@@ -128,9 +128,9 @@ public protocol DatabaseWriter: DatabaseReader {
|
||||
/// - throws: Any ``DatabaseError`` that happens while establishing the
|
||||
/// database access, or the error thrown by `updates`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
func writeWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
) async throws -> T
|
||||
func writeWithoutTransaction<T>(
|
||||
_ updates: sending (Database) throws -> sending T
|
||||
) async throws -> sending T
|
||||
|
||||
/// Executes database operations, and returns their result after they have
|
||||
/// finished executing.
|
||||
@@ -210,9 +210,9 @@ public protocol DatabaseWriter: DatabaseReader {
|
||||
/// - throws: Any ``DatabaseError`` that happens while establishing the
|
||||
/// database access, or the error thrown by `updates`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
func barrierWriteWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
) async throws -> T
|
||||
func barrierWriteWithoutTransaction<T>(
|
||||
_ updates: sending (Database) throws -> sending T
|
||||
) async throws -> sending T
|
||||
|
||||
/// Schedules database operations for execution, and returns immediately.
|
||||
///
|
||||
@@ -608,16 +608,16 @@ extension DatabaseWriter {
|
||||
/// - throws: Any ``DatabaseError`` that happens while establishing the
|
||||
/// database access, or the error thrown by `updates`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
public func write<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func write<T>(
|
||||
_ updates: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await writeWithoutTransaction { db in
|
||||
var result: T?
|
||||
var result = Disconnected<T?>(nil)
|
||||
try db.inTransaction {
|
||||
result = try updates(db)
|
||||
try result.exchange(newValue: updates(db))
|
||||
return .commit
|
||||
}
|
||||
return result!
|
||||
return result.consume()!
|
||||
}
|
||||
}
|
||||
|
||||
@@ -838,9 +838,9 @@ extension AnyDatabaseWriter: DatabaseReader {
|
||||
try base.read(value)
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func read<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await base.read(value)
|
||||
}
|
||||
|
||||
@@ -854,9 +854,9 @@ extension AnyDatabaseWriter: DatabaseReader {
|
||||
try base.unsafeRead(value)
|
||||
}
|
||||
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func unsafeRead<T>(
|
||||
_ value: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await base.unsafeRead(value)
|
||||
}
|
||||
|
||||
@@ -887,9 +887,9 @@ extension AnyDatabaseWriter: DatabaseWriter {
|
||||
try base.writeWithoutTransaction(updates)
|
||||
}
|
||||
|
||||
public func writeWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func writeWithoutTransaction<T>(
|
||||
_ updates: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await base.writeWithoutTransaction(updates)
|
||||
}
|
||||
|
||||
@@ -897,9 +897,9 @@ extension AnyDatabaseWriter: DatabaseWriter {
|
||||
try base.barrierWriteWithoutTransaction(updates)
|
||||
}
|
||||
|
||||
public func barrierWriteWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
public func barrierWriteWithoutTransaction<T>(
|
||||
_ updates: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
try await base.barrierWriteWithoutTransaction(updates)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,9 +19,24 @@ actor DispatchQueueActor {
|
||||
executor.asUnownedSerialExecutor()
|
||||
}
|
||||
|
||||
func execute<T>(_ work: () throws -> T) rethrows -> T {
|
||||
func execute<T>(_ work: () throws -> sending T) rethrows -> sending T {
|
||||
try work()
|
||||
}
|
||||
|
||||
/// Executes `work`, and calls `onCancel` if the current task is
|
||||
/// cancelled during its execution.
|
||||
func execute<T>(
|
||||
_ work: () throws -> sending T,
|
||||
onCancel: sending () -> Void
|
||||
) async rethrows -> sending T {
|
||||
var result = Disconnected<T?>(nil)
|
||||
try await withTaskCancellationHandler {
|
||||
_ = try result.exchange(newValue: work())
|
||||
} onCancel: {
|
||||
onCancel()
|
||||
}
|
||||
return result.consume()!
|
||||
}
|
||||
}
|
||||
|
||||
private final class DispatchQueueExecutor: SerialExecutor {
|
||||
|
||||
@@ -249,21 +249,19 @@ final class SerializedDatabase {
|
||||
}
|
||||
|
||||
/// Asynchronously executes the block.
|
||||
func execute<T: Sendable>(
|
||||
_ block: @Sendable (Database) throws -> T
|
||||
) async throws -> T {
|
||||
func execute<T>(
|
||||
_ block: sending (Database) throws -> sending T
|
||||
) async throws -> sending T {
|
||||
let cancelMutex = Mutex<(@Sendable () -> Void)?>(nil)
|
||||
return try await withTaskCancellationHandler {
|
||||
try await actor.execute {
|
||||
defer {
|
||||
cancelMutex.store(nil)
|
||||
db.uncancel()
|
||||
preconditionNoUnsafeTransactionLeft(db)
|
||||
}
|
||||
cancelMutex.store(db.cancel)
|
||||
try Task.checkCancellation()
|
||||
return try block(db)
|
||||
return try await actor.execute {
|
||||
defer {
|
||||
cancelMutex.store(nil)
|
||||
db.uncancel()
|
||||
preconditionNoUnsafeTransactionLeft(db)
|
||||
}
|
||||
cancelMutex.store(db.cancel)
|
||||
try Task.checkCancellation()
|
||||
return try block(db)
|
||||
} onCancel: {
|
||||
cancelMutex.withLock { $0?() }
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ extension Player: FetchableRecord, PersistableRecord {
|
||||
|
||||
### Non-Sendable Record Types
|
||||
|
||||
In the Swift 6 language mode, and in the Swift 5 language mode with strict concurrency checking, the compiler emits an error or a warning when the application reads, writes, or observes a non-[`Sendable`](https://developer.apple.com/documentation/swift/sendable) type.
|
||||
In the Swift 6 language mode, and in the Swift 5 language mode with strict concurrency checking, the compiler emits an error or a warning when the application reads or observes a non-[`Sendable`](https://developer.apple.com/documentation/swift/sendable) type.
|
||||
|
||||
By default, Swift classes are not Sendable. They are not thread-safe. With GRDB, record classes will typically trigger compiler diagnostics:
|
||||
|
||||
@@ -128,23 +128,27 @@ final class Player: Codable, Identifiable {
|
||||
|
||||
extension Player: FetchableRecord, PersistableRecord { }
|
||||
|
||||
// ❌ Type 'Player' does not conform to the 'Sendable' protocol
|
||||
// ❌ Returning 'db' as a 'sending' result risks causing data races
|
||||
let player = try await writer.read { db in
|
||||
try Player.fetchOne(db, id: 42)
|
||||
}
|
||||
|
||||
// ❌ Capture of 'player' with non-sendable type 'Player' in a `@Sendable` closure
|
||||
let player: Player
|
||||
try await writer.write { db in
|
||||
try player.insert(db)
|
||||
}
|
||||
|
||||
// ❌ Type 'Player' does not conform to the 'Sendable' protocol
|
||||
let observation = ValueObservation.tracking { db in
|
||||
try Player.fetchAll(db)
|
||||
}
|
||||
```
|
||||
|
||||
Writing a non-Sendable record is possible, as long as the record is not used after the write:
|
||||
|
||||
```swift
|
||||
let player: Player
|
||||
try await writer.write { db in
|
||||
try player.insert(db)
|
||||
}
|
||||
// From here, any use of `player` is a compiler error.
|
||||
```
|
||||
|
||||
#### The solution
|
||||
|
||||
The solution is to have the record type conform to `Sendable`.
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2026 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// This file is a modified copy of
|
||||
// <https://github.com/swiftlang/swift/blob/main/stdlib/public/Synchronization/Disconnected.swift>,
|
||||
// distributed under the Apache License v2.0 with Runtime Library Exception
|
||||
// quoted above. The license text is available at
|
||||
// <https://swift.org/LICENSE.txt>.
|
||||
|
||||
/// A wrapper that holds a value in a disconnected isolation region.
|
||||
///
|
||||
/// This is a vendored copy of the `Disconnected` type of the Synchronization
|
||||
/// module, introduced by SE-0538:
|
||||
/// <https://github.com/swiftlang/swift-evolution/blob/main/proposals/0538-disconnected.md>
|
||||
///
|
||||
/// TODO: Remove this copy, and use the Synchronization type, when the
|
||||
/// minimum Swift version is 6.5.
|
||||
struct Disconnected<Value: ~Copyable>: ~Copyable, Sendable {
|
||||
// This is safe since the only values assigned are sent into `Disconnected`.
|
||||
nonisolated(unsafe) var _value: Value
|
||||
|
||||
/// Creates a disconnected wrapper around the given value.
|
||||
@inline(always)
|
||||
init(_ value: consuming sending Value) {
|
||||
self._value = value
|
||||
}
|
||||
|
||||
/// Consumes the wrapper and returns the wrapped value.
|
||||
@inline(always)
|
||||
consuming func consume() -> sending Value {
|
||||
let value = consume _value
|
||||
return value
|
||||
}
|
||||
|
||||
/// Replaces the wrapped value with a new value and returns the
|
||||
/// previous one.
|
||||
@inline(always)
|
||||
@discardableResult
|
||||
mutating func exchange(
|
||||
newValue: consuming sending Value
|
||||
) -> sending Value {
|
||||
let old = consume _value
|
||||
self = Disconnected(newValue)
|
||||
return old
|
||||
}
|
||||
|
||||
/// Calls `body` with mutable access to the wrapped value.
|
||||
///
|
||||
/// If `body` throws, the wrapper retains whatever value the closure
|
||||
/// last left in storage and the error propagates to the caller.
|
||||
@inline(always)
|
||||
mutating func withValue<Return: ~Copyable, Failure>(
|
||||
body: (inout sending Value) throws(Failure) -> Return
|
||||
) throws(Failure) -> Return {
|
||||
var value = consume _value
|
||||
let result: Return
|
||||
do throws(Failure) {
|
||||
result = try body(&value)
|
||||
} catch {
|
||||
self = Disconnected(value)
|
||||
throw error
|
||||
}
|
||||
self = Disconnected(value)
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -154,14 +154,6 @@ final class Pool<T: Sendable>: Sendable {
|
||||
return try block(element)
|
||||
}
|
||||
|
||||
/// Performs an asynchronous block with an element. The element becomes
|
||||
/// available after the block has executed.
|
||||
func get<U>(block: (T) async throws -> U) async throws -> U {
|
||||
let (element, completion) = try await get()
|
||||
defer { completion(.reuse) }
|
||||
return try await block(element)
|
||||
}
|
||||
|
||||
private func release(_ item: Item, completion: PoolCompletion) {
|
||||
contentLock.withLock { content in
|
||||
switch completion {
|
||||
@@ -205,12 +197,18 @@ final class Pool<T: Sendable>: Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
func barrier<R: Sendable>(
|
||||
execute barrier: sending () throws -> sending R
|
||||
/// Runs the `barrier` function when no element is used, and before
|
||||
/// any other element is dequeued. Calls `onCancel` if the current task
|
||||
/// is cancelled during the execution of the barrier.
|
||||
func barrier<R>(
|
||||
execute barrier: sending () throws -> sending R,
|
||||
onCancel: sending () -> Void
|
||||
) async rethrows -> sending R {
|
||||
try await barrierActor.execute {
|
||||
itemsGroup.wait()
|
||||
return try barrier()
|
||||
} onCancel: {
|
||||
onCancel()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import Foundation
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
@@ -9,7 +10,73 @@ private class NonSendable {
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct NonSendableTests {
|
||||
// MARK: - Database readers and writers
|
||||
|
||||
/// The kinds of database readers whose async methods are tested.
|
||||
enum ReaderKind: CaseIterable {
|
||||
case queue
|
||||
case pool
|
||||
case snapshot
|
||||
case snapshotPool
|
||||
}
|
||||
|
||||
/// The kinds of database writers whose async methods are tested.
|
||||
enum WriterKind: CaseIterable {
|
||||
case queue
|
||||
case pool
|
||||
}
|
||||
|
||||
private func withTemporaryDirectory(
|
||||
_ body: (URL) async throws -> Void
|
||||
) async throws {
|
||||
let directoryURL = URL.temporaryDirectory
|
||||
.appending(path: "GRDBNonSendableTests-\(UUID().uuidString)", directoryHint: .isDirectory)
|
||||
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true)
|
||||
defer { try? FileManager.default.removeItem(at: directoryURL) }
|
||||
try await body(directoryURL)
|
||||
}
|
||||
|
||||
private func withDatabaseReader(
|
||||
_ kind: ReaderKind,
|
||||
_ body: (any DatabaseReader) async throws -> Void
|
||||
) async throws {
|
||||
switch kind {
|
||||
case .queue:
|
||||
try await body(DatabaseQueue())
|
||||
case .pool, .snapshot, .snapshotPool:
|
||||
try await withTemporaryDirectory { directoryURL in
|
||||
let dbPool = try DatabasePool(path: directoryURL.appending(path: "db.sqlite").path)
|
||||
switch kind {
|
||||
case .pool:
|
||||
try await body(dbPool)
|
||||
case .snapshot:
|
||||
try await body(dbPool.makeSnapshot())
|
||||
case .snapshotPool:
|
||||
try await body(dbPool.makeSnapshotPool())
|
||||
case .queue:
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func withDatabaseWriter(
|
||||
_ kind: WriterKind,
|
||||
_ body: (any DatabaseWriter) async throws -> Void
|
||||
) async throws {
|
||||
switch kind {
|
||||
case .queue:
|
||||
try await body(DatabaseQueue())
|
||||
case .pool:
|
||||
try await withTemporaryDirectory { directoryURL in
|
||||
try await body(DatabasePool(path: directoryURL.appending(path: "db.sqlite").path))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Non-Sendable inputs
|
||||
|
||||
@Suite struct NonSendableInputTests {
|
||||
@Test func non_sendable_type_in_synchronous_database_access() throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let input = NonSendable(value: 0)
|
||||
@@ -19,58 +86,128 @@ private class NonSendable {
|
||||
#expect(input === output)
|
||||
}
|
||||
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseReader_read() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.read { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseReader_unsafeRead() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.unsafeRead { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseWriter_barrierWriteWithoutTransaction() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.barrierWriteWithoutTransaction { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseWriter_write() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.write { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseWriter_writeWithoutTransaction() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.writeWithoutTransaction { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseReader_read(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await reader.read { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseReader_unsafeRead(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await reader.unsafeRead { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseWriter_barrierWriteWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await writer.barrierWriteWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseWriter_write(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await writer.write { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseWriter_writeWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await writer.writeWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Non-Sendable results
|
||||
|
||||
@Suite struct NonSendableResultTests {
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseReader_read(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = try await reader.read { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseReader_unsafeRead(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = try await reader.unsafeRead { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseWriter_barrierWriteWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = try await writer.barrierWriteWithoutTransaction { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseWriter_write(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = try await writer.write { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseWriter_writeWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = try await writer.writeWithoutTransaction { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_sent_to_and_returned_from_databaseReader_read(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let input = NonSendable(value: 0)
|
||||
let output = try await reader.read { _ in
|
||||
NonSendable(value: input.value + 1)
|
||||
}
|
||||
#expect(output.value == 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import Foundation
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
@@ -9,7 +10,73 @@ nonisolated private class NonSendable {
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct NonSendableTests {
|
||||
// MARK: - Database readers and writers
|
||||
|
||||
/// The kinds of database readers whose async methods are tested.
|
||||
nonisolated enum ReaderKind: CaseIterable {
|
||||
case queue
|
||||
case pool
|
||||
case snapshot
|
||||
case snapshotPool
|
||||
}
|
||||
|
||||
/// The kinds of database writers whose async methods are tested.
|
||||
nonisolated enum WriterKind: CaseIterable {
|
||||
case queue
|
||||
case pool
|
||||
}
|
||||
|
||||
private func withTemporaryDirectory(
|
||||
_ body: (URL) async throws -> Void
|
||||
) async throws {
|
||||
let directoryURL = URL.temporaryDirectory
|
||||
.appending(path: "GRDBNonSendableTests-\(UUID().uuidString)", directoryHint: .isDirectory)
|
||||
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true)
|
||||
defer { try? FileManager.default.removeItem(at: directoryURL) }
|
||||
try await body(directoryURL)
|
||||
}
|
||||
|
||||
private func withDatabaseReader(
|
||||
_ kind: ReaderKind,
|
||||
_ body: (any DatabaseReader) async throws -> Void
|
||||
) async throws {
|
||||
switch kind {
|
||||
case .queue:
|
||||
try await body(DatabaseQueue())
|
||||
case .pool, .snapshot, .snapshotPool:
|
||||
try await withTemporaryDirectory { directoryURL in
|
||||
let dbPool = try DatabasePool(path: directoryURL.appending(path: "db.sqlite").path)
|
||||
switch kind {
|
||||
case .pool:
|
||||
try await body(dbPool)
|
||||
case .snapshot:
|
||||
try await body(dbPool.makeSnapshot())
|
||||
case .snapshotPool:
|
||||
try await body(dbPool.makeSnapshotPool())
|
||||
case .queue:
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func withDatabaseWriter(
|
||||
_ kind: WriterKind,
|
||||
_ body: (any DatabaseWriter) async throws -> Void
|
||||
) async throws {
|
||||
switch kind {
|
||||
case .queue:
|
||||
try await body(DatabaseQueue())
|
||||
case .pool:
|
||||
try await withTemporaryDirectory { directoryURL in
|
||||
try await body(DatabasePool(path: directoryURL.appending(path: "db.sqlite").path))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Non-Sendable inputs
|
||||
|
||||
@Suite struct NonSendableInputTests {
|
||||
@Test func non_sendable_type_in_synchronous_database_access() throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let input = NonSendable(value: 0)
|
||||
@@ -19,58 +86,128 @@ nonisolated private class NonSendable {
|
||||
#expect(input === output)
|
||||
}
|
||||
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseReader_read() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.read { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseReader_unsafeRead() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.unsafeRead { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseWriter_barrierWriteWithoutTransaction() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.barrierWriteWithoutTransaction { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseWriter_write() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.write { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseWriter_writeWithoutTransaction() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.writeWithoutTransaction { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseReader_read(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await reader.read { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseReader_unsafeRead(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await reader.unsafeRead { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseWriter_barrierWriteWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await writer.barrierWriteWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseWriter_write(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await writer.write { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseWriter_writeWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await writer.writeWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Non-Sendable results
|
||||
|
||||
@Suite struct NonSendableResultTests {
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseReader_read(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = try await reader.read { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseReader_unsafeRead(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = try await reader.unsafeRead { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseWriter_barrierWriteWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = try await writer.barrierWriteWithoutTransaction { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseWriter_write(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = try await writer.write { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseWriter_writeWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = try await writer.writeWithoutTransaction { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_sent_to_and_returned_from_databaseReader_read(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let input = NonSendable(value: 0)
|
||||
let output = try await reader.read { _ in
|
||||
NonSendable(value: input.value + 1)
|
||||
}
|
||||
#expect(output.value == 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import Foundation
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
@@ -9,7 +10,73 @@ private class NonSendable {
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct NonSendableTests {
|
||||
// MARK: - Database readers and writers
|
||||
|
||||
/// The kinds of database readers whose async methods are tested.
|
||||
enum ReaderKind: CaseIterable {
|
||||
case queue
|
||||
case pool
|
||||
case snapshot
|
||||
case snapshotPool
|
||||
}
|
||||
|
||||
/// The kinds of database writers whose async methods are tested.
|
||||
enum WriterKind: CaseIterable {
|
||||
case queue
|
||||
case pool
|
||||
}
|
||||
|
||||
private func withTemporaryDirectory(
|
||||
_ body: (URL) async throws -> Void
|
||||
) async throws {
|
||||
let directoryURL = URL.temporaryDirectory
|
||||
.appending(path: "GRDBNonSendableTests-\(UUID().uuidString)", directoryHint: .isDirectory)
|
||||
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true)
|
||||
defer { try? FileManager.default.removeItem(at: directoryURL) }
|
||||
try await body(directoryURL)
|
||||
}
|
||||
|
||||
private func withDatabaseReader(
|
||||
_ kind: ReaderKind,
|
||||
_ body: (any DatabaseReader) async throws -> Void
|
||||
) async throws {
|
||||
switch kind {
|
||||
case .queue:
|
||||
try await body(DatabaseQueue())
|
||||
case .pool, .snapshot, .snapshotPool:
|
||||
try await withTemporaryDirectory { directoryURL in
|
||||
let dbPool = try DatabasePool(path: directoryURL.appending(path: "db.sqlite").path)
|
||||
switch kind {
|
||||
case .pool:
|
||||
try await body(dbPool)
|
||||
case .snapshot:
|
||||
try await body(dbPool.makeSnapshot())
|
||||
case .snapshotPool:
|
||||
try await body(dbPool.makeSnapshotPool())
|
||||
case .queue:
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func withDatabaseWriter(
|
||||
_ kind: WriterKind,
|
||||
_ body: (any DatabaseWriter) async throws -> Void
|
||||
) async throws {
|
||||
switch kind {
|
||||
case .queue:
|
||||
try await body(DatabaseQueue())
|
||||
case .pool:
|
||||
try await withTemporaryDirectory { directoryURL in
|
||||
try await body(DatabasePool(path: directoryURL.appending(path: "db.sqlite").path))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Non-Sendable inputs
|
||||
|
||||
@Suite struct NonSendableInputTests {
|
||||
@Test func non_sendable_type_in_synchronous_database_access() throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let input = NonSendable(value: 0)
|
||||
@@ -19,58 +86,128 @@ private class NonSendable {
|
||||
#expect(input === output)
|
||||
}
|
||||
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseReader_read() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.read { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseReader_unsafeRead() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.unsafeRead { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseWriter_barrierWriteWithoutTransaction() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.barrierWriteWithoutTransaction { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseWriter_write() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.write { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
//
|
||||
// Does not compile.
|
||||
// @Test func non_sendable_type_sent_to_databaseWriter_writeWithoutTransaction() async throws {
|
||||
// let dbQueue = try DatabaseQueue()
|
||||
// let nonSendable = NonSendable(value: 0)
|
||||
// let value = try await dbQueue.writeWithoutTransaction { _ in
|
||||
// // error: capture of 'nonSendable' with non-Sendable type 'NonSendable' in a '@Sendable' closure
|
||||
// nonSendable.value
|
||||
// }
|
||||
// #expect(value == 0)
|
||||
// }
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseReader_read(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await reader.read { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseReader_unsafeRead(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await reader.unsafeRead { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseWriter_barrierWriteWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await writer.barrierWriteWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseWriter_write(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await writer.write { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_sent_to_databaseWriter_writeWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await writer.writeWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Non-Sendable results
|
||||
|
||||
@Suite struct NonSendableResultTests {
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseReader_read(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = try await reader.read { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseReader_unsafeRead(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let nonSendable = try await reader.unsafeRead { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseWriter_barrierWriteWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = try await writer.barrierWriteWithoutTransaction { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseWriter_write(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = try await writer.write { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: WriterKind.allCases)
|
||||
func non_sendable_type_returned_from_databaseWriter_writeWithoutTransaction(kind: WriterKind) async throws {
|
||||
try await withDatabaseWriter(kind) { writer in
|
||||
let nonSendable = try await writer.writeWithoutTransaction { _ in
|
||||
NonSendable(value: 0)
|
||||
}
|
||||
nonSendable.value += 1
|
||||
#expect(nonSendable.value == 1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: ReaderKind.allCases)
|
||||
func non_sendable_type_sent_to_and_returned_from_databaseReader_read(kind: ReaderKind) async throws {
|
||||
try await withDatabaseReader(kind) { reader in
|
||||
let input = NonSendable(value: 0)
|
||||
let output = try await reader.read { _ in
|
||||
NonSendable(value: input.value + 1)
|
||||
}
|
||||
#expect(output.value == 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user