Search in sources :

Example 26 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class InstrumentationTest method testMultipleInstruments.

/*
     * Test onCreate and onDispose invocations for multiple instrument instances.
     */
@Test
public void testMultipleInstruments() throws IOException {
    // initialize
    run("");
    MultipleInstanceInstrument.onCreateCounter = 0;
    MultipleInstanceInstrument.onDisposeCounter = 0;
    MultipleInstanceInstrument.constructor = 0;
    Instrument instrument1 = engine.getInstruments().get("testMultipleInstruments");
    // enabled
    instrument1.lookup(Object.class);
    Assert.assertEquals(1, MultipleInstanceInstrument.constructor);
    Assert.assertEquals(1, MultipleInstanceInstrument.onCreateCounter);
    Assert.assertEquals(0, MultipleInstanceInstrument.onDisposeCounter);
    Instrument instrument2 = engine.getInstruments().get("testMultipleInstruments");
    // the same enabled
    instrument2.lookup(Object.class);
    Assert.assertEquals(1, MultipleInstanceInstrument.constructor);
    Assert.assertEquals(1, MultipleInstanceInstrument.onCreateCounter);
    Assert.assertEquals(0, MultipleInstanceInstrument.onDisposeCounter);
    engine.close();
    engine = null;
    Assert.assertEquals(1, MultipleInstanceInstrument.constructor);
    Assert.assertEquals(1, MultipleInstanceInstrument.onCreateCounter);
    Assert.assertEquals(1, MultipleInstanceInstrument.onDisposeCounter);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 27 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class InstrumentationTest method testAccessLanguages.

@Test
public void testAccessLanguages() {
    Instrument instrument = engine.getInstruments().get("testAccessInstruments");
    TestAccessInstruments access = instrument.lookup(TestAccessInstruments.class);
    LanguageInfo info = access.env.getLanguages().get(InstrumentationTestLanguage.ID);
    assertNotNull(info);
    assertTrue(info.getMimeTypes().contains(InstrumentationTestLanguage.MIME_TYPE));
    assertEquals("InstrumentTestLang", info.getName());
    assertEquals("2.0", info.getVersion());
    assertNotNull(access.env.lookup(info, SpecialService.class));
    assertEquals(InstrumentationTestLanguage.FILENAME_EXTENSION, access.env.lookup(info, SpecialService.class).fileExtension());
}
Also used : LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 28 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class InstrumentationTest method testQueryTags1.

/*
     * Test behavior of queryTags when used with instruments
     */
@Test
public void testQueryTags1() throws IOException {
    Instrument instrument = engine.getInstruments().get("testIsNodeTaggedWith1");
    Instrumenter instrumenter = instrument.lookup(Instrumenter.class);
    TestIsNodeTaggedWith1.expressionNode = null;
    TestIsNodeTaggedWith1.statementNode = null;
    Assert.assertTrue(instrumenter.queryTags(new Node() {
    }).isEmpty());
    run("STATEMENT(EXPRESSION)");
    assertTags(instrumenter.queryTags(TestIsNodeTaggedWith1.expressionNode), InstrumentationTestLanguage.EXPRESSION);
    assertTags(instrumenter.queryTags(TestIsNodeTaggedWith1.statementNode), InstrumentationTestLanguage.STATEMENT);
    try {
        instrumenter.queryTags(null);
        Assert.fail();
    } catch (NullPointerException e) {
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) DirectCallNode(com.oracle.truffle.api.nodes.DirectCallNode) ExecutableNode(com.oracle.truffle.api.nodes.ExecutableNode) Node(com.oracle.truffle.api.nodes.Node) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Instrumenter(com.oracle.truffle.api.instrumentation.Instrumenter) Test(org.junit.Test)

Example 29 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class InstrumentationTest method forgetsToRegisterADeclaredService.

@Test
public void forgetsToRegisterADeclaredService() throws Exception {
    Instrument handle = engine.getInstruments().get("testBrokenRegistration");
    assertNotNull(handle);
    Runnable r = handle.lookup(Runnable.class);
    assertNull("The service isn't there", r);
    if (!err.toString().contains("declares service java.lang.Runnable but doesn't register it")) {
        fail(err.toString());
    }
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 30 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class InstrumentationTest method queryInstrumentsAfterDisposeDoesnotEnable.

// IllegalStateException: Engine is already closed
@Test(expected = IllegalStateException.class)
public void queryInstrumentsAfterDisposeDoesnotEnable() throws Exception {
    engine = Engine.newBuilder().err(err).build();
    engine.close();
    Runnable start = null;
    for (Instrument instr : engine.getInstruments().values()) {
        assertFalse("Instrument is disabled", isInitialized(instr));
        Runnable r = instr.lookup(Runnable.class);
        if (r != null) {
            start = r;
            start.run();
            assertTrue("Now enabled: " + instr, isCreated(instr));
        }
        assertFalse("Instrument left disabled", isInitialized(instr));
    }
    assertNull("No Runnable found", start);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Aggregations

Instrument (org.graalvm.polyglot.Instrument)46 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)37 Test (org.junit.Test)33 Source (org.graalvm.polyglot.Source)14 Context (org.graalvm.polyglot.Context)12 TruffleContext (com.oracle.truffle.api.TruffleContext)11 Engine (org.graalvm.polyglot.Engine)10 EventContext (com.oracle.truffle.api.instrumentation.EventContext)7 ArrayList (java.util.ArrayList)5 Node (com.oracle.truffle.api.nodes.Node)4 RootNode (com.oracle.truffle.api.nodes.RootNode)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Language (org.graalvm.polyglot.Language)3 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 Instrumenter (com.oracle.truffle.api.instrumentation.Instrumenter)2 DirectCallNode (com.oracle.truffle.api.nodes.DirectCallNode)2 ExecutableNode (com.oracle.truffle.api.nodes.ExecutableNode)2 List (java.util.List)2 OptionDescriptor (org.graalvm.options.OptionDescriptor)2