Skip to content

Commit

Permalink
Cache runtime test templates to static field
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Kochurkin <kvanttt@gmail.com>
  • Loading branch information
KvanTTT committed Jun 19, 2022
1 parent c9f5a77 commit 8b757f6
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupString;
import org.stringtemplate.v4.StringRenderer;
import org.stringtemplate.v4.*;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -43,9 +41,9 @@
public abstract class RuntimeTests {
protected abstract RuntimeRunner createRuntimeRunner();

private final static StringRenderer rendered = new StringRenderer();

private final static HashMap<String, RuntimeTestDescriptor[]> testDescriptors = new HashMap<>();
private final static HashMap<String, STGroup> cachedTargetTemplates = new HashMap<>();
private final static StringRenderer rendered = new StringRenderer();

static {
File descriptorsDir = new File(Paths.get(RuntimeTestUtils.resourcePath.toString(), "org/antlr/v4/test/runtime/descriptors").toString());
Expand Down Expand Up @@ -119,10 +117,20 @@ private static void test(RuntimeTestDescriptor descriptor, RuntimeRunner runner)

FileUtils.mkdir(runner.getTempDirPath());

String sourceName = "org/antlr/v4/test/runtime/templates/" + targetName + ".test.stg";
String template = RuntimeTestUtils.getTextFromResource(sourceName);
STGroup targetTemplates = new STGroupString(sourceName, template, '<', '>');
targetTemplates.registerRenderer(String.class, rendered);
STGroup targetTemplates = cachedTargetTemplates.get(targetName);
if (targetTemplates == null) {
synchronized (cachedTargetTemplates) {
targetTemplates = cachedTargetTemplates.get(targetName);
if (targetTemplates == null) {
ClassLoader classLoader = RuntimeTests.class.getClassLoader();
URL templates = classLoader.getResource("org/antlr/v4/test/runtime/templates/" + targetName + ".test.stg");
assert templates != null;
targetTemplates = new STGroupFile(templates, "UTF-8", '<', '>');
targetTemplates.registerRenderer(String.class, rendered);
cachedTargetTemplates.put(targetName, targetTemplates);
}
}
}

// write out any slave grammars
List<Pair<String, String>> slaveGrammars = descriptor.slaveGrammars;
Expand Down

0 comments on commit 8b757f6

Please sign in to comment.