Repository: incubator-freemarker
Updated Branches:
  refs/heads/3 441367655 -> ef4674e84


Forward ported from 2.3-gae: Fixed template interruption test (could hang if 
the template fails for some reason).


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/fa92465b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/fa92465b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/fa92465b

Branch: refs/heads/3
Commit: fa92465b4348222e692bd17392854a3b91762d33
Parents: 4413676
Author: ddekany <[email protected]>
Authored: Tue Oct 3 19:58:31 2017 +0200
Committer: ddekany <[email protected]>
Committed: Tue Oct 3 20:23:09 2017 +0200

----------------------------------------------------------------------
 .../core/TheadInterruptingSupportTest.java      | 32 +++++++++++++++++---
 1 file changed, 27 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/fa92465b/freemarker-core-test/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java
 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java
index 1468e59..933a9de 100644
--- 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java
+++ 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java
@@ -42,7 +42,7 @@ public class TheadInterruptingSupportTest {
     private final Configuration cfg = new TestConfigurationBuilder().build();
 
     @Test
-    public void test() throws IOException, InterruptedException {
+    public void test() throws IOException, InterruptedException, 
TemplateException {
         assertCanBeInterrupted("<#list 1.. as x></#list>");
         assertCanBeInterrupted("<#list 1.. as x>${x}</#list>");
         assertCanBeInterrupted("<#list 1.. as x>t${x}</#list>");
@@ -59,14 +59,24 @@ public class TheadInterruptingSupportTest {
         assertCanBeInterrupted("<#attempt><#list 1.. as 
_></#list><#recover>suppress</#attempt>");
     }
 
-    private void assertCanBeInterrupted(final String templateSourceCode) 
throws IOException, InterruptedException {
+    private void assertCanBeInterrupted(final String templateSourceCode)
+            throws IOException, InterruptedException, TemplateException {
         TemplateRunnerThread trt = new 
TemplateRunnerThread(templateSourceCode);
         trt.start();
         synchronized (trt) {
-            while (!trt.isStarted()) {
+            while (!trt.isStartedOrFailed()) {
                 trt.wait();
             }
         }
+        if (trt.failedWith != null) {
+            if (trt.failedWith instanceof TemplateException) {
+                throw (TemplateException) trt.failedWith;
+            } else if (trt.failedWith instanceof IOException) {
+                throw (IOException) trt.failedWith;
+            } else {
+                throw new RuntimeException("Template processing has failed", 
trt.failedWith);
+            }
+        }
         Thread.sleep(50); // Just to ensure (hope...) that the template 
execution reaches "deep" enough
         trt.interrupt();
         trt.join(TEMPLATE_INTERRUPTION_TIMEOUT);
@@ -77,6 +87,7 @@ public class TheadInterruptingSupportTest {
 
         private final Template template;
         private boolean started;
+        private Throwable failedWith;
         private boolean templateProcessingInterrupted;
 
         public TemplateRunnerThread(String templateSourceCode) throws 
IOException {
@@ -95,6 +106,17 @@ public class TheadInterruptingSupportTest {
                 }
             } catch (Throwable e) {
                 LOG.error("Template processing failed", e);
+                synchronized (TemplateRunnerThread.this) {
+                    failedWith = e;
+                    TemplateRunnerThread.this.notifyAll();
+                }
+            } finally {
+                synchronized (TemplateRunnerThread.this) {
+                    if (!started && failedWith == null) {
+                        failedWith = new IllegalStateException("Start 
directive was never called");
+                        TemplateRunnerThread.this.notifyAll();
+                    }
+                }
             }
         }
 
@@ -102,8 +124,8 @@ public class TheadInterruptingSupportTest {
             return templateProcessingInterrupted;
         }
         
-        public synchronized boolean isStarted() {
-            return started;
+        public synchronized boolean isStartedOrFailed() {
+            return started || failedWith != null;
         }
         
         public TemplateDirectiveModel getStartedDirective() {

Reply via email to