@@ -318,12 +318,9 @@ public func verifySnapshot<Value, Format>(
318
318
if let name = name {
319
319
identifier = sanitizePathComponent ( name)
320
320
} else {
321
- let counter = counterQueue. sync { ( ) -> Int in
322
- let key = snapshotDirectoryUrl. appendingPathComponent ( testName)
323
- counterMap [ key, default: 0 ] += 1
324
- return counterMap [ key] !
325
- }
326
- identifier = String ( counter)
321
+ identifier = String (
322
+ counter. next ( for: snapshotDirectoryUrl. appendingPathComponent ( testName) . absoluteString)
323
+ )
327
324
}
328
325
329
326
let testName = sanitizePathComponent ( testName)
@@ -504,8 +501,19 @@ public func verifySnapshot<Value, Format>(
504
501
505
502
// MARK: - Private
506
503
507
- private let counterQueue = DispatchQueue ( label: " co.pointfree.SnapshotTesting.counter " )
508
- private var counterMap : [ URL : Int ] = [ : ]
504
+ private var counter : File . Counter {
505
+ #if canImport(Testing)
506
+ if Test . current != nil {
507
+ return File . counter
508
+ } else {
509
+ return _counter
510
+ }
511
+ #else
512
+ return _counter
513
+ #endif
514
+ }
515
+
516
+ private let _counter = File . Counter ( )
509
517
510
518
func sanitizePathComponent( _ string: String ) -> String {
511
519
return
@@ -546,8 +554,30 @@ private class CleanCounterBetweenTestCases: NSObject, XCTestObservation {
546
554
}
547
555
548
556
func testCaseDidFinish( _ testCase: XCTestCase ) {
549
- counterQueue. sync {
550
- counterMap = [ : ]
557
+ _counter. reset ( )
558
+ }
559
+ }
560
+
561
+ enum File {
562
+ @TaskLocal static var counter = Counter ( )
563
+
564
+ final class Counter : @unchecked Sendable {
565
+ private var counts : [ String : Int ] = [ : ]
566
+ private let lock = NSLock ( )
567
+
568
+ init ( ) { }
569
+
570
+ func next( for key: String ) -> Int {
571
+ lock. lock ( )
572
+ defer { lock. unlock ( ) }
573
+ counts [ key, default: 0 ] += 1
574
+ return counts [ key] !
575
+ }
576
+
577
+ func reset( ) {
578
+ lock. lock ( )
579
+ defer { lock. unlock ( ) }
580
+ counts. removeAll ( )
551
581
}
552
582
}
553
583
}
0 commit comments