Compare commits

..
2 changed files with 45 additions and 8 deletions

No files matched your search

-3
View File
@@ -21,9 +21,6 @@
/// 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
@@ -10,6 +10,23 @@ private class NonSendable {
}
}
private class NonSendableRecord:
Codable, Identifiable,
FetchableRecord, PersistableRecord
{
var id: Int64
init(id: Int64) {
self.id = id
}
static func createTable(_ db: Database) throws {
try db.create(table: databaseTableName) { t in
t.autoIncrementedPrimaryKey("id")
}
}
}
// MARK: - Database readers and writers
/// The kinds of database readers whose async methods are tested.
@@ -38,21 +55,25 @@ private func withTemporaryDirectory(
private func withDatabaseReader(
_ kind: ReaderKind,
_ body: (any DatabaseReader) async throws -> Void
setup: (any DatabaseWriter) async throws -> Void = { _ in },
test: (any DatabaseReader) async throws -> Void
) async throws {
switch kind {
case .queue:
try await body(DatabaseQueue())
let dbQueue = try DatabaseQueue()
try await setup(dbQueue)
try await test(dbQueue)
case .pool, .snapshot, .snapshotPool:
try await withTemporaryDirectory { directoryURL in
let dbPool = try DatabasePool(path: directoryURL.appending(path: "db.sqlite").path)
try await setup(dbPool)
switch kind {
case .pool:
try await body(dbPool)
try await test(dbPool)
case .snapshot:
try await body(dbPool.makeSnapshot())
try await test(dbPool.makeSnapshot())
case .snapshotPool:
try await body(dbPool.makeSnapshotPool())
try await test(dbPool.makeSnapshotPool())
case .queue:
fatalError()
}
@@ -211,3 +232,22 @@ private func withDatabaseWriter(
}
}
}
// MARK: - Non-Sendable record results
@Suite struct NonSendableRecordResultTests {
@Test(arguments: ReaderKind.allCases)
func non_sendable_record_returned_from_read(kind: ReaderKind) async throws {
try await withDatabaseReader(kind) { writer in
try await writer.write { db in
try NonSendableRecord.createTable(db)
try NonSendableRecord(id: 1).insert(db)
}
} test: { reader in
let record = try await reader.read { db in
try NonSendableRecord.find(db, id: 1)
}
#expect(record.id == 1)
}
}
}