StrictConcurrency/tests: XCTestCase.waitForExpectations is main-actor isolated

This commit is contained in:
Gwendal Roué committed 2024-07-27 16:37:33 +02:00
1 parent 75f67949ea
commit e28558334f
15 files changed
+86 -8

No files matched your search

@@ -156,6 +156,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase {
// MARK: -
@MainActor
func testReadPublisherIsAsynchronous() throws {
guard #available(iOS 13, macOS 10.15, tvOS 13, *) else {
throw XCTSkip("Combine is not available")
@@ -196,6 +197,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase {
// MARK: -
@MainActor
func testReadPublisherDefaultScheduler() throws {
guard #available(iOS 13, macOS 10.15, tvOS 13, *) else {
throw XCTSkip("Combine is not available")
@@ -236,6 +238,7 @@ class DatabaseReaderReadPublisherTests : XCTestCase {
// MARK: -
@MainActor
func testReadPublisherCustomScheduler() throws {
guard #available(iOS 13, macOS 10.15, tvOS 13, *) else {
throw XCTSkip("Combine is not available")
@@ -60,6 +60,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, *) else {
throw XCTSkip("Combine is not available")
@@ -131,6 +131,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase {
// MARK: -
@MainActor
func testWritePublisherIsAsynchronous() throws {
guard #available(iOS 13, macOS 10.15, tvOS 13, *) else {
throw XCTSkip("Combine is not available")
@@ -167,6 +168,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase {
// MARK: -
@MainActor
func testWritePublisherDefaultScheduler() throws {
guard #available(iOS 13, macOS 10.15, tvOS 13, *) else {
throw XCTSkip("Combine is not available")
@@ -205,6 +207,7 @@ class DatabaseWriterWritePublisherTests : XCTestCase {
// MARK: -
@MainActor
func testWritePublisherCustomScheduler() throws {
guard #available(iOS 13, macOS 10.15, tvOS 13, *) else {
throw XCTSkip("Combine is not available")
@@ -63,6 +63,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, *) else {
throw XCTSkip("Combine is not available")
@@ -256,6 +257,7 @@ class ValueObservationPublisherTests : XCTestCase {
}
}
@MainActor
func testDemandNoneReceivesNoElement() throws {
guard #available(iOS 13, macOS 10.15, tvOS 13, *) else {
throw XCTSkip("Combine is not available")
@@ -291,6 +293,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, *) else {
throw XCTSkip("Combine is not available")
@@ -329,6 +332,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, *) else {
throw XCTSkip("Combine is not available")
@@ -371,6 +375,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, *) else {
throw XCTSkip("Combine is not available")
@@ -417,6 +422,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, *) else {
throw XCTSkip("Combine is not available")
@@ -20,6 +20,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: "")
@@ -101,6 +102,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()
@@ -208,6 +210,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, *) else {
throw XCTSkip("Combine is not available")
@@ -234,6 +237,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, *) else {
throw XCTSkip("Combine is not available")
@@ -261,6 +265,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, *) else {
throw XCTSkip("Combine is not available")
@@ -290,6 +295,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, *) else {
throw XCTSkip("Combine is not available")
@@ -371,6 +377,7 @@ class DatabaseMigratorTests : GRDBTestCase {
try Test(test).runAtTemporaryDatabasePath { try DatabasePool(path: $0) }
}
@MainActor
func testMigrationFailureTriggersRollback() throws {
var migrator = DatabaseMigrator()
migrator.registerMigration("createPersons") { db in
@@ -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
@@ -1076,6 +1078,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
// MARK: - AsyncConcurrentRead
@MainActor
func testAsyncConcurrentReadOpensATransaction() throws {
let dbPool = try makeDatabasePool()
let isInsideTransactionMutex: Mutex<Bool?> = Mutex(nil)
@@ -1100,6 +1103,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
XCTAssertEqual(isInsideTransactionMutex.load(), true)
}
@MainActor
func testAsyncConcurrentReadOutsideOfTransaction() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -1140,6 +1144,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
XCTAssertEqual(countMutex.load(), 0)
}
@MainActor
func testAsyncConcurrentReadError() throws {
// Necessary for this test to run as quickly as possible
dbConfiguration.readonlyBusyMode = .immediateError
@@ -1151,23 +1156,24 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
dbPool.asyncConcurrentRead { dbResult in
guard case let .failure(error) = dbResult,
let dbError = error as? DatabaseError
else {
XCTFail("Unexpected result: \(dbResult)")
return
let dbError = error as? DatabaseError
else {
XCTFail("Unexpected result: \(dbResult)")
return
}
readErrorMutex.store(dbError)
expectation.fulfill()
}
waitForExpectations(timeout: 1, handler: nil)
let readError = try XCTUnwrap(readErrorMutex.load())
XCTAssertEqual(readError.resultCode, .SQLITE_BUSY)
XCTAssertEqual(readError.message, "database is locked")
}
waitForExpectations(timeout: 1, handler: nil)
let readError = try XCTUnwrap(readErrorMutex.load())
XCTAssertEqual(readError.resultCode, .SQLITE_BUSY)
XCTAssertEqual(readError.message, "database is locked")
}
// MARK: - Barrier
@MainActor
func testBarrierLocksReads() throws {
let expectation = self.expectation(description: "lock")
expectation.isInverted = true
@@ -1211,6 +1217,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
XCTAssertEqual(fetchedValue, 1)
}
@MainActor
func testBarrierIsLockedByOneUnfinishedRead() throws {
let expectation = self.expectation(description: "lock")
expectation.isInverted = true
@@ -46,6 +46,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
}
#if os(iOS)
@MainActor
func testDatabasePoolReleasesMemoryOnPressureEvent() throws {
// Create a database pool, and expect a reader connection to be closed
let expectation = self.expectation(description: "Reader connection closed")
@@ -79,6 +80,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
}
}
@MainActor
func testDatabasePoolDoesNotReleaseMemoryOnPressureEventIfDisabled() throws {
// Create a database pool, and do not expect any reader connection to be closed
let expectation = self.expectation(description: "Reader connection closed")
@@ -115,6 +117,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
}
// Regression test for <https://github.com/groue/GRDB.swift/pull/1253#issuecomment-1177166630>
@MainActor
func testDatabasePoolDoesNotPreventConcurrentReadsOnPressureEvent() throws {
let dbPool = try makeDatabasePool()
+2
View File
@@ -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
@@ -220,6 +220,7 @@ class DatabaseReaderTests : GRDBTestCase {
// MARK: - AsyncRead
@MainActor
func testAsyncRead() throws {
func test(_ dbReader: some DatabaseReader) throws {
let expectation = self.expectation(description: "updates")
@@ -249,6 +250,7 @@ class DatabaseReaderTests : GRDBTestCase {
#endif
}
@MainActor
func testAsyncReadPreventsDatabaseModification() throws {
func test(_ dbReader: some DatabaseReader) throws {
let expectation = self.expectation(description: "updates")
@@ -14,6 +14,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
}
@MainActor
func testDatabaseRegionObservation_FullDatabase() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write {
@@ -53,6 +54,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
}
@MainActor
func testDatabaseRegionObservation_ImmediateCancellation() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write {
@@ -80,6 +82,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
}
@MainActor
func testDatabaseRegionObservationVariadic() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write {
@@ -122,6 +125,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
}
@MainActor
func testDatabaseRegionObservationArray() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write {
@@ -164,6 +168,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
}
}
@MainActor
func testDatabaseRegionDefaultCancellation() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") }
@@ -202,6 +207,7 @@ class DatabaseRegionObservationTests: GRDBTestCase {
XCTAssertEqual(count, 2)
}
@MainActor
func testDatabaseRegionExtentNextTransaction() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { try $0.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") }
@@ -48,6 +48,7 @@ class DatabaseWriterTests : GRDBTestCase {
}
}
@MainActor
func testAsyncWriteWithoutTransactionSuccess() throws {
func test(_ dbWriter: some DatabaseWriter) throws {
let expectation = self.expectation(description: "updates")
@@ -73,6 +74,7 @@ class DatabaseWriterTests : GRDBTestCase {
try test(makeDatabasePool())
}
@MainActor
func testAsyncWriteWithoutTransactionError() throws {
func test(_ dbWriter: some DatabaseWriter) throws {
let expectation = self.expectation(description: "updates")
@@ -98,6 +100,7 @@ class DatabaseWriterTests : GRDBTestCase {
try test(makeDatabasePool())
}
@MainActor
func testAsyncWriteSuccess() throws {
func test(_ dbWriter: some DatabaseWriter) throws {
let expectation = self.expectation(description: "updates")
@@ -127,6 +130,7 @@ class DatabaseWriterTests : GRDBTestCase {
try test(makeDatabasePool())
}
@MainActor
func testAsyncWriteError() throws {
func test(_ dbWriter: some DatabaseWriter) throws {
let expectation = self.expectation(description: "updates")
+5
View File
@@ -93,6 +93,7 @@ class PoolTests: XCTestCase {
fourth.release(.reuse)
}
@MainActor
func testBarrierLocksElements() throws {
let expectation = self.expectation(description: "lock")
expectation.isInverted = true
@@ -132,6 +133,7 @@ class PoolTests: XCTestCase {
XCTAssertEqual(element, 1)
}
@MainActor
func testBarrierIsLockedByOneUsedElementOutOfOne() throws {
let expectation = self.expectation(description: "lock")
expectation.isInverted = true
@@ -164,6 +166,7 @@ class PoolTests: XCTestCase {
second.release(.reuse)
}
@MainActor
func testBarrierIsLockedByOneUsedElementOutOfTwo() throws {
let expectation = self.expectation(description: "lock")
expectation.isInverted = true
@@ -201,6 +204,7 @@ class PoolTests: XCTestCase {
third.release(.reuse)
}
@MainActor
func testBarrierIsLockedByTwoUsedElementsOutOfTwo() throws {
let expectation = self.expectation(description: "lock")
expectation.isInverted = true
@@ -236,6 +240,7 @@ class PoolTests: XCTestCase {
third.release(.reuse)
}
@MainActor
func testBarrierRemoveAll() throws {
let expectation = self.expectation(description: "lock")
expectation.isInverted = true
@@ -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
@@ -450,6 +461,7 @@ class ValueObservationPrintTests: GRDBTestCase {
}
}
@MainActor
func test_concurrent_success_immediateScheduling() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -498,6 +510,7 @@ class ValueObservationPrintTests: GRDBTestCase {
// MARK: - Varying Database Region
@MainActor
func test_varyingRegion() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
@@ -554,6 +567,7 @@ class ValueObservationPrintTests: GRDBTestCase {
// MARK: - Variations
@MainActor
func test_prefix() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
@@ -588,6 +602,7 @@ class ValueObservationPrintTests: GRDBTestCase {
}
}
@MainActor
func test_chain() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
@@ -624,6 +639,7 @@ class ValueObservationPrintTests: GRDBTestCase {
}
}
@MainActor
func test_handleEvents() throws {
func waitFor<R: ValueReducer>(_ observation: ValueObservation<R>) throws {
let dbQueue = try makeDatabaseQueue()
@@ -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 {
@@ -174,6 +175,7 @@ class ValueObservationRegionRecordingTests: GRDBTestCase {
}
}
@MainActor
func testVaryingRegionTrackingAsyncScheduling() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write {
@@ -51,6 +51,7 @@ class ValueObservationTests: GRDBTestCase {
try test(makeDatabasePool())
}
@MainActor
func testErrorCompletesTheObservation() throws {
struct TestError: Error { }
@@ -100,6 +101,7 @@ class ValueObservationTests: GRDBTestCase {
try test(makeDatabasePool())
}
@MainActor
func testViewOptimization() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write {
@@ -137,6 +139,7 @@ class ValueObservationTests: GRDBTestCase {
}
}
@MainActor
func testPragmaTableOptimization() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write {
@@ -343,6 +346,7 @@ class ValueObservationTests: GRDBTestCase {
// MARK: - Snapshot Optimization
@MainActor
func testDisallowedSnapshotOptimizationWithAsyncScheduler() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -382,6 +386,7 @@ class ValueObservationTests: GRDBTestCase {
}
}
@MainActor
func testDisallowedSnapshotOptimizationWithImmediateScheduler() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -421,6 +426,7 @@ class ValueObservationTests: GRDBTestCase {
}
}
@MainActor
func testAllowedSnapshotOptimizationWithAsyncScheduler() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -470,6 +476,7 @@ class ValueObservationTests: GRDBTestCase {
}
}
@MainActor
func testAllowedSnapshotOptimizationWithImmediateScheduler() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -591,6 +598,7 @@ class ValueObservationTests: GRDBTestCase {
// MARK: - Cancellation
@MainActor
func testCancellableLifetime() throws {
// We need something to change
let dbQueue = try makeDatabaseQueue()
@@ -638,6 +646,7 @@ class ValueObservationTests: GRDBTestCase {
XCTAssertEqual(changesCountMutex.load(), 2)
}
@MainActor
func testCancellableExplicitCancellation() throws {
// We need something to change
let dbQueue = try makeDatabaseQueue()
@@ -685,6 +694,7 @@ class ValueObservationTests: GRDBTestCase {
}
}
@MainActor
func testCancellableInvalidation1() throws {
struct TestContext {
var cancellable: (any DatabaseCancellable)? = nil
@@ -740,6 +750,7 @@ class ValueObservationTests: GRDBTestCase {
try test(makeDatabasePool())
}
@MainActor
func testCancellableInvalidation2() throws {
struct TestContext {
var cancellable: (any DatabaseCancellable)? = nil