View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests for {@link ThreadMonitor}.
30   */
31  public class ThreadMonitorTest {
32  
33      /**
34       * Test task completed before timeout.
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       * Test No timeout.
49       */
50      @Test
51      public void testNoTimeoutMinus1() {
52          // timeout = -1
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       * Test No timeout.
65       */
66      @Test
67      public void testNoTimeoutZero() {
68          // timeout = 0
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       * Test timeout.
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  }