use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class InstrumentationTest method testInstrumentsWhenForked.
@Test
public void testInstrumentsWhenForked() throws IOException {
Instrument instrument = engine.getInstruments().get("testIsNodeTaggedWith1");
assureEnabled(instrument);
TestIsNodeTaggedWith1 service = instrument.lookup(TestIsNodeTaggedWith1.class);
assertEquals(1, service.onCreateCalls);
Source otherLanguageSource = Source.create("testIsNodeTaggedWith1-lang", "STATEMENT(EXPRESSION)");
run(otherLanguageSource);
org.graalvm.polyglot.Context forked = newContext();
assertEquals(1, service.onCreateCalls);
final Map<String, ? extends Instrument> instruments = forked.getEngine().getInstruments();
assertSame(instrument, instruments.get("testIsNodeTaggedWith1"));
assertSame(service, instruments.get("testIsNodeTaggedWith1").lookup(TestIsNodeTaggedWith1.class));
assertEquals(instruments.size(), engine.getInstruments().size());
for (String key : instruments.keySet()) {
assertSame(engine.getInstruments().get(key), instruments.get(key));
}
assertEquals(0, service.onDisposeCalls);
context.close();
assertEquals(0, service.onDisposeCalls);
forked.getEngine().close();
// test if all engines are disposed
assertEquals(1, service.onDisposeCalls);
// avoid a second disposal in @After event
engine = null;
}
use of org.graalvm.polyglot.Instrument 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.Instrument 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);
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class ContextsEventsTest method testGetActiveContexts.
@Test
public void testGetActiveContexts() {
try {
TestContextsInstrument.includeActiveContexts = true;
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, "STATEMENT()"));
assertTrue(Integer.toString(events.size()), events.size() > 1);
// Have creation of polyglot context and a language
assertEquals(3, events.size());
assertTrue(events.get(0).created);
assertNotNull(events.get(0).context);
assertNull(events.get(0).language);
assertTrue(events.get(1).created);
assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
assertTrue(events.get(2).languageInitialized);
}
assertEquals(6, events.size());
Engine engine = Engine.create();
// Contexts created and closed:
try (Context context = Context.newBuilder().engine(engine).build()) {
context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
context.eval(Source.create(InstrumentationTestLanguage.ID, "CONTEXT(STATEMENT())"));
}
Instrument testBlockOnStatementsInstrument = engine.getInstruments().get("testBlockOnStatementsInstrument");
ThreadsEventsTest.TestBlockOnStatementsInstrument testBlock = testBlockOnStatementsInstrument.lookup(ThreadsEventsTest.TestBlockOnStatementsInstrument.class);
testBlock.runOnBlock = new Runnable() {
@Override
// We want to hide 'events' from the outer context not to use it by a mistake
@SuppressWarnings("hiding")
public void run() {
Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
final List<ContextEvent> events = test.events;
assertEquals(3, events.size());
assertTrue(events.get(0).created);
assertNull(events.get(0).language);
assertTrue(events.get(1).created);
assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
assertTrue(events.get(2).languageInitialized);
testBlock.blockOnStatements.set(false);
}
};
Context context = Context.newBuilder().engine(engine).build();
context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(CONTEXT(EXPRESSION()), CONTEXT(STATEMENT))"));
} finally {
TestContextsInstrument.includeActiveContexts = false;
}
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class ContextsEventsTest method testMultipleContexts.
@Test
public void testMultipleContexts() {
final int numContexts = 5;
final List<ContextEvent> events;
try (Engine engine = Engine.create()) {
Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
events = test.events;
Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
for (int i = 0; i < numContexts; i++) {
try (Context context = Context.newBuilder().engine(engine).build()) {
assertEquals(6 * i + 1, events.size());
context.eval(source);
}
assertEquals(6 * i + 6, events.size());
}
assertEquals(6 * numContexts, events.size());
TruffleContext lastContext = null;
for (int i = 0; i < numContexts; i++) {
int ci = 6 * i;
assertTrue(events.get(ci).created);
assertNull(events.get(ci).language);
assertNotEquals(lastContext, events.get(ci).context);
lastContext = events.get(ci).context;
assertTrue(events.get(ci + 1).created);
assertNotNull(events.get(ci + 1).language);
assertEquals(lastContext, events.get(ci + 1).context);
assertTrue(events.get(ci + 2).languageInitialized);
assertTrue(events.get(ci + 3).languageFinalized);
assertNotNull(events.get(ci + 4).language);
assertNull(events.get(ci + 5).language);
}
}
// No more events
assertEquals(6 * numContexts, events.size());
}
Aggregations