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 junit.framework.TestCase;
20
21 /**
22 * Tests for {@link ThreadMonitor}.
23 */
24 public class ThreadMonitorTestCase extends TestCase {
25
26
27 public ThreadMonitorTestCase(final String name) {
28 super(name);
29 }
30
31 /**
32 * Test timeout.
33 */
34 public void testTimeout() {
35 try {
36 final Thread monitor = ThreadMonitor.start(100);
37 Thread.sleep(200);
38 ThreadMonitor.stop(monitor);
39 fail("Expected InterruptedException");
40 } catch (final InterruptedException e) {
41 // expected result - timout
42 }
43 }
44
45 /**
46 * Test task completed before timeout.
47 */
48 public void testCompletedWithoutTimeout() {
49 try {
50 final Thread monitor = ThreadMonitor.start(200);
51 Thread.sleep(100);
52 ThreadMonitor.stop(monitor);
53 } catch (final InterruptedException e) {
54 fail("Timed Out");
55 }
56 }
57
58 /**
59 * Test No timeout.
60 */
61 public void testNoTimeout() {
62
63 // timeout = -1
64 try {
65 final Thread monitor = ThreadMonitor.start(-1);
66 assertNull("Timeout -1, Monitor should be null", monitor);
67 Thread.sleep(100);
68 ThreadMonitor.stop(monitor);
69 } catch (final Exception e) {
70 fail("Timeout -1, threw " + e);
71 }
72
73 // timeout = 0
74 try {
75 final Thread monitor = ThreadMonitor.start(0);
76 assertNull("Timeout 0, Monitor should be null", monitor);
77 Thread.sleep(100);
78 ThreadMonitor.stop(monitor);
79 } catch (final Exception e) {
80 fail("Timeout 0, threw " + e);
81 }
82 }
83 }
84