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    *      https://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.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.File;
23  import java.util.concurrent.TimeUnit;
24  import java.util.concurrent.atomic.AtomicBoolean;
25  
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.Timeout;
28  
29  /**
30   * Tests FileUtils.waitFor().
31   * <p>
32   * This class has been broken out from FileUtilsTestCase to solve issues as per BZ 38927
33   * </p>
34   *
35   * @see FileUtils
36   */
37  class FileUtilsWaitForTest {
38  
39      // Assume that this file does not exist
40      private final File NOSUCHFILE = new File("a.b.c.d." + System.currentTimeMillis());
41  
42      @Test
43      void testIO_488() throws InterruptedException {
44          final long start = System.currentTimeMillis();
45          final AtomicBoolean wasInterrupted = new AtomicBoolean();
46          final int seconds = 3;
47          final Thread thread1 = new Thread(() -> {
48              // This will wait (assuming the file is not found)
49              assertFalse(FileUtils.waitFor(NOSUCHFILE, seconds), "Should not find file");
50              wasInterrupted.set(Thread.currentThread().isInterrupted());
51          });
52          thread1.start();
53          Thread.sleep(500); // This should be enough to ensure the waitFor loop has been entered
54          thread1.interrupt(); // Try to interrupt waitFor
55          thread1.join();
56          assertTrue(wasInterrupted.get(), "Should have been interrupted");
57          final long elapsed = System.currentTimeMillis() - start;
58          assertTrue(elapsed >= seconds * 1000, "Should wait for n seconds, actual: " + elapsed);
59      }
60  
61      @Test
62      @Timeout(value = 30, unit = TimeUnit.MILLISECONDS) // Should complete quickly as the path is present
63      void testWaitFor0() {
64          assertTrue(FileUtils.waitFor(FileUtils.current(), 0));
65      }
66  
67      @Test
68      @Timeout(value = 30, unit = TimeUnit.MILLISECONDS) // Should complete quickly even though the path is missing
69      void testWaitFor0Absent() {
70          assertFalse(FileUtils.waitFor(NOSUCHFILE, 0));
71      }
72  
73      @Test
74      @Timeout(value = 30, unit = TimeUnit.MILLISECONDS) // Should complete quickly as the path is present
75      void testWaitFor10() {
76          assertTrue(FileUtils.waitFor(FileUtils.current(), 10));
77      }
78  
79      @Test
80      @Timeout(value = 30, unit = TimeUnit.MILLISECONDS) // Should complete quickly as the path is present
81      void testWaitFor100() {
82          assertTrue(FileUtils.waitFor(FileUtils.current(), 100));
83      }
84  
85      @Test
86      @Timeout(value = 3, unit = TimeUnit.SECONDS) // Allow for timeout waiting for non-existent file
87      void testWaitFor5Absent() {
88          final long start = System.currentTimeMillis();
89          assertFalse(FileUtils.waitFor(NOSUCHFILE, 2));
90          final long elapsed = System.currentTimeMillis() - start;
91          assertTrue(elapsed >= 2000, "Must reach timeout - expected 2000, actual: " + elapsed);
92      }
93  
94      @Test
95      @Timeout(value = 300, unit = TimeUnit.MILLISECONDS) // Should complete quickly as the path is present
96      void testWaitForNegativeDuration() {
97          assertTrue(FileUtils.waitFor(FileUtils.current(), -1));
98      }
99  
100     @Test
101     @Timeout(value = 30, unit = TimeUnit.MILLISECONDS) // Should complete quickly even though the path is missing
102     void testWaitForNegativeDurationAbsent() {
103         assertFalse(FileUtils.waitFor(NOSUCHFILE, -1));
104     }
105 
106 }