Search in sources :

Example 81 with Context

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);
}
Also used : Context(org.graalvm.polyglot.Context) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 82 with Context

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);
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) Timer(java.util.Timer) TimerTask(java.util.TimerTask) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 83 with Context

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());
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 84 with Context

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();
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 85 with Context

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);
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Aggregations

Context (org.graalvm.polyglot.Context)185 Test (org.junit.Test)148 Value (org.graalvm.polyglot.Value)58 TruffleContext (com.oracle.truffle.api.TruffleContext)56 Env (com.oracle.truffle.api.TruffleLanguage.Env)41 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)37 Engine (org.graalvm.polyglot.Engine)32 ArrayList (java.util.ArrayList)29 PolyglotException (org.graalvm.polyglot.PolyglotException)24 Source (org.graalvm.polyglot.Source)22 Path (java.nio.file.Path)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)20 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)19 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)17 IOException (java.io.IOException)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 EventContext (com.oracle.truffle.api.instrumentation.EventContext)13 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)12