|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8336479 |
| 27 | + * @summary Tests for Process.waitFor(Duration) |
| 28 | + * @library /test/lib |
| 29 | + * @run junit WaitForDuration |
| 30 | + */ |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.time.Duration; |
| 34 | +import java.util.stream.Stream; |
| 35 | +import jdk.test.lib.process.ProcessTools; |
| 36 | + |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.junit.jupiter.params.ParameterizedTest; |
| 39 | +import org.junit.jupiter.params.provider.Arguments; |
| 40 | +import org.junit.jupiter.params.provider.MethodSource; |
| 41 | +import static org.junit.jupiter.api.Assertions.*; |
| 42 | + |
| 43 | +public class WaitForDuration { |
| 44 | + static Stream<Arguments> durations() { |
| 45 | + return Stream.of( |
| 46 | + Arguments.of(Duration.ZERO, 3_600_000, false), |
| 47 | + Arguments.of(Duration.ofSeconds(-100), 3_600_000, false), |
| 48 | + Arguments.of(Duration.ofSeconds(100), 0, true), |
| 49 | + Arguments.of(Duration.ofSeconds(Long.MAX_VALUE), 0, true), // nano overflow |
| 50 | + Arguments.of(Duration.ofSeconds(Long.MIN_VALUE), 3_600_000, false) // nano underflow |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest |
| 55 | + @MethodSource("durations") |
| 56 | + void testEdgeDurations(Duration d, int sleepMillis, boolean expected) |
| 57 | + throws IOException, InterruptedException { |
| 58 | + var pb = ProcessTools.createTestJavaProcessBuilder( |
| 59 | + WaitForDuration.class.getSimpleName(), Integer.toString(sleepMillis)); |
| 60 | + assertEquals(expected, pb.start().waitFor(d)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testNullDuration() throws IOException, InterruptedException { |
| 65 | + var pb = ProcessTools.createTestJavaProcessBuilder( |
| 66 | + WaitForDuration.class.getSimpleName(), "0"); |
| 67 | + assertThrows(NullPointerException.class, () -> pb.start().waitFor(null)); |
| 68 | + } |
| 69 | + |
| 70 | + public static void main(String... args) throws InterruptedException { |
| 71 | + Thread.sleep(Integer.parseInt(args[0])); |
| 72 | + } |
| 73 | +} |
0 commit comments