use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class InstrumentationTest method testAsynchronousStacks4.
@Test
public void testAsynchronousStacks4() throws IOException, InterruptedException {
String code = "ROOT(DEFINE(af11, ROOT(STATEMENT))," + "DEFINE(af12, ROOT(CALL(af11)))," + "DEFINE(af13, ROOT(CALL(af12)))," + "DEFINE(af14, ROOT(CALL(af13)))," + "DEFINE(af21, ROOT(STATEMENT, SPAWN(af14)))," + "DEFINE(af22, ROOT(CALL(af21)))," + "DEFINE(af23, ROOT(CALL(af22)))," + "DEFINE(af24, ROOT(CALL(af23)))," + "DEFINE(f1, ROOT(STATEMENT, SPAWN(af24)))," + "DEFINE(f2, ROOT(CALL(f1)))," + "DEFINE(f3, ROOT(CALL(f2)))," + "DEFINE(f4, ROOT(CALL(f3)))," + "CALL(f4))";
Source source = Source.create(InstrumentationTestLanguage.ID, code);
teardown();
CountDownLatch instrumentationFinished = new CountDownLatch(1);
Context asyncContext = Context.newBuilder().engine(Engine.create()).allowCreateThread(true).build();
AsynchronousStacksInstrument testInstrument = new AsynchronousStacksInstrument();
testInstrument.instrumentationFinished = instrumentationFinished;
setupEnv(asyncContext, new ProxyLanguage() {
// Not to block the context's thread access
@Override
protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
return true;
}
}, testInstrument);
run(source);
instrumentationFinished.await();
run(Source.create(InstrumentationTestLanguage.ID, "JOIN()"));
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class ContextInterruptCloseCancelOrExitTest method testExitFromExitNotification.
@Test
public void testExitFromExitNotification() {
setupEnv(Context.newBuilder(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) {
return new RootNode(languageInstance) {
@Override
public Object execute(VirtualFrame frame) {
LanguageContext languageContext = LanguageContext.get(this);
languageContext.getEnv().getContext().closeExited(this, 1);
return 0;
}
}.getCallTarget();
}
@Override
protected void exitContext(LanguageContext ctx, ExitMode exitMode, int exitCode) {
ctx.getEnv().getContext().closeExited(DUMMY_NODE, 5);
}
});
try {
context.eval(ProxyLanguage.ID, "");
} catch (PolyglotException pe) {
if (!pe.isExit() || pe.getExitStatus() != 1) {
throw pe;
}
}
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class ContextInterruptCloseCancelOrExitTest method testCancelPolyglotThreadWhileClosing.
@Test
public void testCancelPolyglotThreadWhileClosing() throws ExecutionException, InterruptedException, IOException {
enterContext = false;
AtomicReference<Runnable> runOnNaturalContextExit = new AtomicReference<>();
ProxyLanguage proxyLanguage = new ProxyLanguage() {
@Override
protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
return true;
}
@Override
protected void exitContext(LanguageContext ctx, ExitMode exitMode, int exitCode) {
if (exitMode == ExitMode.NATURAL && runOnNaturalContextExit.get() != null) {
runOnNaturalContextExit.get().run();
}
}
};
setupEnv(Context.newBuilder().allowCreateThread(true), proxyLanguage);
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
CountDownLatch cancelLatch = new CountDownLatch(1);
Source source = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(DEFINE(foo,CONSTANT(42),LOOP(infinity,STATEMENT)),SPAWN(foo))", "InfiniteLoop").build();
attachListener(cancelLatch::countDown, instrumentEnv);
Future<?> future = executorService.submit(() -> {
context.eval(source);
});
cancelLatch.await();
future.get();
runOnNaturalContextExit.set(() -> {
/*
* Cancel must work even when entered.
*/
context.close(true);
});
context.close();
assertContextState("CLOSED_CANCELLED");
} finally {
executorService.shutdownNow();
executorService.awaitTermination(100, TimeUnit.SECONDS);
}
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class ContextInterruptCloseCancelOrExitTest method testNestedExitOnHost.
@Test
public void testNestedExitOnHost() {
setupEnv(Context.newBuilder(ProxyLanguage.ID, InstrumentationTestLanguage.ID).allowHostClassLoading(true).allowHostClassLookup((s) -> true).allowHostAccess(HostAccess.ALL), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) {
return new RootNode(languageInstance) {
@Override
public Object execute(VirtualFrame frame) {
LanguageContext languageContext = LanguageContext.get(this);
Object javaClass = languageContext.getEnv().lookupHostSymbol(ContextInterruptCloseCancelOrExitTest.class.getName());
try {
InteropLibrary.getUncached().invokeMember(javaClass, "callExit");
} catch (UnsupportedMessageException | ArityException | UnknownIdentifierException | UnsupportedTypeException e) {
throw new RuntimeException(e);
}
return 0;
}
}.getCallTarget();
}
@Override
protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
return true;
}
});
staticCtx = context;
try {
context.eval(ProxyLanguage.ID, "");
Assert.fail();
} catch (PolyglotException pe) {
if (!pe.isExit()) {
throw pe;
}
}
context.close();
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class MultiThreadingTest method testInitMultiThreading.
@Test
public void testInitMultiThreading() throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(2);
try {
int multiThreadingInits = 0;
int lastIterationNo = 0;
AtomicBoolean multiThreadingInitNotExecuted = new AtomicBoolean();
for (int itNo = 0; itNo < 10000 && !multiThreadingInitNotExecuted.get(); itNo++) {
lastIterationNo = itNo;
CountDownLatch enterLeaveLoopLatch = new CountDownLatch(1);
enterContext = false;
AtomicBoolean testEnd = new AtomicBoolean();
AtomicBoolean multiThreadingInitialized = new AtomicBoolean();
setupEnv(Context.newBuilder(), new ProxyLanguage() {
@Override
protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
return true;
}
@Override
protected void initializeMultiThreading(LanguageContext c) {
multiThreadingInitialized.set(true);
}
});
AtomicInteger enteredCount = new AtomicInteger();
Future<?> future1 = executorService.submit(() -> {
TruffleContext truffleContext = languageEnv.getContext();
Object prev = truffleContext.enter(INVALID_NODE);
truffleContext.leave(INVALID_NODE, prev);
enterLeaveLoopLatch.countDown();
while (!testEnd.get()) {
prev = truffleContext.enter(INVALID_NODE);
int localEnteredCount = enteredCount.incrementAndGet();
if (localEnteredCount > 1 && !multiThreadingInitialized.get()) {
multiThreadingInitNotExecuted.set(true);
}
enteredCount.decrementAndGet();
truffleContext.leave(INVALID_NODE, prev);
}
});
Future<?> future2 = executorService.submit(() -> {
TruffleContext truffleContext = languageEnv.getContext();
try {
enterLeaveLoopLatch.await();
} catch (InterruptedException ie) {
throw new AssertionError(ie);
}
Object prev = truffleContext.enter(INVALID_NODE);
int localEnteredCount = enteredCount.incrementAndGet();
if (localEnteredCount > 1 && !multiThreadingInitialized.get()) {
multiThreadingInitNotExecuted.set(true);
}
enteredCount.decrementAndGet();
truffleContext.leave(INVALID_NODE, prev);
testEnd.set(true);
});
enterLeaveLoopLatch.await();
future1.get();
future2.get();
context.close();
if (multiThreadingInitialized.get()) {
multiThreadingInits++;
}
}
Assert.assertTrue("Multi-threading was never initialized!", multiThreadingInits > 0);
Assert.assertFalse("Multi-threading was not initialized even though more than one thread was entered at the same time! lastIterationNo = " + lastIterationNo, multiThreadingInitNotExecuted.get());
} finally {
executorService.shutdownNow();
executorService.awaitTermination(100, TimeUnit.SECONDS);
}
}
Aggregations