| 1 | /* |
| 2 | * Copyright 2015-2024 the original author or authors. |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | * accompanies this distribution and is available at |
| 7 | * |
| 8 | * https://www.eclipse.org/legal/epl-v20.html |
| 9 | */ |
| 10 | |
| 11 | package org.junit.jupiter.api; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.EXPERIMENTAL; |
| 14 | import static org.apiguardian.api.API.Status.STABLE; |
| 15 | |
| 16 | import java.lang.annotation.Documented; |
| 17 | import java.lang.annotation.ElementType; |
| 18 | import java.lang.annotation.Inherited; |
| 19 | import java.lang.annotation.Retention; |
| 20 | import java.lang.annotation.RetentionPolicy; |
| 21 | import java.lang.annotation.Target; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
| 24 | import org.apiguardian.api.API; |
| 25 | |
| 26 | /** |
| 27 | * {@code @Timeout} is used to define a timeout for a method or all testable |
| 28 | * methods within one class and its {@link Nested @Nested} classes. |
| 29 | * |
| 30 | * <p>This annotation may also be used on lifecycle methods annotated with |
| 31 | * {@link BeforeAll @BeforeAll}, {@link BeforeEach @BeforeEach}, |
| 32 | * {@link AfterEach @AfterEach}, or {@link AfterAll @AfterAll}. |
| 33 | * |
| 34 | * <p>Applying this annotation to a test class has the same effect as applying |
| 35 | * it to all testable methods, i.e. all methods annotated or meta-annotated with |
| 36 | * {@link Test @Test}, {@link TestFactory @TestFactory}, or |
| 37 | * {@link TestTemplate @TestTemplate}, but not to its lifecycle methods. |
| 38 | * |
| 39 | * <h2>Default Timeouts</h2> |
| 40 | * |
| 41 | * <p>If this annotation is not present, no timeout will be used unless a |
| 42 | * default timeout is defined via one of the following configuration parameters: |
| 43 | * |
| 44 | * <dl> |
| 45 | * <dt>{@value #DEFAULT_TIMEOUT_PROPERTY_NAME}</dt> |
| 46 | * <dd>Default timeout for all testable and lifecycle methods</dd> |
| 47 | * <dt>{@value #DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME}</dt> |
| 48 | * <dd>Default timeout for all testable methods</dd> |
| 49 | * <dt>{@value #DEFAULT_TEST_METHOD_TIMEOUT_PROPERTY_NAME}</dt> |
| 50 | * <dd>Default timeout for {@link Test @Test} methods</dd> |
| 51 | * <dt>{@value #DEFAULT_TEST_TEMPLATE_METHOD_TIMEOUT_PROPERTY_NAME}</dt> |
| 52 | * <dd>Default timeout for {@link TestTemplate @TestTemplate} methods</dd> |
| 53 | * <dt>{@value #DEFAULT_TEST_FACTORY_METHOD_TIMEOUT_PROPERTY_NAME}</dt> |
| 54 | * <dd>Default timeout for {@link TestFactory @TestFactory} methods</dd> |
| 55 | * <dt>{@value #DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME}</dt> |
| 56 | * <dd>Default timeout for all lifecycle methods</dd> |
| 57 | * <dt>{@value #DEFAULT_BEFORE_ALL_METHOD_TIMEOUT_PROPERTY_NAME}</dt> |
| 58 | * <dd>Default timeout for {@link BeforeAll @BeforeAll} methods</dd> |
| 59 | * <dt>{@value #DEFAULT_BEFORE_EACH_METHOD_TIMEOUT_PROPERTY_NAME}</dt> |
| 60 | * <dd>Default timeout for {@link BeforeEach @BeforeEach} methods</dd> |
| 61 | * <dt>{@value #DEFAULT_AFTER_EACH_METHOD_TIMEOUT_PROPERTY_NAME}</dt> |
| 62 | * <dd>Default timeout for {@link AfterEach @AfterEach} methods</dd> |
| 63 | * <dt>{@value #DEFAULT_AFTER_ALL_METHOD_TIMEOUT_PROPERTY_NAME}</dt> |
| 64 | * <dd>Default timeout for {@link AfterAll @AfterAll} methods</dd> |
| 65 | * </dl> |
| 66 | * |
| 67 | * <p>More specific configuration parameters override less specific ones. For |
| 68 | * example, {@value #DEFAULT_TEST_METHOD_TIMEOUT_PROPERTY_NAME} |
| 69 | * overrides {@value #DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME} |
| 70 | * which overrides {@value #DEFAULT_TIMEOUT_PROPERTY_NAME}. |
| 71 | * |
| 72 | * <h3 id="supported-values">Supported Values</h3> |
| 73 | * |
| 74 | * <p>Values for timeouts must be in the following, case-insensitive format: |
| 75 | * {@code <number> [ns|μs|ms|s|m|h|d]}. The space between the number and the |
| 76 | * unit may be omitted. Specifying no unit is equivalent to using seconds. |
| 77 | * |
| 78 | * <table class="plain"> |
| 79 | * <caption>Timeout configuration via configuration parameter vs. annotation</caption> |
| 80 | * <tr><th> Value </th><th> Equivalent annotation </th></tr> |
| 81 | * <tr><td> {@code 42} </td><td> {@code @Timeout(42)} </td></tr> |
| 82 | * <tr><td> {@code 42 ns} </td><td> {@code @Timeout(value = 42, unit = NANOSECONDS)} </td></tr> |
| 83 | * <tr><td> {@code 42 μs} </td><td> {@code @Timeout(value = 42, unit = MICROSECONDS)} </td></tr> |
| 84 | * <tr><td> {@code 42 ms} </td><td> {@code @Timeout(value = 42, unit = MILLISECONDS)} </td></tr> |
| 85 | * <tr><td> {@code 42 s} </td><td> {@code @Timeout(value = 42, unit = SECONDS)} </td></tr> |
| 86 | * <tr><td> {@code 42 m} </td><td> {@code @Timeout(value = 42, unit = MINUTES)} </td></tr> |
| 87 | * <tr><td> {@code 42 h} </td><td> {@code @Timeout(value = 42, unit = HOURS)} </td></tr> |
| 88 | * <tr><td> {@code 42 d} </td><td> {@code @Timeout(value = 42, unit = DAYS)} </td></tr> |
| 89 | * </table> |
| 90 | * |
| 91 | * <h2>Disabling Timeouts</h2> |
| 92 | * |
| 93 | * <p>You may use the {@value #TIMEOUT_MODE_PROPERTY_NAME} configuration |
| 94 | * parameter to explicitly enable or disable timeouts. |
| 95 | * |
| 96 | * <p>Supported values: |
| 97 | * <ul> |
| 98 | * <li>{@code enabled}: enables timeouts |
| 99 | * <li>{@code disabled}: disables timeouts |
| 100 | * <li>{@code disabled_on_debug}: disables timeouts while debugging |
| 101 | * </ul> |
| 102 | * |
| 103 | * @since 5.5 |
| 104 | */ |
| 105 | @Target({ ElementType.TYPE, ElementType.METHOD }) |
| 106 | @Retention(RetentionPolicy.RUNTIME) |
| 107 | @Documented |
| 108 | @Inherited |
| 109 | @API(status = STABLE, since = "5.7") |
| 110 | public @interface Timeout { |
| 111 | |
| 112 | /** |
| 113 | * Property name used to set the default timeout for all testable and |
| 114 | * lifecycle methods: {@value}. |
| 115 | * |
| 116 | * <p>The value of this property will be used unless overridden by a more |
| 117 | * specific property or a {@link Timeout @Timeout} |
| 118 | * annotation present on the method or on an enclosing test class (for |
| 119 | * testable methods). |
| 120 | * |
| 121 | * <p>Please refer to the <a href="#supported-values">class |
| 122 | * description</a> for the definition of supported values. |
| 123 | * |
| 124 | * @since 5.5 |
| 125 | */ |
| 126 | @API(status = STABLE, since = "5.9") |
| 127 | String DEFAULT_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.default"; |
| 128 | |
| 129 | /** |
| 130 | * Property name used to set the default timeout for all testable methods: |
| 131 | * {@value}. |
| 132 | * |
| 133 | * <p>The value of this property will be used unless overridden by a more |
| 134 | * specific property or a {@link Timeout @Timeout} |
| 135 | * annotation present on the testable method or on an enclosing test class. |
| 136 | * |
| 137 | * <p>This property overrides the {@value #DEFAULT_TIMEOUT_PROPERTY_NAME} |
| 138 | * property. |
| 139 | * |
| 140 | * <p>Please refer to the <a href="#supported-values">class |
| 141 | * description</a> for the definition of supported values. |
| 142 | * |
| 143 | * @since 5.5 |
| 144 | */ |
| 145 | @API(status = STABLE, since = "5.9") |
| 146 | String DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.testable.method.default"; |
| 147 | |
| 148 | /** |
| 149 | * Property name used to set the default timeout for all {@link Test @Test} |
| 150 | * methods: {@value}. |
| 151 | * |
| 152 | * <p>The value of this property will be used unless overridden by a |
| 153 | * {@link Timeout @Timeout} annotation present on the {@link Test @Test} |
| 154 | * method or on an enclosing test class. |
| 155 | * |
| 156 | * <p>This property overrides the |
| 157 | * {@value #DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME} property. |
| 158 | * |
| 159 | * <p>Please refer to the <a href="#supported-values">class |
| 160 | * description</a> for the definition of supported values. |
| 161 | * |
| 162 | * @since 5.5 |
| 163 | */ |
| 164 | @API(status = STABLE, since = "5.9") |
| 165 | String DEFAULT_TEST_METHOD_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.test.method.default"; |
| 166 | |
| 167 | /** |
| 168 | * Property name used to set the default timeout for all |
| 169 | * {@link TestTemplate @TestTemplate} methods: {@value}. |
| 170 | * |
| 171 | * <p>The value of this property will be used unless overridden by a |
| 172 | * {@link Timeout @Timeout} annotation present on the |
| 173 | * {@link TestTemplate @TestTemplate} method or on an enclosing test class. |
| 174 | * |
| 175 | * <p>This property overrides the |
| 176 | * {@value #DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME} property. |
| 177 | * |
| 178 | * <p>Please refer to the <a href="#supported-values">class |
| 179 | * description</a> for the definition of supported values. |
| 180 | * |
| 181 | * @since 5.5 |
| 182 | */ |
| 183 | @API(status = STABLE, since = "5.9") |
| 184 | String DEFAULT_TEST_TEMPLATE_METHOD_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.testtemplate.method.default"; |
| 185 | |
| 186 | /** |
| 187 | * Property name used to set the default timeout for all |
| 188 | * {@link TestFactory @TestFactory} methods: {@value}. |
| 189 | * |
| 190 | * <p>The value of this property will be used unless overridden by a |
| 191 | * {@link Timeout @Timeout} annotation present on the |
| 192 | * {@link TestFactory @TestFactory} method or on an enclosing test class. |
| 193 | * |
| 194 | * <p>This property overrides the |
| 195 | * {@value #DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME} property. |
| 196 | * |
| 197 | * <p>Please refer to the <a href="#supported-values">class |
| 198 | * description</a> for the definition of supported values. |
| 199 | * |
| 200 | * @since 5.5 |
| 201 | */ |
| 202 | @API(status = STABLE, since = "5.9") |
| 203 | String DEFAULT_TEST_FACTORY_METHOD_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.testfactory.method.default"; |
| 204 | |
| 205 | /** |
| 206 | * Property name used to set the default timeout for all lifecycle methods: |
| 207 | * {@value}. |
| 208 | * |
| 209 | * <p>The value of this property will be used unless overridden by a more |
| 210 | * specific property or a {@link Timeout @Timeout} annotation present on the |
| 211 | * lifecycle method. |
| 212 | * |
| 213 | * <p>This property overrides the {@value #DEFAULT_TIMEOUT_PROPERTY_NAME} |
| 214 | * property. |
| 215 | * |
| 216 | * <p>Please refer to the <a href="#supported-values">class |
| 217 | * description</a> for the definition of supported values. |
| 218 | * |
| 219 | * @since 5.5 |
| 220 | */ |
| 221 | @API(status = STABLE, since = "5.9") |
| 222 | String DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.lifecycle.method.default"; |
| 223 | |
| 224 | /** |
| 225 | * Property name used to set the default timeout for all |
| 226 | * {@link BeforeAll @BeforeAll} methods: {@value}. |
| 227 | * |
| 228 | * <p>The value of this property will be used unless overridden by a |
| 229 | * {@link Timeout @Timeout} annotation present on the |
| 230 | * {@link BeforeAll @BeforeAll} method. |
| 231 | * |
| 232 | * <p>This property overrides the |
| 233 | * {@value #DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME} property. |
| 234 | * |
| 235 | * <p>Please refer to the <a href="#supported-values">class |
| 236 | * description</a> for the definition of supported values. |
| 237 | * |
| 238 | * @since 5.5 |
| 239 | */ |
| 240 | @API(status = STABLE, since = "5.9") |
| 241 | String DEFAULT_BEFORE_ALL_METHOD_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.beforeall.method.default"; |
| 242 | |
| 243 | /** |
| 244 | * Property name used to set the default timeout for all |
| 245 | * {@link BeforeEach @BeforeEach} methods: {@value}. |
| 246 | * |
| 247 | * <p>The value of this property will be used unless overridden by a |
| 248 | * {@link Timeout @Timeout} annotation present on the |
| 249 | * {@link BeforeEach @BeforeEach} method. |
| 250 | * |
| 251 | * <p>This property overrides the |
| 252 | * {@value #DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME} property. |
| 253 | * |
| 254 | * <p>Please refer to the <a href="#supported-values">class |
| 255 | * description</a> for the definition of supported values. |
| 256 | * |
| 257 | * @since 5.5 |
| 258 | */ |
| 259 | @API(status = STABLE, since = "5.9") |
| 260 | String DEFAULT_BEFORE_EACH_METHOD_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.beforeeach.method.default"; |
| 261 | |
| 262 | /** |
| 263 | * Property name used to set the default timeout for all |
| 264 | * {@link AfterEach @AfterEach} methods: {@value}. |
| 265 | * |
| 266 | * <p>The value of this property will be used unless overridden by a |
| 267 | * {@link Timeout @Timeout} annotation present on the |
| 268 | * {@link AfterEach @AfterEach} method. |
| 269 | * |
| 270 | * <p>This property overrides the |
| 271 | * {@value #DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME} property. |
| 272 | * |
| 273 | * <p>Please refer to the <a href="#supported-values">class |
| 274 | * description</a> for the definition of supported values. |
| 275 | * |
| 276 | * @since 5.5 |
| 277 | */ |
| 278 | @API(status = STABLE, since = "5.9") |
| 279 | String DEFAULT_AFTER_EACH_METHOD_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.aftereach.method.default"; |
| 280 | |
| 281 | /** |
| 282 | * Property name used to set the default timeout for all |
| 283 | * {@link AfterAll @AfterAll} methods: {@value}. |
| 284 | * |
| 285 | * <p>The value of this property will be used unless overridden by a |
| 286 | * {@link Timeout @Timeout} annotation present on the |
| 287 | * {@link AfterAll @AfterAll} method. |
| 288 | * |
| 289 | * <p>This property overrides the |
| 290 | * {@value #DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME} property. |
| 291 | * |
| 292 | * <p>Please refer to the <a href="#supported-values">class |
| 293 | * description</a> for the definition of supported values. |
| 294 | * |
| 295 | * @since 5.5 |
| 296 | */ |
| 297 | @API(status = STABLE, since = "5.9") |
| 298 | String DEFAULT_AFTER_ALL_METHOD_TIMEOUT_PROPERTY_NAME = "junit.jupiter.execution.timeout.afterall.method.default"; |
| 299 | |
| 300 | /** |
| 301 | * Property name used to configure whether timeouts are applied to tests: {@value}. |
| 302 | * |
| 303 | * <p>The value of this property will be used to toggle whether |
| 304 | * {@link Timeout @Timeout} is applied to tests.</p> |
| 305 | * |
| 306 | * <h4>Supported timeout mode values:</h4> |
| 307 | * <ul> |
| 308 | * <li>{@code enabled}: enables timeouts |
| 309 | * <li>{@code disabled}: disables timeouts |
| 310 | * <li>{@code disabled_on_debug}: disables timeouts while debugging |
| 311 | * </ul> |
| 312 | * |
| 313 | * <p>If not specified, the default is {@code enabled}. |
| 314 | * |
| 315 | * @since 5.6 |
| 316 | */ |
| 317 | @API(status = STABLE, since = "5.9") |
| 318 | String TIMEOUT_MODE_PROPERTY_NAME = "junit.jupiter.execution.timeout.mode"; |
| 319 | |
| 320 | /** |
| 321 | * Property name used to set the default thread mode for all testable and lifecycle |
| 322 | * methods: "junit.jupiter.execution.timeout.thread.mode.default". |
| 323 | * |
| 324 | * <p>The value of this property will be used unless overridden by a {@link Timeout @Timeout} |
| 325 | * annotation present on the method or on an enclosing test class (for testable methods). |
| 326 | * |
| 327 | * <p>The supported values are {@code SAME_THREAD} or {@code SEPARATE_THREAD}, if none is provided |
| 328 | * {@code SAME_THREAD} is used as default. |
| 329 | * |
| 330 | * @since 5.9 |
| 331 | */ |
| 332 | @API(status = EXPERIMENTAL, since = "5.9") |
| 333 | String DEFAULT_TIMEOUT_THREAD_MODE_PROPERTY_NAME = "junit.jupiter.execution.timeout.thread.mode.default"; |
| 334 | |
| 335 | /** |
| 336 | * The duration of this timeout. |
| 337 | * |
| 338 | * @return timeout duration; must be a positive number |
| 339 | */ |
| 340 | long value(); |
| 341 | |
| 342 | /** |
| 343 | * The time unit of this timeout. |
| 344 | * |
| 345 | * @return time unit |
| 346 | * @see TimeUnit |
| 347 | */ |
| 348 | TimeUnit unit() default TimeUnit.SECONDS; |
| 349 | |
| 350 | /** |
| 351 | * The thread mode of this timeout. |
| 352 | * |
| 353 | * @return thread mode |
| 354 | * @since 5.9 |
| 355 | * @see ThreadMode |
| 356 | */ |
| 357 | @API(status = EXPERIMENTAL, since = "5.9") |
| 358 | ThreadMode threadMode() default ThreadMode.INFERRED; |
| 359 | |
| 360 | /** |
| 361 | * {@code ThreadMode} is use to define whether the test code should be executed in the thread |
| 362 | * of the calling code or in a separated thread. |
| 363 | * |
| 364 | * @since 5.9 |
| 365 | */ |
| 366 | @API(status = EXPERIMENTAL, since = "5.9") |
| 367 | enum ThreadMode { |
| 368 | /** |
| 369 | * The thread mode is determined using the parameter configured in property |
| 370 | * {@value Timeout#DEFAULT_TIMEOUT_THREAD_MODE_PROPERTY_NAME}. |
| 371 | */ |
| 372 | INFERRED, |
| 373 | |
| 374 | /** |
| 375 | * The test code is executed in the thread of the calling code. |
| 376 | */ |
| 377 | SAME_THREAD, |
| 378 | |
| 379 | /** |
| 380 | * The test code is executed in a different thread than that of the calling code. Furthermore, |
| 381 | * execution of the test code will be preemptively aborted if the timeout is exceeded. See the |
| 382 | * {@linkplain Assertions Preemptive Timeouts} section of the class-level |
| 383 | * Javadoc for a discussion of possible undesirable side effects. |
| 384 | */ |
| 385 | SEPARATE_THREAD, |
| 386 | } |
| 387 | |
| 388 | } |