Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #29 from jglick/Timeout-JENKINS-32986
[JENKINS-32986] Introducing a general-purpose Timeout utility
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/main/java/org/jenkinsci/plugins/workflow/support/concurrent/Timeout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/org/jenkinsci/plugins/workflow/support/concurrent/TimeoutTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |