Skip to content

Commit 2286934

Browse files
committed
Fix regression
Unfortunately #946 introduced a deadlock when `assertSnapshot` is called on the main thread but not the main queue, so let's revert to the old code path for now with an explicit `Test.current` check to avoid the original issue. Fixes #961.
1 parent b2d4cb3 commit 2286934

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

Sources/SnapshotTesting/AssertSnapshot.swift

+13-18
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,13 @@ public func verifySnapshot<Value, Format>(
285285
line: UInt = #line,
286286
column: UInt = #column
287287
) -> String? {
288-
CleanCounterBetweenTestCases.registerIfNeeded()
288+
#if canImport(Testing)
289+
if Test.current == nil {
290+
CleanCounterBetweenTestCases.registerIfNeeded()
291+
}
292+
#else
293+
CleanCounterBetweenTestCases.registerIfNeeded()
294+
#endif
289295

290296
let record =
291297
(recording == true ? .all : recording == false ? .missing : nil)
@@ -523,28 +529,17 @@ func sanitizePathComponent(_ string: String) -> String {
523529
}
524530
#endif
525531

526-
extension DispatchQueue {
527-
private static let key = DispatchSpecificKey<UInt8>()
528-
private static let value: UInt8 = 0
529-
530-
fileprivate static func mainSync<R>(execute block: () -> R) -> R {
531-
Self.main.setSpecific(key: key, value: value)
532-
if getSpecific(key: key) == value {
533-
return block()
534-
} else {
535-
return main.sync(execute: block)
536-
}
537-
}
538-
}
539-
540532
// We need to clean counter between tests executions in order to support test-iterations.
541533
private class CleanCounterBetweenTestCases: NSObject, XCTestObservation {
542534
private static var registered = false
543535

544536
static func registerIfNeeded() {
545-
DispatchQueue.mainSync {
546-
if !registered {
547-
registered = true
537+
guard !registered else { return }
538+
defer { registered = true }
539+
if Thread.isMainThread {
540+
XCTestObservationCenter.shared.addTestObserver(CleanCounterBetweenTestCases())
541+
} else {
542+
DispatchQueue.main.sync {
548543
XCTestObservationCenter.shared.addTestObserver(CleanCounterBetweenTestCases())
549544
}
550545
}

0 commit comments

Comments
 (0)