From 140b4d41d695f32e75c785f6179430f677d244ae Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 1 Jun 2015 16:43:30 +1000 Subject: Make shrinking print where it's up to when signal (ie. ctrl+c) before exiting --- .../id/zancanaro/javacheck/junit/Properties.java | 25 ++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src/main/java') 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 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> trees = failed.getChildren(); Set> 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> 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 +} -- cgit v1.2.3