|
| 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 | +} |
0 commit comments