15
15
/// - ``Trait/timeLimit(_:)``
16
16
@available ( _clockAPI, * )
17
17
public struct TimeLimitTrait : TestTrait , SuiteTrait {
18
+ /// A type representing the duration of a time limit applied to a test.
19
+ ///
20
+ /// This type is intended for use specifically for specifying test timeouts
21
+ /// with ``TimeLimitTrait``. It is used instead of Swift's built-in `Duration`
22
+ /// type because test timeouts do not support high-precision, arbitrarily
23
+ /// short durations. The smallest allowed unit of time is minutes.
24
+ @_spi ( Experimental)
25
+ public struct Duration : Sendable {
26
+ /// The underlying Swift `Duration` which this time limit duration
27
+ /// represents.
28
+ var underlyingDuration : Swift . Duration
29
+
30
+ /// Construct a time limit duration given a number of minutes.
31
+ ///
32
+ /// - Parameters:
33
+ /// - minutes: The number of minutes the resulting duration should
34
+ /// represent.
35
+ ///
36
+ /// - Returns: A duration representing the specified number of minutes.
37
+ public static func minutes( _ minutes: some BinaryInteger ) -> Self {
38
+ Self ( underlyingDuration: . seconds( 60 ) * minutes)
39
+ }
40
+ }
41
+
18
42
/// The maximum amount of time a test may run for before timing out.
19
- public var timeLimit : Duration
43
+ public var timeLimit : Swift . Duration
20
44
21
45
public var isRecursive : Bool {
22
46
// Since test functions cannot be nested inside other test functions,
@@ -39,18 +63,123 @@ extension Trait where Self == TimeLimitTrait {
39
63
///
40
64
/// - Returns: An instance of ``TimeLimitTrait``.
41
65
///
66
+ /// Test timeouts do not support high-precision, arbitrarily short durations
67
+ /// due to variability in testing environments. The time limit must be at
68
+ /// least one minute, and can only be expressed in increments of one minute.
69
+ ///
42
70
/// When this trait is associated with a test, that test must complete within
43
71
/// a time limit of, at most, `timeLimit`. If the test runs longer, an issue
44
72
/// of kind ``Issue/Kind/timeLimitExceeded(timeLimitComponents:)`` is
45
73
/// recorded. This timeout is treated as a test failure.
46
74
///
75
+ /// The time limit amount specified by `timeLimit` may be reduced if the
76
+ /// testing library is configured to enforce a maximum per-test limit. When
77
+ /// such a maximum is set, the effective time limit of the test this trait is
78
+ /// applied to will be the lesser of `timeLimit` and that maximum. This is a
79
+ /// policy which may be configured on a global basis by the tool responsible
80
+ /// for launching the test process. Refer to that tool's documentation for
81
+ /// more details.
82
+ ///
47
83
/// If a test is parameterized, this time limit is applied to each of its
48
84
/// test cases individually. If a test has more than one time limit associated
49
85
/// with it, the shortest one is used. A test run may also be configured with
50
86
/// a maximum time limit per test case.
51
87
public static func timeLimit( _ timeLimit: Duration ) -> Self {
52
88
return Self ( timeLimit: timeLimit)
53
89
}
90
+
91
+ /// Construct a time limit trait that causes a test to time out if it runs for
92
+ /// too long.
93
+ ///
94
+ /// - Parameters:
95
+ /// - timeLimit: The maximum amount of time the test may run for.
96
+ ///
97
+ /// - Returns: An instance of ``TimeLimitTrait``.
98
+ ///
99
+ /// Test timeouts do not support high-precision, arbitrarily short durations
100
+ /// due to variability in testing environments. The time limit must be at
101
+ /// least one minute, and can only be expressed in increments of one minute.
102
+ ///
103
+ /// When this trait is associated with a test, that test must complete within
104
+ /// a time limit of, at most, `timeLimit`. If the test runs longer, an issue
105
+ /// of kind ``Issue/Kind/timeLimitExceeded(timeLimitComponents:)`` is
106
+ /// recorded. This timeout is treated as a test failure.
107
+ ///
108
+ /// The time limit amount specified by `timeLimit` may be reduced if the
109
+ /// testing library is configured to enforce a maximum per-test limit. When
110
+ /// such a maximum is set, the effective time limit of the test this trait is
111
+ /// applied to will be the lesser of `timeLimit` and that maximum. This is a
112
+ /// policy which may be configured on a global basis by the tool responsible
113
+ /// for launching the test process. Refer to that tool's documentation for
114
+ /// more details.
115
+ ///
116
+ /// If a test is parameterized, this time limit is applied to each of its
117
+ /// test cases individually. If a test has more than one time limit associated
118
+ /// with it, the shortest one is used. A test run may also be configured with
119
+ /// a maximum time limit per test case.
120
+ @_spi ( Experimental)
121
+ public static func timeLimit( _ timeLimit: Self . Duration ) -> Self {
122
+ return Self ( timeLimit: timeLimit. underlyingDuration)
123
+ }
124
+ }
125
+
126
+ @available ( _clockAPI, * )
127
+ extension TimeLimitTrait . Duration {
128
+ /// Construct a time limit duration given a number of seconds.
129
+ ///
130
+ /// This function is unavailable and is provided for diagnostic purposes only.
131
+ @available ( * , unavailable, message: " Time limit must be specified in minutes " )
132
+ public static func seconds( _ seconds: some BinaryInteger ) -> Self {
133
+ fatalError ( " Unsupported " )
134
+ }
135
+
136
+ /// Construct a time limit duration given a number of seconds.
137
+ ///
138
+ /// This function is unavailable and is provided for diagnostic purposes only.
139
+ @available ( * , unavailable, message: " Time limit must be specified in minutes " )
140
+ public static func seconds( _ seconds: Double ) -> Self {
141
+ fatalError ( " Unsupported " )
142
+ }
143
+
144
+ /// Construct a time limit duration given a number of milliseconds.
145
+ ///
146
+ /// This function is unavailable and is provided for diagnostic purposes only.
147
+ @available ( * , unavailable, message: " Time limit must be specified in minutes " )
148
+ public static func milliseconds( _ milliseconds: some BinaryInteger ) -> Self {
149
+ fatalError ( " Unsupported " )
150
+ }
151
+
152
+ /// Construct a time limit duration given a number of milliseconds.
153
+ ///
154
+ /// This function is unavailable and is provided for diagnostic purposes only.
155
+ @available ( * , unavailable, message: " Time limit must be specified in minutes " )
156
+ public static func milliseconds( _ milliseconds: Double ) -> Self {
157
+ fatalError ( " Unsupported " )
158
+ }
159
+
160
+ /// Construct a time limit duration given a number of microseconds.
161
+ ///
162
+ /// This function is unavailable and is provided for diagnostic purposes only.
163
+ @available ( * , unavailable, message: " Time limit must be specified in minutes " )
164
+ public static func microseconds( _ microseconds: some BinaryInteger ) -> Self {
165
+ fatalError ( " Unsupported " )
166
+ }
167
+
168
+ /// Construct a time limit duration given a number of microseconds.
169
+ ///
170
+ /// This function is unavailable and is provided for diagnostic purposes only.
171
+ @available ( * , unavailable, message: " Time limit must be specified in minutes " )
172
+ public static func microseconds( _ microseconds: Double ) -> Self {
173
+ fatalError ( " Unsupported " )
174
+ }
175
+
176
+ /// Construct a time limit duration given a number of nanoseconds.
177
+ ///
178
+ /// This function is unavailable and is provided for diagnostic purposes only.
179
+ @available ( * , unavailable, message: " Time limit must be specified in minutes " )
180
+ public static func nanoseconds( _ nanoseconds: some BinaryInteger ) -> Self {
181
+ fatalError ( " Unsupported " )
182
+ }
54
183
}
55
184
56
185
// MARK: -
0 commit comments