Skip to content

Commit

Permalink
Re-use doIt CallTarget for faster test execution
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Dec 21, 2023
1 parent 8f6b973 commit 30d2802
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.junit.Assume;
import org.junit.BeforeClass;

import com.oracle.truffle.api.CallTarget;

import de.hpi.swa.trufflesqueak.exceptions.SqueakExceptions.SqueakException;
import de.hpi.swa.trufflesqueak.model.ArrayObject;
import de.hpi.swa.trufflesqueak.model.NativeObject;
Expand All @@ -48,6 +50,7 @@ public class AbstractSqueakTestCaseWithImage extends AbstractSqueakTestCase {
// For now we are single-threaded, so the flag can be static.
private static volatile boolean testWithImageIsActive;
private static ExecutorService executor;
private static CallTarget doItCallTarget;

@BeforeClass
public static void setUp() {
Expand Down Expand Up @@ -111,6 +114,12 @@ private static void patchImageForTesting() {
// Patch TestCase>>#performTest, so errors are printed to stderr for debugging purposes.
patchMethod("TestCase", "performTest", "performTest [self perform: testSelector asSymbol] on: Error do: [:e | e printVerboseOn: FileStream stderr. e signal]");
}
context.enter();
doItCallTarget = image.createDoItCallTarget("""
[((Smalltalk at: (Smalltalk getSystemAttribute: 3) asSymbol) selector: ((Smalltalk getSystemAttribute: 4) asSymbol)) runCase. '%s']
on: TestFailure, Error
do: [:e | (String streamContents: [:s | e printVerboseOn: s]) withUnixLineEndings ]""".formatted(PASSED_VALUE));
image.systemAttributes.cmdArguments = new NativeObject[]{image.asByteString("--"), null, null};
println("Image ready for testing...");
}

Expand Down Expand Up @@ -163,6 +172,17 @@ protected static Object evaluate(final String expression) {
}
}

protected static Object evaluate(final TestRequest request) {
context.enter();
image.systemAttributes.cmdArguments[1] = image.asByteString(request.testCase);
image.systemAttributes.cmdArguments[2] = image.asByteString(request.testSelector);
try {
return doItCallTarget.call();
} finally {
context.leave();
}
}

protected static void patchMethod(final String className, final String selector, final String body) {
println("Patching " + className + ">>#" + selector + "...");
final Object patchResult = evaluate(String.join(" ",
Expand Down Expand Up @@ -212,7 +232,7 @@ protected static <T, R> R runWithTimeout(final T argument, final Function<T, R>
}

private static TestResult extractFailuresAndErrorsFromTestResult(final TestRequest request) {
final Object result = evaluate(testCommand(request));
final Object result = evaluate(request);
if (!(result instanceof final NativeObject n) || !n.isByteType()) {
return TestResult.failure("did not return a ByteString, got " + result);
}
Expand All @@ -231,11 +251,6 @@ private static TestResult extractFailuresAndErrorsFromTestResult(final TestReque
}
}

private static String testCommand(final TestRequest request) {
return String.format("[(%s selector: #%s) runCase. '%s'] on: TestFailure, Error do: [:e | (String streamContents: [:s | e printVerboseOn: s]) withUnixLineEndings ]",
request.testCase, request.testSelector, PASSED_VALUE);
}

private static String shouldPassCommand(final TestRequest request) {
return String.format("[(%s selector: #%s) shouldPass] on: Error do: [:e | false]", request.testCase, request.testSelector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.file.Paths;
import java.util.HashMap;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
Expand Down Expand Up @@ -129,7 +130,7 @@ public final class SqueakImageContext {
@CompilationFinal(dimensions = 1) private byte[] resourcesPathBytes;
private final boolean isHeadless;
public final SqueakContextOptions options;
private final SqueakSystemAttributes systemAttributes = new SqueakSystemAttributes(this);
public final SqueakSystemAttributes systemAttributes = new SqueakSystemAttributes(this);

/* System */
public NativeObject clipboardTextHeadless = asByteString("");
Expand Down Expand Up @@ -253,7 +254,12 @@ public SqueakImage getSqueakImage() {

@TruffleBoundary
public Object evaluate(final String sourceCode) {
return getDoItContextNode(sourceCode, false).getCallTarget().call();
return createDoItCallTarget(sourceCode).call();
}

@TruffleBoundary
public CallTarget createDoItCallTarget(final String sourceCode) {
return getDoItContextNode(sourceCode, false).getCallTarget();
}

@TruffleBoundary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
*/
package de.hpi.swa.trufflesqueak.image;

import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand All @@ -29,7 +27,7 @@ public final class SqueakSystemAttributes {
private final SqueakImageContext image;
private final NativeObject vmPath;
@CompilationFinal private NativeObject imagePath;
@CompilationFinal(dimensions = 1) private NativeObject[] cmdArguments;
@CompilationFinal(dimensions = 1) public NativeObject[] cmdArguments;
private final NativeObject platformName;
private final NativeObject operatingSystemVersion;
private final NativeObject platformProcessorType;
Expand Down

0 comments on commit 30d2802

Please sign in to comment.