Repository: brooklyn-server Updated Branches: refs/heads/master ddb6acc92 -> da14297bb
Add Exceptions.propagateIfInterrupt Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/9f2f17f9 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/9f2f17f9 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/9f2f17f9 Branch: refs/heads/master Commit: 9f2f17f9c3fad19506341aa6b4e486b7f88eeb03 Parents: e1c310f Author: Aled Sage <aled.s...@gmail.com> Authored: Tue Aug 2 18:53:17 2016 +0100 Committer: Aled Sage <aled.s...@gmail.com> Committed: Thu Aug 4 09:10:47 2016 +0100 ---------------------------------------------------------------------- .../brooklyn/util/exceptions/Exceptions.java | 23 ++++++++---- .../util/exceptions/ExceptionsTest.java | 39 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9f2f17f9/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java b/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java index 2997f49..c517eed 100644 --- a/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java +++ b/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java @@ -164,20 +164,29 @@ public class Exceptions { } throw new PropagatedRuntimeException(msg, throwable); } - + /** - * Propagate exceptions which are fatal. - * <p> - * Propagates only those exceptions which one rarely (if ever) wants to capture, - * such as {@link InterruptedException} and {@link Error}s. + * Propagate exceptions which are interrupts (be it {@link InterruptedException} + * or {@link RuntimeInterruptedException}. */ - public static void propagateIfFatal(Throwable throwable) { + public static void propagateIfInterrupt(Throwable throwable) { if (throwable instanceof InterruptedException) { throw new RuntimeInterruptedException((InterruptedException) throwable); } else if (throwable instanceof RuntimeInterruptedException) { Thread.currentThread().interrupt(); throw (RuntimeInterruptedException) throwable; - } else if (throwable instanceof Error) { + } + } + + /** + * Propagate exceptions which are fatal. + * <p> + * Propagates only those exceptions which one rarely (if ever) wants to capture, + * such as {@link InterruptedException} and {@link Error}s. + */ + public static void propagateIfFatal(Throwable throwable) { + propagateIfInterrupt(throwable); + if (throwable instanceof Error) { throw (Error) throwable; } } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9f2f17f9/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java ---------------------------------------------------------------------- diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java b/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java index d5dcd04..65b5d91 100644 --- a/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java +++ b/utils/common/src/test/java/org/apache/brooklyn/util/exceptions/ExceptionsTest.java @@ -127,6 +127,45 @@ public class ExceptionsTest { Throwable t = new Throwable(); Exceptions.propagateIfFatal(t); } + + @Test + public void testPropagateIfInterruptPropagatesInterruptedException() throws Exception { + InterruptedException tothrow = new InterruptedException("simulated"); + try { + Exceptions.propagateIfInterrupt(tothrow); + fail(); + } catch (RuntimeException e) { + assertTrue(Thread.interrupted()); // note this clears the interrupted flag as well + assertEquals(e.getCause(), tothrow); + } + } + + @Test + public void testPropagateIfInterruptPropagatesRuntimeInterruptedException() throws Exception { + RuntimeInterruptedException tothrow = new RuntimeInterruptedException(new InterruptedException("simulated")); + try { + Exceptions.propagateIfInterrupt(tothrow); + fail(); + } catch (RuntimeInterruptedException e) { + assertTrue(Thread.interrupted()); // note this clears the interrupted flag as well + assertEquals(e, tothrow); + } + } + + @Test + public void testPropagateIfInterruptDoesNotPropagateOtherExceptions() throws Exception { + Exception e = new Exception(); + Exceptions.propagateIfInterrupt(e); + + RuntimeException re = new RuntimeException(); + Exceptions.propagateIfInterrupt(re); + + Throwable t = new Throwable(); + Exceptions.propagateIfInterrupt(t); + + Throwable er = new Error(); + Exceptions.propagateIfInterrupt(er); + } @Test public void testGetFirstThrowableOfType() throws Exception {