|
22 | 22 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
23 | 23 | import static org.junit.jupiter.api.Assertions.fail;
|
24 | 24 |
|
| 25 | +import java.time.Duration; |
25 | 26 | import java.util.concurrent.CountDownLatch;
|
26 | 27 | import java.util.concurrent.atomic.AtomicBoolean;
|
27 | 28 | import java.util.concurrent.atomic.AtomicReference;
|
|
37 | 38 | */
|
38 | 39 | class AssertTimeoutAssertionsTests {
|
39 | 40 |
|
| 41 | + private static final Duration PREEMPTIVE_TIMEOUT = ofMillis(1000); |
| 42 | + |
40 | 43 | private static ThreadLocal<AtomicBoolean> changed = ThreadLocal.withInitial(() -> new AtomicBoolean(false));
|
41 | 44 |
|
42 | 45 | private final Executable nix = () -> {
|
@@ -189,26 +192,29 @@ void assertTimeoutPreemptivelyForExecutableThatThrowsAnAssertionFailedError() {
|
189 | 192 | @Test
|
190 | 193 | void assertTimeoutPreemptivelyForExecutableThatCompletesAfterTheTimeout() {
|
191 | 194 | AssertionFailedError error = assertThrows(AssertionFailedError.class,
|
192 |
| - () -> assertTimeoutPreemptively(ofMillis(10), this::waitForInterrupt)); |
193 |
| - assertMessageEquals(error, "execution timed out after 10 ms"); |
| 195 | + () -> assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, this::waitForInterrupt)); |
| 196 | + assertMessageEquals(error, "execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms"); |
194 | 197 | assertMessageStartsWith(error.getCause(), "Execution timed out in ");
|
195 | 198 | assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
|
196 | 199 | }
|
197 | 200 |
|
198 | 201 | @Test
|
199 | 202 | void assertTimeoutPreemptivelyWithMessageForExecutableThatCompletesAfterTheTimeout() {
|
200 | 203 | AssertionFailedError error = assertThrows(AssertionFailedError.class,
|
201 |
| - () -> assertTimeoutPreemptively(ofMillis(10), this::waitForInterrupt, "Tempus Fugit")); |
202 |
| - assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms"); |
| 204 | + () -> assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, this::waitForInterrupt, "Tempus Fugit")); |
| 205 | + assertMessageEquals(error, |
| 206 | + "Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms"); |
203 | 207 | assertMessageStartsWith(error.getCause(), "Execution timed out in ");
|
204 | 208 | assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
|
205 | 209 | }
|
206 | 210 |
|
207 | 211 | @Test
|
208 | 212 | void assertTimeoutPreemptivelyWithMessageSupplierForExecutableThatCompletesAfterTheTimeout() {
|
209 | 213 | AssertionFailedError error = assertThrows(AssertionFailedError.class,
|
210 |
| - () -> assertTimeoutPreemptively(ofMillis(10), this::waitForInterrupt, () -> "Tempus" + " " + "Fugit")); |
211 |
| - assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms"); |
| 214 | + () -> assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, this::waitForInterrupt, |
| 215 | + () -> "Tempus" + " " + "Fugit")); |
| 216 | + assertMessageEquals(error, |
| 217 | + "Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms"); |
212 | 218 | assertMessageStartsWith(error.getCause(), "Execution timed out in ");
|
213 | 219 | assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
|
214 | 220 | }
|
@@ -258,38 +264,43 @@ void assertTimeoutPreemptivelyForSupplierThatThrowsAnAssertionFailedError() {
|
258 | 264 | @Test
|
259 | 265 | void assertTimeoutPreemptivelyForSupplierThatCompletesAfterTheTimeout() {
|
260 | 266 | AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> {
|
261 |
| - assertTimeoutPreemptively(ofMillis(10), () -> { |
| 267 | + assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, () -> { |
262 | 268 | waitForInterrupt();
|
263 | 269 | return "Tempus Fugit";
|
264 | 270 | });
|
265 | 271 | });
|
266 |
| - assertMessageEquals(error, "execution timed out after 10 ms"); |
| 272 | + |
| 273 | + assertMessageEquals(error, "execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms"); |
267 | 274 | assertMessageStartsWith(error.getCause(), "Execution timed out in ");
|
268 | 275 | assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
|
269 | 276 | }
|
270 | 277 |
|
271 | 278 | @Test
|
272 | 279 | void assertTimeoutPreemptivelyWithMessageForSupplierThatCompletesAfterTheTimeout() {
|
273 | 280 | AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> {
|
274 |
| - assertTimeoutPreemptively(ofMillis(10), () -> { |
| 281 | + assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, () -> { |
275 | 282 | waitForInterrupt();
|
276 | 283 | return "Tempus Fugit";
|
277 | 284 | }, "Tempus Fugit");
|
278 | 285 | });
|
279 |
| - assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms"); |
| 286 | + |
| 287 | + assertMessageEquals(error, |
| 288 | + "Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms"); |
280 | 289 | assertMessageStartsWith(error.getCause(), "Execution timed out in ");
|
281 | 290 | assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
|
282 | 291 | }
|
283 | 292 |
|
284 | 293 | @Test
|
285 | 294 | void assertTimeoutPreemptivelyWithMessageSupplierForSupplierThatCompletesAfterTheTimeout() {
|
286 | 295 | AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> {
|
287 |
| - assertTimeoutPreemptively(ofMillis(10), () -> { |
| 296 | + assertTimeoutPreemptively(PREEMPTIVE_TIMEOUT, () -> { |
288 | 297 | waitForInterrupt();
|
289 | 298 | return "Tempus Fugit";
|
290 | 299 | }, () -> "Tempus" + " " + "Fugit");
|
291 | 300 | });
|
292 |
| - assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms"); |
| 301 | + |
| 302 | + assertMessageEquals(error, |
| 303 | + "Tempus Fugit ==> execution timed out after " + PREEMPTIVE_TIMEOUT.toMillis() + " ms"); |
293 | 304 | assertMessageStartsWith(error.getCause(), "Execution timed out in ");
|
294 | 305 | assertStackTraceContains(error.getCause().getStackTrace(), "CountDownLatch", "await");
|
295 | 306 | }
|
|
0 commit comments