Skip to content

Commit b24fb76

Browse files
committed
Failing test for task locals with DatabaseWriter (#1793)
1 parent cbbd0cb commit b24fb76

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Tests/GRDBTests/DatabaseWriterTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import XCTest
22
import GRDB
33

4+
@TaskLocal private var localUUID = UUID()
5+
46
class DatabaseWriterTests : GRDBTestCase {
57

68
func testDatabaseQueueUnsafeReentrantWrite() throws {
@@ -413,6 +415,48 @@ class DatabaseWriterTests : GRDBTestCase {
413415
try await test(makeDatabasePool())
414416
}
415417

418+
// MARK: - Task locals
419+
420+
func test_write_can_access_task_local() async throws {
421+
func test<T: DatabaseWriter>(_ dbWriter: T) async throws {
422+
let expectedUUID = UUID()
423+
let dbUUID = try await $localUUID.withValue(expectedUUID) {
424+
try await dbWriter.write { db in localUUID }
425+
}
426+
XCTAssertEqual(dbUUID, expectedUUID)
427+
}
428+
429+
try await test(makeDatabaseQueue())
430+
try await test(makeDatabasePool())
431+
}
432+
433+
func test_writeWithoutTransaction_can_access_task_local() async throws {
434+
func test<T: DatabaseWriter>(_ dbWriter: T) async throws {
435+
let expectedUUID = UUID()
436+
let dbUUID = try await $localUUID.withValue(expectedUUID) {
437+
try await dbWriter.writeWithoutTransaction { db in localUUID }
438+
}
439+
XCTAssertEqual(dbUUID, expectedUUID)
440+
}
441+
442+
try await test(makeDatabaseQueue())
443+
try await test(makeDatabasePool())
444+
}
445+
446+
func test_barrierWriteWithoutTransaction_can_access_task_local() async throws {
447+
func test<T: DatabaseWriter>(_ dbWriter: T) async throws {
448+
let expectedUUID = UUID()
449+
let dbUUID = try await $localUUID.withValue(expectedUUID) {
450+
try await dbWriter.barrierWriteWithoutTransaction { db in localUUID }
451+
}
452+
XCTAssertEqual(dbUUID, expectedUUID)
453+
}
454+
455+
try await test(makeDatabaseQueue())
456+
try await test(makeDatabasePool())
457+
}
458+
459+
416460
// MARK: - Task Cancellation
417461

418462
// Regression test for <https://github.com/groue/GRDB.swift/issues/1715>.

0 commit comments

Comments
 (0)