1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io;
18
19 import static org.junit.jupiter.api.Assertions.assertNull;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.time.Duration;
24
25 import org.apache.commons.io.test.TestUtils;
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 class ThreadMonitorTest {
32
33
34
35
36 @Test
37 void testCompletedWithoutTimeout() {
38 try {
39 final Thread monitor = ThreadMonitor.start(Duration.ofMillis(400));
40 TestUtils.sleep(1);
41 ThreadMonitor.stop(monitor);
42 } catch (final InterruptedException e) {
43 Thread.currentThread().interrupt();
44 fail("Timed Out", e);
45 }
46 }
47
48
49
50
51 @Test
52 void testNoTimeoutMinus1() {
53
54 try {
55 final Thread monitor = ThreadMonitor.start(Duration.ofMillis(-1));
56 assertNull(monitor, "Timeout -1, Monitor should be null");
57 TestUtils.sleep(100);
58 ThreadMonitor.stop(monitor);
59 } catch (final Exception e) {
60 fail("Timeout -1, threw " + e, e);
61 }
62 }
63
64
65
66
67 @Test
68 void testNoTimeoutZero() {
69
70 try {
71 final Thread monitor = ThreadMonitor.start(Duration.ZERO);
72 assertNull(monitor, "Timeout 0, Monitor should be null");
73 TestUtils.sleep(100);
74 ThreadMonitor.stop(monitor);
75 } catch (final Exception e) {
76 fail("Timeout 0, threw " + e, e);
77 }
78 }
79
80
81
82
83 @Test
84 void testTimeout() {
85 assertThrows(InterruptedException.class, () -> {
86 final Thread monitor = ThreadMonitor.start(Duration.ofMillis(100));
87 TestUtils.sleep(400);
88 ThreadMonitor.stop(monitor);
89 });
90 }
91 }