# HG changeset patch # User Jaroslav Tulach # Date 1445433388 -7200 # Node ID 7e212c4f211513f5f806912221d4d0991f68c0c5 # Parent 5ee16f4908e565c39fc6d8422fda53c677d350a7 Rewriting the SL debugger test to be synchronous. Makes it easier to reason about what is going on. diff -r 5ee16f4908e5 -r 7e212c4f2115 truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLDebugTest.java --- 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> 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.class) { + PolyglotEngine engine = PolyglotEngine.buildNew().onEvent(new EventConsumer(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() { + onEvent(new Callable() { @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() { @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 callable) throws Throwable { + run(callable); + } }