|
1 | 1 | import XCTest |
2 | 2 | import GRDB |
3 | 3 |
|
| 4 | +@TaskLocal private var localUUID = UUID() |
| 5 | + |
4 | 6 | class DatabaseWriterTests : GRDBTestCase { |
5 | 7 |
|
6 | 8 | func testDatabaseQueueUnsafeReentrantWrite() throws { |
@@ -413,6 +415,48 @@ class DatabaseWriterTests : GRDBTestCase { |
413 | 415 | try await test(makeDatabasePool()) |
414 | 416 | } |
415 | 417 |
|
| 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 | + |
416 | 460 | // MARK: - Task Cancellation |
417 | 461 |
|
418 | 462 | // Regression test for <https://github.com/groue/GRDB.swift/issues/1715>. |
|
0 commit comments