Skip to content

Commit 227cfa1

Browse files
committed
StdlibUnittest: add a simple API for race tests
1 parent ccc318c commit 227cfa1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

stdlib/private/StdlibUnittest/RaceTest.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,31 @@ public func consumeCPU(units amountOfWork: Int) {
573573
}
574574
}
575575
}
576+
577+
internal struct ClosureBasedRaceTest : RaceTestWithPerTrialData {
578+
static var thread: () -> () = {}
579+
580+
class RaceData {}
581+
typealias ThreadLocalData = Void
582+
typealias Observation = Void
583+
584+
func makeRaceData() -> RaceData { return RaceData() }
585+
func makeThreadLocalData() -> Void { return Void() }
586+
587+
func thread1(
588+
_ raceData: RaceData, _ threadLocalData: inout ThreadLocalData
589+
) {
590+
ClosureBasedRaceTest.thread()
591+
}
592+
593+
func evaluateObservations(
594+
_ observations: [Observation],
595+
_ sink: (RaceTestObservationEvaluation) -> Void
596+
) {}
597+
}
598+
599+
public func runRaceTest(trials: Int, threads: Int? = nil, body: () -> ()) {
600+
ClosureBasedRaceTest.thread = body
601+
runRaceTest(ClosureBasedRaceTest.self, trials: trials, threads: threads)
602+
}
603+

validation-test/StdlibUnittest/RaceTest.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ RaceTestSuite.test("fails") {
9292
// CHECK: stdout>>> Failure: 1 times
9393
// CHECK: stdout>>> Failure (65534): 3 times
9494
// CHECK: [ FAIL ] Race.fails
95+
96+
RaceTestSuite.test("closure") {
97+
let count = _stdlib_AtomicInt(0)
98+
runRaceTest(trials: 10) {
99+
_ = count.fetchAndAdd(1)
100+
}
101+
expectNotEqual(0, count.load())
102+
}
103+
// CHECK: [ RUN ] Race.closure
104+
// CHECK: [ OK ] Race.closure
95105
// CHECK: Race: Some tests failed, aborting
96106

97107
runAllTests()

0 commit comments

Comments
 (0)