use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLDebugALot method test.
@Test
public void test() {
try (Engine engine = Engine.newBuilder().out(out).err(err).option("debugalot", "true").build()) {
try (Context context = Context.newBuilder().engine(engine).build()) {
context.eval(slCode);
}
}
String log = out.toString();
String successMessage = "Executed successfully:";
int index = log.lastIndexOf(successMessage);
Assert.assertTrue(log, index > 0);
String success = log.substring(index + successMessage.length()).trim();
Assert.assertEquals(log, "TRUE", success);
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLDebugTest method testTimeboxing.
@Test(expected = PolyglotException.class)
public void testTimeboxing() throws Throwable {
final Source endlessLoop = slCode("function main() {\n" + " i = 1; \n" + " while(i > 0) {\n" + " i = i + 1;\n" + " }\n" + " return i; \n" + "}\n");
final Context context = Context.create("sl");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
context.getEngine().getInstruments().get("debugger").lookup(Debugger.class).startSession(new SuspendedCallback() {
public void onSuspend(SuspendedEvent event) {
event.prepareKill();
}
}).suspendNextExecution();
}
}, 1000);
context.eval(endlessLoop);
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class InstrumentationMultiThreadingTest method testAsyncAttachement.
@Test
public void testAsyncAttachement() throws InterruptedException, ExecutionException {
// unfortunately we don't support multiple evals yet
int nEvals = 1;
int nInstruments = 10;
for (int j = 0; j < 5; j++) {
ExecutorService executorService = Executors.newFixedThreadPool(nEvals + nInstruments);
List<Future<?>> futures = new ArrayList<>();
final AtomicBoolean terminated = new AtomicBoolean(false);
final AtomicReference<Context> engineRef = new AtomicReference<>();
for (int i = 0; i < nEvals; i++) {
futures.add(executorService.submit(new Runnable() {
public void run() {
final Context engine = Context.create();
engineRef.set(engine);
while (!terminated.get()) {
try {
engine.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(BLOCK(STATEMENT(EXPRESSION, EXPRESSION), STATEMENT(EXPRESSION)))"));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}
}));
}
for (int i = 0; i < nInstruments; i++) {
final int index = i;
futures.add(executorService.submit(new Runnable() {
public void run() {
while (!terminated.get()) {
Context engine = engineRef.get();
if (engine != null) {
engine.getEngine().getInstruments().get("testAsyncAttachement1").lookup(EnableableInstrument.class).setEnabled(index % 2 == 0);
engine.getEngine().getInstruments().get("testAsyncAttachement2").lookup(EnableableInstrument.class).setEnabled(index % 2 == 1);
}
}
}
}));
}
Thread.sleep(1000);
terminated.set(true);
for (Future<?> future : futures) {
future.get();
}
engineRef.get().getEngine().getInstruments().get("testAsyncAttachement1").lookup(EnableableInstrument.class).setEnabled(false);
engineRef.get().getEngine().getInstruments().get("testAsyncAttachement2").lookup(EnableableInstrument.class).setEnabled(false);
}
Assert.assertEquals(0, createDisposeCount.get());
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ThreadsEventsTest method testSpawnThread.
@Test
public void testSpawnThread() {
final List<ThreadEvent> events;
try (Context context = Context.newBuilder().allowCreateThread(true).build()) {
Instrument testThreadsInstrument = context.getEngine().getInstruments().get("testThreadsInstrument");
TestThreadsInstrument test = testThreadsInstrument.lookup(TestThreadsInstrument.class);
events = test.events;
context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(DEFINE(foo, STATEMENT), SPAWN(foo), JOIN())"));
assertEquals(3, events.size());
// current thread
assertTrue(events.get(0).isNew);
assertEquals(Thread.currentThread(), events.get(0).thread);
assertNotNull(events.get(0).context);
// spawned thread
assertTrue(events.get(1).isNew);
assertNotEquals(Thread.currentThread(), events.get(1).thread);
assertEquals(events.get(0).context, events.get(1).context);
assertFalse(events.get(2).isNew);
assertEquals(events.get(1).thread, events.get(2).thread);
assertEquals(events.get(1).context, events.get(2).context);
}
assertEquals(4, events.size());
// current thread
assertFalse(events.get(3).isNew);
assertEquals(Thread.currentThread(), events.get(3).thread);
assertEquals(events.get(0).context, events.get(3).context);
events.clear();
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ContextsEventsTest method testInnerContext.
@Test
public void testInnerContext() {
final List<ContextEvent> events;
try (Context context = Context.create()) {
Instrument testContexsInstrument = context.getEngine().getInstruments().get("testContexsInstrument");
TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
events = test.events;
context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(STATEMENT(), CONTEXT(STATEMENT()))"));
assertTrue(events.get(0).created);
assertTrue(events.get(1).languageInitialized);
assertTrue(events.get(2).created);
assertNotEquals(events.get(0).context, events.get(2).context);
assertEquals(events.get(0).context, events.get(2).context.getParent());
assertNull(events.get(2).language);
assertTrue(events.get(3).created);
assertEquals(events.get(2).context, events.get(3).context);
assertNotNull(events.get(3).language);
assertTrue(events.get(4).languageInitialized);
assertTrue(events.get(5).languageFinalized);
assertFalse(events.get(6).created);
assertEquals(InstrumentationTestLanguage.ID, events.get(6).language.getId());
assertFalse(events.get(7).created);
assertNull(events.get(7).language);
}
assertEquals(11, events.size());
assertTrue(events.get(8).languageFinalized);
assertEquals(events.get(0).context, events.get(8).context);
assertFalse(events.get(9).created);
assertEquals(InstrumentationTestLanguage.ID, events.get(9).language.getId());
assertFalse(events.get(10).created);
assertNull(events.get(10).language);
assertEquals(events.get(0).context, events.get(10).context);
}
Aggregations