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 public class ThreadMonitorTest {
32
33
34
35
36 @Test
37 public 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 fail("Timed Out", e);
44 }
45 }
46
47
48
49
50 @Test
51 public void testNoTimeoutMinus1() {
52
53 try {
54 final Thread monitor = ThreadMonitor.start(Duration.ofMillis(-1));
55 assertNull(monitor, "Timeout -1, Monitor should be null");
56 TestUtils.sleep(100);
57 ThreadMonitor.stop(monitor);
58 } catch (final Exception e) {
59 fail("Timeout -1, threw " + e, e);
60 }
61 }
62
63
64
65
66 @Test
67 public void testNoTimeoutZero() {
68
69 try {
70 final Thread monitor = ThreadMonitor.start(Duration.ZERO);
71 assertNull(monitor, "Timeout 0, Monitor should be null");
72 TestUtils.sleep(100);
73 ThreadMonitor.stop(monitor);
74 } catch (final Exception e) {
75 fail("Timeout 0, threw " + e, e);
76 }
77 }
78
79
80
81
82 @Test
83 public void testTimeout() {
84 assertThrows(InterruptedException.class, () -> {
85 final Thread monitor = ThreadMonitor.start(Duration.ofMillis(100));
86 TestUtils.sleep(400);
87 ThreadMonitor.stop(monitor);
88 });
89 }
90 }