use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class ThreadsEventsTest method testSpawnManyThread.
@Test
public void testSpawnManyThread() {
final int numThreads = 100;
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), LOOP(" + numThreads + ", SPAWN(foo)), JOIN())"));
}
assertEquals(2 + 2 * numThreads, events.size());
List<ThreadEvent> startEvents = new ArrayList<>(1 + numThreads);
for (ThreadEvent event : events) {
if (event.isNew) {
startEvents.add(event);
}
}
assertEquals(1 + numThreads, startEvents.size());
TruffleContext tcontext = events.get(0).context;
for (ThreadEvent event : events) {
assertEquals(tcontext, event.context);
}
events.clear();
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class ThreadsEventsTest method testContextThreads.
@Test
public void testContextThreads() throws Exception {
Engine engine = Engine.create();
Instrument testThreadsInstrument = engine.getInstruments().get("testThreadsInstrument");
TestThreadsInstrument test = testThreadsInstrument.lookup(TestThreadsInstrument.class);
final List<ThreadEvent> events = test.events;
Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
int numThreads = 10;
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++) {
threads[i] = new Thread() {
@Override
public void run() {
try (Context context = Context.newBuilder().engine(engine).build()) {
context.eval(source);
}
}
};
}
for (int i = 0; i < numThreads; i++) {
threads[i].start();
}
for (int i = 0; i < numThreads; i++) {
threads[i].join();
}
assertEquals(2 * numThreads, events.size());
List<ThreadEvent> startEvents = new ArrayList<>(numThreads);
for (ThreadEvent event : events) {
if (event.isNew) {
startEvents.add(event);
}
}
assertEquals(numThreads, startEvents.size());
// Verify that we got all threads in the events:
Set<Thread> allThreads = new HashSet<>(Arrays.asList(threads));
for (int i = 0; i < numThreads; i++) {
assertTrue(allThreads.remove(startEvents.get(i).thread));
}
assertTrue(allThreads.toString(), allThreads.isEmpty());
// Verify that we got 'numThreads' number of contexts:
Set<TruffleContext> distinctContexts = new HashSet<>();
for (ThreadEvent event : events) {
distinctContexts.add(event.context);
}
assertEquals(numThreads, distinctContexts.size());
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class ThreadsEventsTest method testSingleThread.
@Test
public void testSingleThread() {
final List<ThreadEvent> events;
try (Context context = Context.create()) {
Instrument testThreadsInstrument = context.getEngine().getInstruments().get("testThreadsInstrument");
TestThreadsInstrument test = testThreadsInstrument.lookup(TestThreadsInstrument.class);
events = test.events;
context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
assertEquals(1, events.size());
assertTrue(events.get(0).isNew);
assertEquals(Thread.currentThread(), events.get(0).thread);
assertNotNull(events.get(0).context);
}
assertEquals(2, events.size());
assertFalse(events.get(1).isNew);
assertEquals(Thread.currentThread(), events.get(1).thread);
assertEquals(events.get(0).context, events.get(1).context);
events.clear();
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class DebuggerExampleTest method setupDebugger.
@Before
public void setupDebugger() throws IOException {
// BEGIN: DebuggerExampleTest
Instrument instrument = engine.getInstruments().get(DebuggerExample.ID);
assert !isCreated(instrument) : "Not enabled yet";
debugger = instrument.lookup(DebuggerController.class);
assert isCreated(instrument) : "Got enabled";
assert debugger != null : "We can control the debugger";
// END: DebuggerExampleTest
assertTrue("Enabled by requesting registered services class", isCreated(instrument));
assertNotNull("Debugger interface found", debugger);
DebuggerExample itself = instrument.lookup(DebuggerExample.class);
assertNull("Debugger instrument itself isn't found", itself);
TruffleInstrument instr = instrument.lookup(TruffleInstrument.class);
assertNull("Instrument itself isn't found", instr);
// ensure debugger gets loaded
assertEvalOut("", "");
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class ContextsEventsTest method testContextsNotCloseable.
@Test
public void testContextsNotCloseable() {
try (Engine engine = Engine.create()) {
Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
final List<ContextEvent> events = test.events;
try (Context context = Context.newBuilder().engine(engine).build()) {
context.eval(Source.create(InstrumentationTestLanguage.ID, "CONTEXT(STATEMENT())"));
for (ContextEvent event : events) {
try {
event.context.close();
fail("Context close should fail.");
} catch (UnsupportedOperationException ex) {
// O.K.
}
}
}
}
}
Aggregations