Skip to content

Commit f27eabb

Browse files
committed
Use IssueHandlingTrait to count issues
1 parent 0aa2374 commit f27eabb

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

Sources/PropertyBased/IssueCounting.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,30 @@ func countIssues(isolation: isolated (any Actor)? = #isolation, suppress: Bool,
1212
{
1313
let found = Mutex(0)
1414

15-
// This is currently the only way to get a callback whenever an issue is found within a block.
15+
#if swift(>=6.2)
16+
nonisolated(unsafe) let closure = perform
17+
18+
let handler = IssueHandlingTrait.filterIssues { _ in
19+
found.withLock { $0 += 1 }
20+
return !suppress
21+
}
22+
23+
try? await handler.provideScope(for: Test.current!, testCase: Test.Case.current) {
24+
try await run(closure, in: isolation)
25+
}
26+
#else
1627
try? await withKnownIssue(isIntermittent: true, isolation: isolation) {
1728
try await perform()
1829
} matching: { _ in
1930
found.withLock { $0 += 1 }
2031
return suppress
2132
}
33+
#endif
2234

2335
return found.withLock { $0 }
2436
}
37+
38+
@inlinable
39+
func run(_ closure: () async throws -> Void, in isolation: isolated (any Actor)?) async throws {
40+
try await closure()
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// ActorTests.swift
3+
// PropertyBased
4+
//
5+
// Created by Lennard Sprong on 08/09/2025.
6+
//
7+
8+
import Testing
9+
10+
@testable import PropertyBased
11+
12+
@globalActor actor OtherActor {
13+
static let shared = OtherActor()
14+
15+
func doNothing() {}
16+
}
17+
18+
@Suite(.shrinking) struct ActorTests {
19+
@MainActor @Test func testOnMainActor() async {
20+
let issues = await countIssues(suppress: true) {
21+
await propertyCheck(input: Gen.int(in: 0...1000)) { i in
22+
await OtherActor.shared.doNothing()
23+
MainActor.assertIsolated("testOnMainActor failure")
24+
#expect(i < 500)
25+
}
26+
}
27+
#expect(issues > 0)
28+
}
29+
30+
@OtherActor @Test func testOnOtherActor() async {
31+
let issues = await countIssues(suppress: true) {
32+
await propertyCheck(input: Gen.int(in: 0...1000)) { i in
33+
await MainActor.run { /* do nothing */ }
34+
OtherActor.assertIsolated("testOnOtherActor failure")
35+
#expect(i < 500)
36+
}
37+
}
38+
#expect(issues > 0)
39+
}
40+
}

0 commit comments

Comments
 (0)