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.assertTrue;
20  
21  import java.util.concurrent.CountDownLatch;
22  import java.util.concurrent.atomic.AtomicBoolean;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests FileUtils.waitFor().
28   * <p>
29   * This class has been broken out from FileUtilsTestCase to solve issues as per BZ 38927
30   * </p>
31   *
32   * @see FileUtils
33   */
34  public class FileUtilsWaitForTest {
35  
36      @Test
37      public void testWaitFor0() {
38          FileUtils.waitFor(FileUtils.current(), 0);
39      }
40  
41      /**
42       * TODO Fails randomly.
43       */
44      @Test
45      public void testWaitForInterrupted() throws InterruptedException {
46          final AtomicBoolean wasInterrupted = new AtomicBoolean();
47          final CountDownLatch started = new CountDownLatch(2);
48          final int seconds = 10;
49          final Thread thread1 = new Thread(() -> {
50              started.countDown();
51              assertTrue(FileUtils.waitFor(FileUtils.current(), seconds));
52              wasInterrupted.set(Thread.currentThread().isInterrupted());
53          });
54          thread1.start();
55          // Make sure the thread does not finish before we interrupt it:
56          started.countDown();
57          thread1.interrupt();
58          started.await();
59          thread1.join();
60          assertTrue(wasInterrupted.get());
61      }
62  
63      @Test
64      public void testWaitForNegativeDuration() {
65          FileUtils.waitFor(FileUtils.current(), -1);
66      }
67  
68  }