Skip to content

Commit 8c89abd

Browse files
Test to make sure HelloWorld.ir gets loaded and is used on subsequent executions
1 parent dfe42a8 commit 8c89abd

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.enso.interpreter.caches;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.File;
7+
import java.io.PrintStream;
8+
import java.util.logging.Level;
9+
import java.util.stream.Collectors;
10+
import org.enso.common.RuntimeOptions;
11+
import org.enso.test.utils.ContextUtils;
12+
import org.graalvm.polyglot.Source;
13+
import org.junit.Test;
14+
15+
public class HelloWorldCacheTest {
16+
17+
@Test
18+
public void loadingHelloWorldTwiceUsesCaching() throws Exception {
19+
var root = new File("../..").getAbsoluteFile();
20+
assertTrue("build.sbt exists at " + root, new File(root, "build.sbt").exists());
21+
var helloWorld = children(root, "test", "Benchmarks", "src", "Startup", "Hello_World.enso");
22+
assertTrue("Hello_World.enso found", helloWorld.exists());
23+
24+
// the first run may or may not use caches
25+
var firstMsgs = executeOnce(helloWorld);
26+
assertTrue("Contains hello world:\n" + firstMsgs, firstMsgs.endsWith("Hello World"));
27+
// after the first run the caches for Hello_World.enso must be generated
28+
29+
// the second run must read Hello_World from its .ir file!
30+
var secondMsgs = executeOnce(helloWorld);
31+
assertTrue("Contains hello world:\n" + secondMsgs, secondMsgs.contains("Hello World"));
32+
assertTrue(
33+
"Properly deserialized:\n" + secondMsgs,
34+
secondMsgs.contains("Deserializing module Hello_World from IR file: true"));
35+
}
36+
37+
private static String executeOnce(File src) throws Exception {
38+
var backLog = new ByteArrayOutputStream();
39+
var log = new PrintStream(backLog);
40+
41+
try (var ctx =
42+
ContextUtils.defaultContextBuilder()
43+
.out(log)
44+
.err(log)
45+
.logHandler(log)
46+
.option(RuntimeOptions.LOG_LEVEL, Level.FINE.getName())
47+
.option(RuntimeOptions.DISABLE_IR_CACHES, "false")
48+
.option(RuntimeOptions.PROJECT_ROOT, findBenchmarks(src).getAbsolutePath())
49+
.build()) {
50+
var code = Source.newBuilder("enso", src).build();
51+
var res = ContextUtils.evalModule(ctx, code, "main");
52+
assertTrue("Result of IO.println is Nothing", res.isNull());
53+
}
54+
return backLog
55+
.toString()
56+
.lines()
57+
.filter(l -> l.toUpperCase().contains("HELLO"))
58+
.collect(Collectors.joining("\n"));
59+
}
60+
61+
private static File children(File f, String... names) {
62+
for (var n : names) {
63+
f = new File(f, n);
64+
}
65+
return f;
66+
}
67+
68+
private static File findBenchmarks(File f) {
69+
for (; ; ) {
70+
if (f.getName().equals("Benchmarks")) {
71+
return f;
72+
}
73+
f = f.getParentFile();
74+
}
75+
}
76+
}

lib/java/test-utils/src/main/java/org/enso/test/utils/ContextUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,19 @@ public static Value evalModule(Context ctx, CharSequence src, String name, Strin
162162
var b = Source.newBuilder("enso", src, name);
163163
s = b.buildLiteral();
164164
}
165-
var module = ctx.eval(s);
165+
return evalModule(ctx, s, methodName);
166+
}
167+
168+
/**
169+
* Evaluates the given source as if it was in a module with given name.
170+
*
171+
* @param ctx context to evaluate the module at
172+
* @param src The source code of the module
173+
* @param methodName name of main method to invoke
174+
* @return The value returned from the main method of the unnamed module.
175+
*/
176+
public static Value evalModule(Context ctx, Source src, String methodName) {
177+
var module = ctx.eval(src);
166178
var assocType = module.invokeMember(Module.GET_ASSOCIATED_TYPE);
167179
var method = module.invokeMember(Module.GET_METHOD, assocType, methodName);
168180
return "main".equals(methodName) ? method.execute() : method.execute(assocType);

0 commit comments

Comments
 (0)