From 22a50a027a53fa7cfceeec2572307c5709cbd504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 17 Feb 2024 11:07:26 +0100 Subject: [PATCH] Tests wait for expections on the MainActor --- .../DatabaseReaderReadPublisherTests.swift | 6 ++++++ .../DatabaseRegionObservationPublisherTests.swift | 2 ++ .../DatabaseWriterWritePublisherTests.swift | 13 +++++++++++++ Tests/GRDBCombineTests/Support.swift | 1 + .../ValueObservationPublisherTests.swift | 11 +++++++++++ Tests/GRDBTests/DatabaseMigratorTests.swift | 12 ++++++++++++ Tests/GRDBTests/ValueObservationTests.swift | 3 +++ 7 files changed, 48 insertions(+) diff --git a/Tests/GRDBCombineTests/DatabaseReaderReadPublisherTests.swift b/Tests/GRDBCombineTests/DatabaseReaderReadPublisherTests.swift index f2b42977c..b734a0016 100644 --- a/Tests/GRDBCombineTests/DatabaseReaderReadPublisherTests.swift +++ b/Tests/GRDBCombineTests/DatabaseReaderReadPublisherTests.swift @@ -21,6 +21,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { // MARK: - + @MainActor func testReadPublisher() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -127,6 +128,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { // frame #70: 0x0000000100002f07 xctest`main + 210 // frame #71: 0x00007fff72311cc9 libdyld.dylib`start + 1 // frame #72: 0x00007fff72311cc9 libdyld.dylib`start + 1 + @MainActor func testReadPublisherError() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -156,6 +158,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { // MARK: - + @MainActor func testReadPublisherIsAsynchronous() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -196,6 +199,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { // MARK: - + @MainActor func testReadPublisherDefaultScheduler() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -236,6 +240,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { // MARK: - + @MainActor func testReadPublisherCustomScheduler() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -277,6 +282,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase { // MARK: - + @MainActor func testReadPublisherIsReadonly() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") diff --git a/Tests/GRDBCombineTests/DatabaseRegionObservationPublisherTests.swift b/Tests/GRDBCombineTests/DatabaseRegionObservationPublisherTests.swift index e126c54b3..f7a81da00 100644 --- a/Tests/GRDBCombineTests/DatabaseRegionObservationPublisherTests.swift +++ b/Tests/GRDBCombineTests/DatabaseRegionObservationPublisherTests.swift @@ -19,6 +19,7 @@ private struct Player: Codable, FetchableRecord, PersistableRecord { class DatabaseRegionObservationPublisherTests : XCTestCase { + @MainActor func testChangesNotifications() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -60,6 +61,7 @@ class DatabaseRegionObservationPublisherTests : XCTestCase { // // TODO: do the same, but asynchronously. If this is too hard, update the // public API so that users can easily do it. + @MainActor func testPrependInitialDatabaseSync() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") diff --git a/Tests/GRDBCombineTests/DatabaseWriterWritePublisherTests.swift b/Tests/GRDBCombineTests/DatabaseWriterWritePublisherTests.swift index 1ab142081..522a660aa 100644 --- a/Tests/GRDBCombineTests/DatabaseWriterWritePublisherTests.swift +++ b/Tests/GRDBCombineTests/DatabaseWriterWritePublisherTests.swift @@ -21,6 +21,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // MARK: - + @MainActor func testWritePublisher() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -48,6 +49,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // MARK: - + @MainActor func testWritePublisherValue() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -75,6 +77,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // MARK: - + @MainActor func testWritePublisherError() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -98,6 +101,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testWritePublisherErrorRollbacksTransaction() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -131,6 +135,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // MARK: - + @MainActor func testWritePublisherIsAsynchronous() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -167,6 +172,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // MARK: - + @MainActor func testWritePublisherDefaultScheduler() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -205,6 +211,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // MARK: - + @MainActor func testWritePublisherCustomScheduler() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -246,6 +253,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // TODO: Fix flaky test with both pool and on-disk queue: // - Expectation timeout + @MainActor func testWriteThenReadPublisher() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -273,6 +281,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // MARK: - + @MainActor func testWriteThenReadPublisherIsReadonly() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -298,6 +307,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // MARK: - + @MainActor func testWriteThenReadPublisherWriteError() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -321,6 +331,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testWriteThenReadPublisherWriteErrorRollbacksTransaction() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -358,6 +369,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // TODO: Fix flaky test with both pool and on-disk queue: // - Expectation timeout + @MainActor func testWriteThenReadPublisherReadError() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -385,6 +397,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase { // Regression test against deadlocks created by concurrent completion // and cancellations triggered by .switchToLatest().prefix(1) + @MainActor func testDeadlockPrevention() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") diff --git a/Tests/GRDBCombineTests/Support.swift b/Tests/GRDBCombineTests/Support.swift index d8f2ec838..05d6abb9c 100644 --- a/Tests/GRDBCombineTests/Support.swift +++ b/Tests/GRDBCombineTests/Support.swift @@ -3,6 +3,7 @@ import Combine import Foundation import XCTest +@MainActor final class Test { // Raise the repeatCount in order to help spotting flaky tests. private let repeatCount: Int diff --git a/Tests/GRDBCombineTests/ValueObservationPublisherTests.swift b/Tests/GRDBCombineTests/ValueObservationPublisherTests.swift index b55fd98c6..aa9a76126 100644 --- a/Tests/GRDBCombineTests/ValueObservationPublisherTests.swift +++ b/Tests/GRDBCombineTests/ValueObservationPublisherTests.swift @@ -21,6 +21,7 @@ class ValueObservationPublisherTests : XCTestCase { // MARK: - Default Scheduler + @MainActor func testDefaultSchedulerChangesNotifications() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -63,6 +64,7 @@ class ValueObservationPublisherTests : XCTestCase { try Test(test).runAtTemporaryDatabasePath { try setUp(DatabasePool(path: $0)) } } + @MainActor func testDefaultSchedulerFirstValueIsEmittedAsynchronously() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -96,6 +98,7 @@ class ValueObservationPublisherTests : XCTestCase { try Test(test).runAtTemporaryDatabasePath { try setUp(DatabasePool(path: $0)) } } + @MainActor func testDefaultSchedulerError() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -122,6 +125,7 @@ class ValueObservationPublisherTests : XCTestCase { // MARK: - Immediate Scheduler + @MainActor func testImmediateSchedulerChangesNotifications() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -164,6 +168,7 @@ class ValueObservationPublisherTests : XCTestCase { try Test(test).runAtTemporaryDatabasePath { try setUp(DatabasePool(path: $0)) } } + @MainActor func testImmediateSchedulerEmitsFirstValueSynchronously() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -200,6 +205,7 @@ class ValueObservationPublisherTests : XCTestCase { try Test(test).runAtTemporaryDatabasePath { try setUp(DatabasePool(path: $0)) } } + @MainActor func testImmediateSchedulerError() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -256,6 +262,7 @@ class ValueObservationPublisherTests : XCTestCase { } } + @MainActor func testDemandNoneReceivesNoElement() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -291,6 +298,7 @@ class ValueObservationPublisherTests : XCTestCase { try Test(test).runAtTemporaryDatabasePath { try setUp(DatabasePool(path: $0)) } } + @MainActor func testDemandOneReceivesOneElement() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -329,6 +337,7 @@ class ValueObservationPublisherTests : XCTestCase { try Test(test).runAtTemporaryDatabasePath { try setUp(DatabasePool(path: $0)) } } + @MainActor func testDemandOneDoesNotReceiveTwoElements() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -371,6 +380,7 @@ class ValueObservationPublisherTests : XCTestCase { try Test(test).runAtTemporaryDatabasePath { try setUp(DatabasePool(path: $0)) } } + @MainActor func testDemandTwoReceivesTwoElements() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -417,6 +427,7 @@ class ValueObservationPublisherTests : XCTestCase { // MARK: - Regression Tests /// Regression test for https://github.com/groue/GRDB.swift/issues/1194 + @MainActor func testIssue1194() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") diff --git a/Tests/GRDBTests/DatabaseMigratorTests.swift b/Tests/GRDBTests/DatabaseMigratorTests.swift index 758d2a8d8..57506bebc 100644 --- a/Tests/GRDBTests/DatabaseMigratorTests.swift +++ b/Tests/GRDBTests/DatabaseMigratorTests.swift @@ -3,6 +3,7 @@ import GRDB class DatabaseMigratorTests : GRDBTestCase { + @MainActor func testEmptyMigratorSync() throws { func test(writer: some DatabaseWriter) throws { let migrator = DatabaseMigrator() @@ -14,6 +15,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testEmptyMigratorAsync() throws { func test(writer: some DatabaseWriter) throws { let expectation = self.expectation(description: "") @@ -34,6 +36,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testEmptyMigratorPublisher() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -51,6 +54,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testNonEmptyMigratorSync() throws { func test(writer: some DatabaseWriter) throws { var migrator = DatabaseMigrator() @@ -95,6 +99,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testNonEmptyMigratorAsync() throws { func test(writer: some DatabaseWriter) throws { var migrator = DatabaseMigrator() @@ -149,6 +154,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testNonEmptyMigratorPublisher() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -205,6 +211,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testEmptyMigratorPublisherIsAsynchronous() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -231,6 +238,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testNonEmptyMigratorPublisherIsAsynchronous() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -258,6 +266,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testMigratorPublisherDefaultScheduler() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -287,6 +296,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testMigratorPublisherCustomScheduler() throws { guard #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) else { throw XCTSkip("Combine is not available") @@ -317,6 +327,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testMigrateUpTo() throws { func test(writer: some DatabaseWriter) throws { var migrator = DatabaseMigrator() @@ -368,6 +379,7 @@ class DatabaseMigratorTests : GRDBTestCase { try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) } } + @MainActor func testMigrationFailureTriggersRollback() throws { var migrator = DatabaseMigrator() migrator.registerMigration("createPersons") { db in diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index 05e401f56..669137c52 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -1034,6 +1034,7 @@ class ValueObservationTests: GRDBTestCase { } // An attempt at finding a regression test for + @MainActor func testManyObservations() throws { // We'll start many observations let observationCount = 100 @@ -1092,6 +1093,7 @@ class ValueObservationTests: GRDBTestCase { } // An attempt at finding a regression test for + @MainActor func testManyObservationsWithLongConcurrentWrite() throws { // We'll start many observations let observationCount = 100 @@ -1157,6 +1159,7 @@ class ValueObservationTests: GRDBTestCase { } // Regression test for + @MainActor func testIssue1362() throws { func test(_ writer: some DatabaseWriter) throws { try writer.write { try $0.execute(sql: "CREATE TABLE s(id INTEGER PRIMARY KEY AUTOINCREMENT)") }