aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com
diff options
context:
space:
mode:
authorGravatar ulfjack <ulfjack@google.com>2017-08-10 10:28:50 +0200
committerGravatar Marcel Hlopko <hlopko@google.com>2017-08-10 13:48:10 +0200
commit0ec7180c361fd524f0beef1814c7930e7ed9898d (patch)
tree1ad11c56984a45c2c00338d5f6a225046e9ad897 /src/test/java/com
parentd456fca1f79c5c8f0306cf593a38504343d36a7f (diff)
Implement timeouts on top of Java Process
PiperOrigin-RevId: 164827022
Diffstat (limited to 'src/test/java/com')
-rw-r--r--src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java5
-rw-r--r--src/test/java/com/google/devtools/build/lib/shell/CommandTest.java89
-rw-r--r--src/test/java/com/google/devtools/build/lib/shell/SimpleKillableObserver.java47
3 files changed, 3 insertions, 138 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java b/src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java
index 74a4193498..e822904a17 100644
--- a/src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java
@@ -135,11 +135,6 @@ public class LocalSpawnRunnerTest {
private static final class SubprocessInterceptor implements Subprocess.Factory {
@Override
- public boolean supportsTimeout() {
- return true;
- }
-
- @Override
public Subprocess create(SubprocessBuilder params) throws IOException {
throw new UnsupportedOperationException();
}
diff --git a/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java b/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java
index a4aab8611a..52dd8b9909 100644
--- a/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java
+++ b/src/test/java/com/google/devtools/build/lib/shell/CommandTest.java
@@ -44,9 +44,6 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class CommandTest {
- private static final long LONG_TIME = 10000;
- private static final long SHORT_TIME = 250;
-
// Platform-independent tests ----------------------------------------------
@Before
@@ -285,7 +282,7 @@ public class CommandTest {
new Command(args).execute();
fail("Should have exited with status " + exit);
} catch (BadExitStatusException e) {
- assertThat(e).hasMessage("Process exited with status " + exit);
+ assertThat(e).hasMessageThat().isEqualTo("Process exited with status " + exit);
checkCommandElements(e, "/bin/sh", "-c", "exit " + exit);
TerminationStatus status = e.getResult().getTerminationStatus();
assertThat(status.success()).isFalse();
@@ -303,7 +300,7 @@ public class CommandTest {
new Command(args).execute();
fail("Should have exited with status " + expected);
} catch (BadExitStatusException e) {
- assertThat(e).hasMessage("Process exited with status " + expected);
+ assertThat(e).hasMessageThat().isEqualTo("Process exited with status " + expected);
checkCommandElements(e, "/bin/bash", "-c", "exit " + exit);
TerminationStatus status = e.getResult().getTerminationStatus();
assertThat(status.success()).isFalse();
@@ -328,7 +325,7 @@ public class CommandTest {
new Command(args).execute();
fail("Expected signal " + signal);
} catch (AbnormalTerminationException e) {
- assertThat(e).hasMessage("Process terminated by signal " + signal);
+ assertThat(e).hasMessageThat().isEqualTo("Process terminated by signal " + signal);
checkCommandElements(e, killmyself, "" + signal);
TerminationStatus status = e.getResult().getTerminationStatus();
assertThat(status.success()).isFalse();
@@ -440,86 +437,6 @@ public class CommandTest {
}
}
- /**
- * Helper to test KillableObserver classes.
- */
- private static class KillableTester implements Killable {
- private boolean isKilled = false;
- private boolean timedOut = false;
- @Override
- public synchronized void kill() {
- isKilled = true;
- notifyAll();
- }
- public synchronized boolean getIsKilled() {
- return isKilled;
- }
- /**
- * Wait for a specified time or until the {@link #kill()} is called.
- */
- public synchronized void sleepUntilKilled(final long timeoutMS) {
- long nowTime = System.currentTimeMillis();
- long endTime = nowTime + timeoutMS;
- while (!isKilled && !timedOut) {
- long waitTime = endTime - nowTime;
- if (waitTime <= 0) {
- // Process has timed out, needs killing.
- timedOut = true;
- break;
- }
- try {
- wait(waitTime); // Suffers "spurious wakeup", hence the while() loop.
- nowTime = System.currentTimeMillis();
- } catch (InterruptedException exception) {
- break;
- }
- }
- }
- }
-
- @Test
- public void testTimeOutKillableObserverNoKill() throws Exception {
- KillableTester killable = new KillableTester();
- TimeoutKillableObserver observer = new TimeoutKillableObserver(LONG_TIME);
- observer.startObserving(killable);
- observer.stopObserving(killable);
- assertThat(observer.hasTimedOut()).isFalse();
- assertThat(killable.getIsKilled()).isFalse();
- }
-
- @Test
- public void testTimeOutKillableObserverNoKillWithDelay() throws Exception {
- KillableTester killable = new KillableTester();
- TimeoutKillableObserver observer = new TimeoutKillableObserver(LONG_TIME);
- observer.startObserving(killable);
- killable.sleepUntilKilled(SHORT_TIME);
- observer.stopObserving(killable);
- assertThat(observer.hasTimedOut()).isFalse();
- assertThat(killable.getIsKilled()).isFalse();
- }
-
- @Test
- public void testTimeOutKillableObserverWithKill() throws Exception {
- KillableTester killable = new KillableTester();
- TimeoutKillableObserver observer = new TimeoutKillableObserver(SHORT_TIME);
- observer.startObserving(killable);
- killable.sleepUntilKilled(LONG_TIME);
- observer.stopObserving(killable);
- assertThat(observer.hasTimedOut()).isTrue();
- assertThat(killable.getIsKilled()).isTrue();
- }
-
- @Test
- public void testTimeOutKillableObserverWithKillZeroMillis() throws Exception {
- KillableTester killable = new KillableTester();
- TimeoutKillableObserver observer = new TimeoutKillableObserver(0);
- observer.startObserving(killable);
- killable.sleepUntilKilled(LONG_TIME);
- observer.stopObserving(killable);
- assertThat(observer.hasTimedOut()).isTrue();
- assertThat(killable.getIsKilled()).isTrue();
- }
-
private static void checkCommandElements(CommandException e,
String... expected) {
assertArrayEquals(expected, e.getCommand().getCommandLineElements());
diff --git a/src/test/java/com/google/devtools/build/lib/shell/SimpleKillableObserver.java b/src/test/java/com/google/devtools/build/lib/shell/SimpleKillableObserver.java
deleted file mode 100644
index 3a2e4a633a..0000000000
--- a/src/test/java/com/google/devtools/build/lib/shell/SimpleKillableObserver.java
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright 2014 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.google.devtools.build.lib.shell;
-
-/**
- * A simple implementation of {@link KillableObserver} which can be told explicitly to kill its
- * {@link Killable} by calling {@link #kill()}. This is the sort of functionality that callers might
- * expect to find available on the {@link Command} class.</p>
- *
- * <p>Note that this class can only observe one {@link Killable} at a time; multiple instances
- * should be used for concurrent calls to {@link Command#execute}.
- */
-final class SimpleKillableObserver implements KillableObserver {
- private Killable killable;
-
- @Override
- public synchronized void startObserving(final Killable killable) {
- this.killable = killable;
- }
-
- @Override
- public synchronized void stopObserving(final Killable killable) {
- if (!this.killable.equals(killable)) {
- throw new IllegalStateException("start/stopObservering called with " +
- "different Killables");
- }
- this.killable = null;
- }
-
- public synchronized void kill() {
- if (killable != null) {
- killable.kill();
- }
- }
-}