From eebf8b1c207d75d2fbbc797107b2d3d26870c152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 18 Feb 2024 11:58:14 +0100 Subject: [PATCH] waitForExpectations must be isolated to MainActor --- .../GRDBTests/DatabasePoolConcurrencyTests.swift | 5 +++++ Tests/GRDBTests/DatabaseQueueTests.swift | 2 ++ Tests/GRDBTests/DatabaseReaderTests.swift | 2 ++ .../DatabaseRegionObservationTests.swift | 1 + Tests/GRDBTests/DatabaseWriterTests.swift | 4 ++++ Tests/GRDBTests/PoolTests.swift | 5 +++++ Tests/GRDBTests/ValueObservationPrintTests.swift | 16 ++++++++++++++++ .../ValueObservationRegionRecordingTests.swift | 2 ++ Tests/GRDBTests/ValueObservationTests.swift | 11 +++++++++++ 9 files changed, 48 insertions(+) diff --git a/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift b/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift index 461e2fbc7..daf0c5f20 100644 --- a/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift +++ b/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift @@ -966,6 +966,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { } } + @MainActor func testTargetQueue() throws { func test(targetQueue: DispatchQueue) throws { dbConfiguration.targetQueue = targetQueue @@ -990,6 +991,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { waitForExpectations(timeout: 2, handler: nil) } + @MainActor func testWriteTargetQueue() throws { func test(targetQueue: DispatchQueue, writeTargetQueue: DispatchQueue) throws { dbConfiguration.targetQueue = targetQueue @@ -1214,6 +1216,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { XCTAssertEqual(count, 0) } + @MainActor func testAsyncConcurrentReadError() throws { // Necessary for this test to run as quickly as possible dbConfiguration.readonlyBusyMode = .immediateError @@ -1241,6 +1244,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // MARK: - Barrier + @MainActor func testBarrierLocksReads() throws { let expectation = self.expectation(description: "lock") expectation.isInverted = true @@ -1284,6 +1288,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { XCTAssertEqual(fetchedValue, 1) } + @MainActor func testBarrierIsLockedByOneUnfinishedRead() throws { let expectation = self.expectation(description: "lock") expectation.isInverted = true diff --git a/Tests/GRDBTests/DatabaseQueueTests.swift b/Tests/GRDBTests/DatabaseQueueTests.swift index 81e3d70ce..140fd7f33 100644 --- a/Tests/GRDBTests/DatabaseQueueTests.swift +++ b/Tests/GRDBTests/DatabaseQueueTests.swift @@ -151,6 +151,7 @@ class DatabaseQueueTests: GRDBTestCase { } } + @MainActor func testTargetQueue() throws { func test(targetQueue: DispatchQueue) throws { dbConfiguration.targetQueue = targetQueue @@ -175,6 +176,7 @@ class DatabaseQueueTests: GRDBTestCase { waitForExpectations(timeout: 1, handler: nil) } + @MainActor func testWriteTargetQueue() throws { func test(targetQueue: DispatchQueue, writeTargetQueue: DispatchQueue) throws { dbConfiguration.targetQueue = targetQueue // unused diff --git a/Tests/GRDBTests/DatabaseReaderTests.swift b/Tests/GRDBTests/DatabaseReaderTests.swift index 3905d2282..868e89cb2 100644 --- a/Tests/GRDBTests/DatabaseReaderTests.swift +++ b/Tests/GRDBTests/DatabaseReaderTests.swift @@ -204,6 +204,7 @@ class DatabaseReaderTests : GRDBTestCase { // MARK: - AsyncRead + @MainActor func testAsyncRead() throws { func test(_ dbReader: some DatabaseReader) throws { let expectation = self.expectation(description: "updates") @@ -235,6 +236,7 @@ class DatabaseReaderTests : GRDBTestCase { #endif } + @MainActor func testAsyncReadPreventsDatabaseModification() throws { func test(_ dbReader: some DatabaseReader) throws { let expectation = self.expectation(description: "updates") diff --git a/Tests/GRDBTests/DatabaseRegionObservationTests.swift b/Tests/GRDBTests/DatabaseRegionObservationTests.swift index 22bc79199..67978c12d 100644 --- a/Tests/GRDBTests/DatabaseRegionObservationTests.swift +++ b/Tests/GRDBTests/DatabaseRegionObservationTests.swift @@ -42,6 +42,7 @@ class DatabaseRegionObservationTests: GRDBTestCase { } } + @MainActor func testDatabaseRegionObservation_ImmediateCancellation() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { diff --git a/Tests/GRDBTests/DatabaseWriterTests.swift b/Tests/GRDBTests/DatabaseWriterTests.swift index 9139b1f9e..9f47dcdbe 100644 --- a/Tests/GRDBTests/DatabaseWriterTests.swift +++ b/Tests/GRDBTests/DatabaseWriterTests.swift @@ -39,6 +39,7 @@ class DatabaseWriterTests : GRDBTestCase { } } + @MainActor func testAsyncWriteWithoutTransactionSuccess() throws { func test(_ dbWriter: some DatabaseWriter) throws { let expectation = self.expectation(description: "updates") @@ -64,6 +65,7 @@ class DatabaseWriterTests : GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func testAsyncWriteWithoutTransactionError() throws { func test(_ dbWriter: some DatabaseWriter) throws { let expectation = self.expectation(description: "updates") @@ -89,6 +91,7 @@ class DatabaseWriterTests : GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func testAsyncWriteSuccess() throws { func test(_ dbWriter: some DatabaseWriter) throws { let expectation = self.expectation(description: "updates") @@ -118,6 +121,7 @@ class DatabaseWriterTests : GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func testAsyncWriteError() throws { func test(_ dbWriter: some DatabaseWriter) throws { let expectation = self.expectation(description: "updates") diff --git a/Tests/GRDBTests/PoolTests.swift b/Tests/GRDBTests/PoolTests.swift index b40668634..5d7ee00c5 100644 --- a/Tests/GRDBTests/PoolTests.swift +++ b/Tests/GRDBTests/PoolTests.swift @@ -91,6 +91,7 @@ class PoolTests: XCTestCase { fourth.release(.reuse) } + @MainActor func testBarrierLocksElements() throws { let expectation = self.expectation(description: "lock") expectation.isInverted = true @@ -130,6 +131,7 @@ class PoolTests: XCTestCase { XCTAssertEqual(element, 1) } + @MainActor func testBarrierIsLockedByOneUsedElementOutOfOne() throws { let expectation = self.expectation(description: "lock") expectation.isInverted = true @@ -162,6 +164,7 @@ class PoolTests: XCTestCase { second.release(.reuse) } + @MainActor func testBarrierIsLockedByOneUsedElementOutOfTwo() throws { let expectation = self.expectation(description: "lock") expectation.isInverted = true @@ -199,6 +202,7 @@ class PoolTests: XCTestCase { third.release(.reuse) } + @MainActor func testBarrierIsLockedByTwoUsedElementsOutOfTwo() throws { let expectation = self.expectation(description: "lock") expectation.isInverted = true @@ -234,6 +238,7 @@ class PoolTests: XCTestCase { third.release(.reuse) } + @MainActor func testBarrierRemoveAll() throws { let expectation = self.expectation(description: "lock") expectation.isInverted = true diff --git a/Tests/GRDBTests/ValueObservationPrintTests.swift b/Tests/GRDBTests/ValueObservationPrintTests.swift index 7cbaa820a..b09314d7a 100644 --- a/Tests/GRDBTests/ValueObservationPrintTests.swift +++ b/Tests/GRDBTests/ValueObservationPrintTests.swift @@ -22,6 +22,7 @@ class ValueObservationPrintTests: GRDBTestCase { // MARK: - Readonly + @MainActor func test_readonly_success_asynchronousScheduling() throws { let dbPool = try makeDatabasePool(filename: "test") try dbPool.write { db in @@ -59,6 +60,7 @@ class ValueObservationPrintTests: GRDBTestCase { #endif } + @MainActor func test_readonly_success_immediateScheduling() throws { let dbPool = try makeDatabasePool(filename: "test") try dbPool.write { db in @@ -96,6 +98,7 @@ class ValueObservationPrintTests: GRDBTestCase { #endif } + @MainActor func test_readonly_failure_asynchronousScheduling() throws { struct TestError: Error { } let dbPool = try makeDatabasePool(filename: "test") @@ -134,6 +137,7 @@ class ValueObservationPrintTests: GRDBTestCase { #endif } + @MainActor func test_readonly_failure_immediateScheduling() throws { struct TestError: Error { } let dbPool = try makeDatabasePool(filename: "test") @@ -174,6 +178,7 @@ class ValueObservationPrintTests: GRDBTestCase { // MARK: - Writeonly + @MainActor func test_writeonly_success_asynchronousScheduling() throws { func test(_ dbWriter: some DatabaseWriter) throws { try dbWriter.write { db in @@ -216,6 +221,7 @@ class ValueObservationPrintTests: GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func test_writeonly_success_immediateScheduling() throws { func test(_ dbWriter: some DatabaseWriter) throws { try dbWriter.write { db in @@ -258,6 +264,7 @@ class ValueObservationPrintTests: GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func test_writeonly_immediateFailure_asynchronousScheduling() throws { func test(_ dbWriter: some DatabaseWriter) throws { let logger = TestStream() @@ -285,6 +292,7 @@ class ValueObservationPrintTests: GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func test_writeonly_immediateFailure_immediateScheduling() throws { func test(_ dbWriter: some DatabaseWriter) throws { let logger = TestStream() @@ -312,6 +320,7 @@ class ValueObservationPrintTests: GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func test_writeonly_lateFailure_asynchronousScheduling() throws { func test(_ dbWriter: some DatabaseWriter) throws { try dbWriter.write { db in @@ -355,6 +364,7 @@ class ValueObservationPrintTests: GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func test_writeonly_lateFailure_immediateScheduling() throws { func test(_ dbWriter: some DatabaseWriter) throws { try dbWriter.write { db in @@ -400,6 +410,7 @@ class ValueObservationPrintTests: GRDBTestCase { // MARK: - Concurrent + @MainActor func test_concurrent_success_asynchronousScheduling() throws { let dbPool = try makeDatabasePool() try dbPool.write { db in @@ -453,6 +464,7 @@ class ValueObservationPrintTests: GRDBTestCase { } } + @MainActor func test_concurrent_success_immediateScheduling() throws { let dbPool = try makeDatabasePool() try dbPool.write { db in @@ -504,6 +516,7 @@ class ValueObservationPrintTests: GRDBTestCase { // MARK: - Varying Database Region + @MainActor func test_varyingRegion() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in @@ -560,6 +573,7 @@ class ValueObservationPrintTests: GRDBTestCase { // MARK: - Variations + @MainActor func test_prefix() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in @@ -594,6 +608,7 @@ class ValueObservationPrintTests: GRDBTestCase { } } + @MainActor func test_chain() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in @@ -630,6 +645,7 @@ class ValueObservationPrintTests: GRDBTestCase { } } + @MainActor func test_handleEvents() throws { func waitFor(_ observation: ValueObservation) throws { let dbQueue = try makeDatabaseQueue() diff --git a/Tests/GRDBTests/ValueObservationRegionRecordingTests.swift b/Tests/GRDBTests/ValueObservationRegionRecordingTests.swift index 573b1a3ec..3fa943c3a 100644 --- a/Tests/GRDBTests/ValueObservationRegionRecordingTests.swift +++ b/Tests/GRDBTests/ValueObservationRegionRecordingTests.swift @@ -119,6 +119,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase { onChange: { (int: Int, string: String) in }) // <- destructure } + @MainActor func testVaryingRegionTrackingImmediateScheduling() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { @@ -172,6 +173,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase { } } + @MainActor func testVaryingRegionTrackingAsyncScheduling() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index 27509aacf..e016af8e4 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -28,6 +28,7 @@ class ValueObservationTests: GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func testErrorCompletesTheObservation() throws { struct TestError: Error { } @@ -79,6 +80,7 @@ class ValueObservationTests: GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func testViewOptimization() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { @@ -116,6 +118,7 @@ class ValueObservationTests: GRDBTestCase { } } + @MainActor func testPragmaTableOptimization() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { @@ -324,6 +327,7 @@ class ValueObservationTests: GRDBTestCase { // MARK: - Snapshot Optimization + @MainActor func testDisallowedSnapshotOptimizationWithAsyncScheduler() throws { let dbPool = try makeDatabasePool() try dbPool.write { db in @@ -366,6 +370,7 @@ class ValueObservationTests: GRDBTestCase { } } + @MainActor func testDisallowedSnapshotOptimizationWithImmediateScheduler() throws { let dbPool = try makeDatabasePool() try dbPool.write { db in @@ -408,6 +413,7 @@ class ValueObservationTests: GRDBTestCase { } } + @MainActor func testAllowedSnapshotOptimizationWithAsyncScheduler() throws { let dbPool = try makeDatabasePool() try dbPool.write { db in @@ -460,6 +466,7 @@ class ValueObservationTests: GRDBTestCase { } } + @MainActor func testAllowedSnapshotOptimizationWithImmediateScheduler() throws { let dbPool = try makeDatabasePool() try dbPool.write { db in @@ -584,6 +591,7 @@ class ValueObservationTests: GRDBTestCase { // MARK: - Cancellation + @MainActor func testCancellableLifetime() throws { // We need something to change let dbQueue = try makeDatabaseQueue() @@ -637,6 +645,7 @@ class ValueObservationTests: GRDBTestCase { XCTAssertEqual(context.changesCount, 2) } + @MainActor func testCancellableExplicitCancellation() throws { // We need something to change let dbQueue = try makeDatabaseQueue() @@ -689,6 +698,7 @@ class ValueObservationTests: GRDBTestCase { } } + @MainActor func testCancellableInvalidation1() throws { struct Context { var shouldStopObservation = false @@ -741,6 +751,7 @@ class ValueObservationTests: GRDBTestCase { try test(makeDatabasePool()) } + @MainActor func testCancellableInvalidation2() throws { struct Context { var shouldStopObservation = false