File tree Expand file tree Collapse file tree 1 file changed +39
-1
lines changed
Sources/TestingExtensions Expand file tree Collapse file tree 1 file changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ extension Trait where Self == Testing.ConditionTrait {
41
41
/// Test case usage:
42
42
///
43
43
/// ```swift
44
- /// @Test(.enabledIfShiftOnlyIsDown) func foo async throws {
44
+ /// @Test(.enabledIfShiftOnlyIsDown) func foo() async throws {
45
45
/// // test logic...
46
46
/// }
47
47
/// ```
@@ -50,4 +50,42 @@ extension Trait where Self == Testing.ConditionTrait {
50
50
}
51
51
}
52
52
53
+ // MARK: - System Timing Precision
54
+
55
+ /// Returns `true` if system conditions are suitable for executing tests that rely on precise system timing.
56
+ public func isSystemTimingStable(
57
+ duration: TimeInterval = 0.1 ,
58
+ tolerance: TimeInterval = 0.01
59
+ ) -> Bool {
60
+ let durationMS = UInt32 ( duration * TimeInterval( USEC_PER_SEC) )
61
+
62
+ let start = Date ( )
63
+ usleep ( durationMS)
64
+ let end = Date ( )
65
+ let diff = end. timeIntervalSince ( start)
66
+
67
+ let range = ( duration - tolerance) ... ( duration + tolerance)
68
+ return range. contains ( diff)
69
+ }
70
+
71
+ extension Trait where Self == Testing . ConditionTrait {
72
+ /// Convenience proxy for `.enabled(if: isSystemTimingStable())`.
73
+ ///
74
+ /// Returns `true` if system conditions are suitable for executing tests that rely on precise system timing.
75
+ ///
76
+ /// Test case usage:
77
+ ///
78
+ /// ```swift
79
+ /// @Test(.enabledIfSystemTimingStable()) func foo() async throws {
80
+ /// // test logic...
81
+ /// }
82
+ /// ```
83
+ public static func enabledIfSystemTimingStable(
84
+ duration: TimeInterval = 0.1 ,
85
+ tolerance: TimeInterval = 0.01
86
+ ) -> ConditionTrait {
87
+ . enabled( if: isSystemTimingStable ( duration: duration, tolerance: tolerance) )
88
+ }
89
+ }
90
+
53
91
#endif
You can’t perform that action at this time.
0 commit comments