Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-32986] Introducing a general-purpose Timeout utility.
  • Loading branch information
jglick committed Feb 5, 2017
1 parent eb031e0 commit c810b38
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
@@ -0,0 +1,70 @@
/*
* The MIT License
*
* Copyright 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.support.concurrent;

import hudson.FilePath;
import hudson.remoting.VirtualChannel;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.util.Timer;

/**
* Allows operations to be limited in execution time.
* For example, {@link VirtualChannel#call} or {@link FilePath#isDirectory} could otherwise hang forever.
* Use in a {@code try}-with-resources block.
*/
public class Timeout implements AutoCloseable {

private static final Logger LOGGER = Logger.getLogger(Timeout.class.getName());

private final ScheduledFuture<?> task;

private Timeout(ScheduledFuture<?> task) {
this.task = task;
}

@Override public void close() {
task.cancel(false);
}

public static Timeout limit(final long time, final TimeUnit unit) {
final Thread thread = Thread.currentThread();
return new Timeout(Timer.get().schedule(new Runnable() {
@Override public void run() {
if (LOGGER.isLoggable(Level.FINE)) {
Throwable t = new Throwable();
t.setStackTrace(thread.getStackTrace());
LOGGER.log(Level.FINE, "Interrupting " + thread + " after " + time + " " + unit, t);
}
thread.interrupt();
}
}, time, unit));
}

// TODO JENKINS-32986 offer a variant that will escalate to Thread.stop

}
@@ -0,0 +1,50 @@
/*
* The MIT License
*
* Copyright 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.support.concurrent;

import java.util.concurrent.TimeUnit;
import org.junit.Test;
import static org.junit.Assert.*;

public class TimeoutTest {

@Test public void passed() throws Exception {
try (Timeout timeout = Timeout.limit(5, TimeUnit.SECONDS)) {
Thread.sleep(1_000);
}
Thread.sleep(5_000);
}

@Test public void failed() throws Exception {
try (Timeout timeout = Timeout.limit(5, TimeUnit.SECONDS)) {
Thread.sleep(10_000);
fail("should have timed out");
} catch (InterruptedException x) {
// good
}
Thread.sleep(1_000);
}

}

0 comments on commit c810b38

Please sign in to comment.