diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-01 16:43:30 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-01 16:43:30 +1000 |
commit | 140b4d41d695f32e75c785f6179430f677d244ae (patch) | |
tree | 9dfd588234ab9666c0e409f563ca94e2052dd7c1 | |
parent | 7db38e2e4f2c24c2a21d1c19c12bfdad4727ebad (diff) |
Make shrinking print where it's up to when signal (ie. ctrl+c) before exiting
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/junit/Properties.java | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java index 64bd9ab..6ff0538 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java @@ -221,7 +221,13 @@ public class Properties extends BlockJUnit4ClassRunner { } private ShrinkResult shrink(RoseTree<Object[]> failed, Throwable originalEx) { - ShrinkResult smallest = new ShrinkResult(failed.getValue(), originalEx); + // this array is a mutable container so the shutdown handler can see the new version + ShrinkResult[] smallest = new ShrinkResult[]{ + new ShrinkResult(failed.getValue(), originalEx)}; + + Thread shutdownHandler = makeShutdownHandler(smallest, originalEx); + Runtime.getRuntime().addShutdownHook(shutdownHandler); + Iterator<RoseTree<Object[]>> trees = failed.getChildren(); Set<List<Object>> seenArgs = new HashSet<>(); while (trees.hasNext()) { @@ -232,7 +238,7 @@ public class Properties extends BlockJUnit4ClassRunner { } catch (AssumptionViolatedException ex) { // ignore, because it's not useful } catch (Throwable ex) { - smallest = new ShrinkResult(tree.getValue(), ex); + smallest[0] = new ShrinkResult(tree.getValue(), ex); Iterator<RoseTree<Object[]>> children = tree.getChildren(); if (children.hasNext()) { trees = children; @@ -242,7 +248,18 @@ public class Properties extends BlockJUnit4ClassRunner { } } } - return smallest; + + Runtime.getRuntime().removeShutdownHook(shutdownHandler); + return smallest[0]; + } + + private Thread makeShutdownHandler(ShrinkResult[] smallest, Throwable originalException) { + return new Thread(() -> { + System.err.println("Signal received while shrinking.\n" + + "Current best shrink is: " + Arrays.toString(smallest[0].args) + "\n" + + "Shrinking exception: " + smallest[0].thrown + "\n" + + "Originally was: " + originalException); + }); } public void runTest(final Object[] args) throws Throwable { @@ -276,4 +293,4 @@ public class Properties extends BlockJUnit4ClassRunner { } } -}
\ No newline at end of file +} |