Skip to content

Commit 959c787

Browse files
committed
See if we can fix that flaky test
1 parent 4015d02 commit 959c787

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Tests/SwiftQueryTests/ConcurrentFetchTests.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -466,29 +466,28 @@ struct ConcurrentFetchTests {
466466
}
467467
}
468468

469+
@MainActor
469470
@Test func perform_multipleContextsConcurrentMutation() async throws {
470-
let countOfJacks = 2
471+
let countOfJacks = try countOfJacks()
471472

472473
@Sendable func incrementJacks(by amount: Int) async throws {
473474
try await modelContainer.createQueryActor().perform { actor in
474-
let jacks = try Query<Person>()
475-
.include(#Predicate { $0.name == "Jack"})
476-
.results()
477-
478-
#expect(jacks.count == countOfJacks)
475+
try actor.modelContext.transaction {
476+
let jacks = try Query<Person>()
477+
.include(#Predicate { $0.name == "Jack"})
478+
.results(isolation: actor)
479479

480-
for jack in jacks {
481-
jack.age += amount
480+
for jack in jacks {
481+
jack.age += amount
482+
}
482483
}
483-
484-
try actor.modelContext.save()
485484
}
486485
}
487486

488487
let iterations = 5
489488
let increment = 5
490489

491-
let age = try await jacksAge()
490+
let age = try jacksAge()
492491

493492
try await withThrowingDiscardingTaskGroup { group in
494493
(0..<iterations).forEach { index in
@@ -502,7 +501,7 @@ struct ConcurrentFetchTests {
502501
}
503502
}
504503

505-
let newAge = try await jacksAge()
504+
let newAge = try jacksAge()
506505
let expectedAge = age + (iterations * increment * countOfJacks)
507506

508507
#expect(newAge == expectedAge)
@@ -563,13 +562,19 @@ struct ConcurrentFetchTests {
563562
}
564563

565564
@MainActor
566-
private func jacksAge() async throws -> Int {
565+
private func countOfJacks() throws -> Int {
566+
try Query<Person>()
567+
.include(#Predicate { $0.name == "Jack"})
568+
.count(in: modelContainer)
569+
}
570+
571+
@MainActor
572+
private func jacksAge() throws -> Int {
567573
let jacks = try Query<Person>()
568574
.include(#Predicate { $0.name == "Jack"})
569575
.results(in: modelContainer)
570576
return jacks.reduce(0) { $0 + $1.age }
571577
}
572-
573578
}
574579

575580
@ModelActor

0 commit comments

Comments
 (0)