changeset 22321:7e212c4f2115

Rewriting the SL debugger test to be synchronous. Makes it easier to reason about what is going on.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 21 Oct 2015 15:16:28 +0200
parents 5ee16f4908e5
children 7c396c4f46f5
files truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLDebugTest.java
diffstat 1 files changed, 33 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLDebugTest.java	Wed Oct 21 00:16:07 2015 +0200
+++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLDebugTest.java	Wed Oct 21 15:16:28 2015 +0200
@@ -50,17 +50,18 @@
 import com.oracle.truffle.api.vm.EventConsumer;
 import com.oracle.truffle.api.vm.PolyglotEngine;
 import java.io.ByteArrayOutputStream;
+import java.util.LinkedList;
 import java.util.concurrent.Callable;
-import java.util.concurrent.Executors;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import org.junit.Test;
 
 public class SLDebugTest {
     private Source factorial;
     private Debugger debugger;
-    private Callable<?> run;
+    private final LinkedList<Callable<?>> run = new LinkedList<>();
     private SuspendedEvent suspendedEvent;
     private Throwable ex;
     private ExecutionEvent executionEvent;
@@ -89,7 +90,7 @@
 
         ByteArrayOutputStream os = new ByteArrayOutputStream();
 
-        PolyglotEngine engine = PolyglotEngine.buildNew().executor(Executors.newSingleThreadExecutor()).onEvent(new EventConsumer<ExecutionEvent>(ExecutionEvent.class) {
+        PolyglotEngine engine = PolyglotEngine.buildNew().onEvent(new EventConsumer<ExecutionEvent>(ExecutionEvent.class) {
             @Override
             protected void on(ExecutionEvent event) {
                 onExecution(event);
@@ -101,14 +102,7 @@
             }
         }).setOut(os).build();
 
-        PolyglotEngine.Value value;
-        synchronized (this) {
-            value = engine.eval(factorial);
-            wait();
-        }
-        assertNotNull("Debugger initalized", debugger);
-
-        run(new Callable<Void>() {
+        onEvent(new Callable<Void>() {
             @Override
             public Void call() throws Exception {
                 LineLocation nMinusOne = factorial.createLineLocation(7);
@@ -118,10 +112,11 @@
             }
         });
 
+        PolyglotEngine.Value value = engine.eval(factorial);
+
         assertNull("Parsing done", value.get());
 
-        PolyglotEngine.Value main = engine.findGlobalSymbol("main");
-        value = main.invoke(null);
+        assertExecutedOK();
 
         run(new Callable<Void>() {
             @Override
@@ -172,67 +167,62 @@
             }
         });
 
-        run(null);
+        PolyglotEngine.Value main = engine.findGlobalSymbol("main");
+        value = main.invoke(null);
+
+        assertExecutedOK();
 
         Number n = value.as(Number.class);
         assertNotNull(n);
         assertEquals("Factorial computed OK", 2, n.intValue());
     }
 
-    synchronized void onExecution(ExecutionEvent event) {
+    void onExecution(ExecutionEvent event) {
         executionEvent = event;
         debugger = event.getDebugger();
-        notifyAll();
-        waitForWork();
+        performWork();
     }
 
-    synchronized void onSuspended(SuspendedEvent event) {
+    void onSuspended(SuspendedEvent event) {
         suspendedEvent = event;
-        notifyAll();
-        waitForWork();
+        performWork();
     }
 
-    private synchronized void run(Callable<?> callable) throws Throwable {
+    private void run(Callable<?> callable) throws Throwable {
         if (ex != null) {
             throw ex;
         }
-        while (run != null) {
-            wait();
+        if (callable == null) {
+            assertTrue("Assuming all requests processed: " + run, run.isEmpty());
+            return;
         }
-        run = callable;
-        notifyAll();
+        run.addLast(callable);
         if (ex != null) {
             throw ex;
         }
     }
 
-    private void waitForWork() {
+    private void performWork() {
+        Callable<?> c = run.removeFirst();
         try {
-            waitForWork0().call();
-        } catch (Throwable tmpEx) {
-            this.ex = tmpEx;
+            c.call();
+        } catch (Exception e) {
+            this.ex = e;
         }
-
     }
 
-    private synchronized Callable<?> waitForWork0() {
-        while (run == null) {
-            try {
-                wait();
-            } catch (InterruptedException tmpEx) {
-                throw new IllegalStateException(tmpEx);
-            }
-        }
-        Callable<?> c = run;
-        run = null;
-        notifyAll();
-        return c;
+    private void assertExecutedOK() throws Throwable {
+        run(null);
     }
 
-    void assertLine(int line) {
+    private void assertLine(int line) {
         assertNotNull(suspendedEvent);
         final SourceSection expLoc = factorial.createSection("Line " + line, line);
         final SourceSection sourceLoc = suspendedEvent.getNode().getEncapsulatingSourceSection();
         assertEquals("Exp\n" + expLoc + "\nbut was\n" + sourceLoc, line, sourceLoc.getLineLocation().getLineNumber());
     }
+
+    private void onEvent(Callable<Void> callable) throws Throwable {
+        run(callable);
+    }
 }