use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class TimeBoxingTest method testTimeBoxing.
@Test
public void testTimeBoxing() throws Exception {
final Context context = Context.create();
Source source = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(LOOP(infinity,STATEMENT))", "NotEnoughTime").buildLiteral();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
debugger.startSession(new SuspendedCallback() {
public void onSuspend(SuspendedEvent event) {
event.prepareKill();
}
}).suspendNextExecution();
}
}, 1000);
try {
// throws KillException, wrapped by PolyglotException
context.eval(source);
Assert.fail();
} catch (PolyglotException pex) {
Assert.assertEquals("com.oracle.truffle.api.debug.KillException", pex.getMessage());
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class LanguageSPITest method testLookupHostDisabled.
@Test
public void testLookupHostDisabled() {
Context context = Context.newBuilder().allowHostAccess(false).build();
try {
eval(context, new Function<Env, Object>() {
public Object apply(Env t) {
return t.lookupHostSymbol("java.util.HashMap");
}
});
fail();
} catch (PolyglotException e) {
assertTrue(!e.isInternalError());
}
context.close();
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class MultiThreadedLanguageTest method testAsssertionIfThreadStillActive.
@Test
public void testAsssertionIfThreadStillActive() throws InterruptedException {
MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
return true;
};
Engine engine = Engine.create();
Context context = Context.newBuilder().allowCreateThread(true).engine(engine).build();
Semaphore wait = new Semaphore(0);
Thread returnThread = eval(context, new Function<Env, Object>() {
public Object apply(Env env) {
Semaphore waitForEnter = new Semaphore(0);
Thread t = env.createThread(() -> {
try {
waitForEnter.release();
wait.acquire();
} catch (InterruptedException e) {
}
});
t.start();
try {
waitForEnter.acquire();
} catch (InterruptedException e) {
}
return t;
}
}).asHostObject();
try {
engine.close();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isInternalError());
assertTrue(e.getMessage().contains("The language did not complete all polyglot threads but should have"));
}
wait.release(1);
returnThread.join();
engine.close();
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class PolyglotExceptionTest method testExceptionWrapping.
@Test
public void testExceptionWrapping() {
Context context = Context.create();
Context otherContext = Context.create();
CauseErrorTruffleObject causeError = new CauseErrorTruffleObject();
causeError.thrownError = new TestGuestError();
Value throwError = context.asValue(causeError);
Value throwErrorOtherContext = otherContext.asValue(causeError);
try {
throwError.execute();
Assert.fail();
} catch (PolyglotException e) {
Assert.assertEquals(e.getMessage(), "MyError");
Assert.assertTrue(e.isGuestException());
}
Value verifyError = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
try {
throwError.execute();
Assert.fail();
} catch (PolyglotException e) {
Assert.assertEquals(e.getMessage(), "MyError");
Assert.assertTrue(e.isGuestException());
throw e;
}
return null;
}
});
try {
verifyError.execute();
Assert.fail();
} catch (PolyglotException e) {
Assert.assertEquals(e.getMessage(), "MyError");
Assert.assertTrue(e.isGuestException());
}
// if the exception was thrown by a different context it will be treated
// as a host exception.
Value verifyErrorOtherContext = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
try {
throwErrorOtherContext.execute();
} catch (PolyglotException e) {
Assert.assertEquals(e.getMessage(), "MyError");
Assert.assertTrue(e.isGuestException());
throw e;
}
return null;
}
});
try {
verifyErrorOtherContext.execute();
Assert.fail();
} catch (PolyglotException e) {
// assert that polyglot exception was not unboxed if from other context
Assert.assertTrue(e.asHostException() instanceof PolyglotException);
PolyglotException polyglot = (PolyglotException) e.asHostException();
Assert.assertEquals(polyglot.getMessage(), "MyError");
Assert.assertTrue(polyglot.isGuestException());
}
context.close();
otherContext.close();
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class ProxyAPITest method testExceptionFrames.
@Test
public void testExceptionFrames() {
Value innerInner = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
throw new RuntimeException("foobar");
}
});
Value inner = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
return innerInner.execute();
}
});
Value outer = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
return inner.execute();
}
});
try {
outer.execute();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof RuntimeException);
assertEquals("foobar", e.getMessage());
Iterator<StackFrame> frameIterator = e.getPolyglotStackTrace().iterator();
StackFrame frame;
for (int i = 0; i < 6; i++) {
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("execute", frame.toHostFrame().getMethodName());
}
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("testExceptionFrames", frame.toHostFrame().getMethodName());
}
}
Aggregations