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