Skip to content

Commit

Permalink
[GR-35797] TCK tests do not handle IAE on Value#execute.
Browse files Browse the repository at this point in the history
  • Loading branch information
tzezula committed Jun 30, 2022
1 parent 6d51160 commit 394b04a
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -67,8 +67,9 @@ public interface ResultVerifier extends Consumer<ResultVerifier.SnippetRun> {
* verifier.
*
* @param snippetRun the snippet execution data. The {@link SnippetRun} provides the actual
* snippet parameters, the execution result or the {@link PolyglotException} thrown
* by the execution.
* snippet parameters, the execution result, or the {@link PolyglotException} thrown
* by the execution. If the snippet execution throws an
* {@link IllegalArgumentException}, it's converted to {@link PolyglotException}.
* @throws PolyglotException may propagate the {@link PolyglotException} from the snippetRun
* @throws AssertionError may throw an {@link AssertionError} as a result of a verification
* @since 0.30
Expand Down Expand Up @@ -115,7 +116,10 @@ public Value getResult() {
}

/**
* Returns the {@link PolyglotException} thrown during snippet execution.
* Returns the {@link PolyglotException} thrown during snippet execution. If the snippet
* execution throws an {@link IllegalArgumentException}, this exception is first converted
* to a {@link PolyglotException}, and the resulting {@link PolyglotException} is provided
* by this method.
*
* @return the {@link PolyglotException} thrown during the execution or null in case of
* successful execution.
Expand Down
4 changes: 4 additions & 0 deletions truffle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This changelog summarizes major changes between Truffle versions relevant to languages implementors building upon the Truffle framework. The main focus is on APIs exported by Truffle.

## Version 22.3.0

* (GR-35797) The [SnippetRun#getException()](https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/tck/ResultVerifier.SnippetRun.html#getException--) now provides an `IllegalArgumentException` thrown during the snippet execution. The `IllegalArgumentException` is converted to a `PolyglotException` before it is returned.

## Version 22.2.0

* GR-33829 Added support on libgraal for caching encoded graphs across Truffle compilations to speedup partial evaluation. The cache is enabled by default and can be enabled/disabled with the `--engine.EncodedGraphCache` option.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -44,6 +44,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -52,7 +53,6 @@
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import org.graalvm.polyglot.PolyglotException;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -74,7 +74,7 @@ public class ErrorTypeTest {
public static Collection<? extends TestRun> createErrorTypeTests() {
context = new TestContext(ErrorTypeTest.class);
final Set<? extends String> requiredLanguages = TestUtil.getRequiredLanguages(context);
final Collection<TestRun> testRuns = new TreeSet<>((a, b) -> a.toString().compareTo(b.toString()));
final Collection<TestRun> testRuns = new TreeSet<>(Comparator.comparing(TestRun::toString));
for (String snippetLanguage : requiredLanguages) {
Collection<? extends Snippet> snippets = context.getExpressions(null, null, snippetLanguage);
Map<String, Collection<? extends Snippet>> overloads = computeOverloads(snippets);
Expand Down Expand Up @@ -113,7 +113,7 @@ private static void computeSnippets(
if (snippetLanguage.equals(parLanguage)) {
continue;
}
final Collection<Map.Entry<String, ? extends Snippet>> valueConstructors = new TreeSet<>((a, b) -> a.getValue().getId().compareTo(b.getValue().getId()));
final Collection<Map.Entry<String, ? extends Snippet>> valueConstructors = new TreeSet<>(Comparator.comparing(a -> a.getValue().getId()));
for (Snippet valueConstructor : context.getValueConstructors(null, parLanguage)) {
valueConstructors.add(new AbstractMap.SimpleImmutableEntry<>(parLanguage, valueConstructor));
}
Expand All @@ -136,12 +136,7 @@ private static void computeSnippets(
private static Map<String, Collection<? extends Snippet>> computeOverloads(final Collection<? extends Snippet> snippets) {
final Map<String, Collection<Snippet>> res = new HashMap<>();
for (Snippet snippet : snippets) {
res.computeIfAbsent(snippet.getId(), new Function<String, Collection<Snippet>>() {
@Override
public Collection<Snippet> apply(String id) {
return new ArrayList<>();
}
}).add(snippet);
res.computeIfAbsent(snippet.getId(), id -> new ArrayList<>()).add(snippet);
}
return (Map<String, Collection<? extends Snippet>>) (Map<String, ?>) res;
}
Expand Down Expand Up @@ -244,18 +239,24 @@ public void testErrorType() {
Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
boolean passed = false;
try {
PolyglotException polyglotException = null;
try {
testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
} catch (PolyglotException pe) {
} catch (PolyglotException e) {
polyglotException = e;
} catch (IllegalArgumentException e) {
polyglotException = context.getContext().asValue(e).as(PolyglotException.class);
}
if (polyglotException != null) {
try {
TestUtil.validateResult(testRun, null, pe, true);
TestUtil.validateResult(testRun, polyglotException);
} catch (PolyglotException | AssertionError e) {
if (pe.equals(e)) {
if (polyglotException.equals(e)) {
passed = true;
} else {
throw new AssertionError(
TestUtil.formatErrorMessage(
"Unexpected Exception: " + e.getMessage() + ", expected: " + pe.getMessage(),
"Unexpected Exception: " + e.getMessage() + ", expected: " + polyglotException.getMessage(),
testRun,
context),
e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -43,15 +43,13 @@
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import org.graalvm.polyglot.Value;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.tck.Snippet;
import org.junit.AfterClass;

@RunWith(Parameterized.class)
Expand All @@ -67,18 +65,8 @@ public static Collection<? extends TestRun> createExpressionTests() {
final Collection<? extends TestRun> testRuns = TestUtil.createTestRuns(
TestUtil.getRequiredLanguages(context),
TestUtil.getRequiredValueLanguages(context),
new Function<String, Collection<? extends Snippet>>() {
@Override
public Collection<? extends Snippet> apply(String lang) {
return context.getExpressions(null, null, lang);
}
},
new Function<String, Collection<? extends Snippet>>() {
@Override
public Collection<? extends Snippet> apply(String lang) {
return context.getValueConstructors(null, lang);
}
});
lang -> context.getExpressions(null, null, lang),
lang -> context.getValueConstructors(null, lang));
if (testRuns.isEmpty()) {
// BeforeClass and AfterClass annotated methods are not called when there are no tests
// to run. But we need to free TestContext.
Expand Down Expand Up @@ -108,12 +96,18 @@ public void testExpression() {
Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
boolean success = false;
try {
Value result = null;
try {
final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
TestUtil.validateResult(testRun, result, null, true);
result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
} catch (IllegalArgumentException e) {
TestUtil.validateResult(testRun, context.getContext().asValue(e).as(PolyglotException.class));
success = true;
} catch (PolyglotException pe) {
TestUtil.validateResult(testRun, null, pe, true);
} catch (PolyglotException e) {
TestUtil.validateResult(testRun, e);
success = true;
}
if (result != null) {
TestUtil.validateResult(testRun, result, true);
success = true;
}
} catch (PolyglotException | AssertionError e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -40,12 +40,6 @@
*/
package com.oracle.truffle.tck.tests;

import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;

import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.tck.LanguageProvider;
Expand All @@ -57,6 +51,11 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

@RunWith(Parameterized.class)
public class IdentityFunctionTest {

Expand All @@ -70,18 +69,8 @@ public static Collection<? extends TestRun> createExpressionTests() {
final Collection<? extends TestRun> testRuns = TestUtil.createTestRuns(
TestUtil.getRequiredLanguages(context),
TestUtil.getRequiredValueLanguages(context),
new Function<String, Collection<? extends Snippet>>() {
@Override
public Collection<? extends Snippet> apply(String lang) {
return Arrays.asList(createIdentitySnippet(lang));
}
},
new Function<String, Collection<? extends Snippet>>() {
@Override
public Collection<? extends Snippet> apply(String lang) {
return context.getValueConstructors(null, lang);
}
});
lang -> List.of(createIdentitySnippet(lang)),
lang -> context.getValueConstructors(null, lang));
if (testRuns.isEmpty()) {
// BeforeClass and AfterClass annotated methods are not called when there are no tests
// to run. But we need to free TestContext.
Expand Down Expand Up @@ -116,12 +105,18 @@ public void testIdentityFunction() {
Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
boolean success = false;
try {
Value result = null;
try {
final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
TestUtil.validateResult(testRun, result, null, true);
result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
} catch (IllegalArgumentException e) {
TestUtil.validateResult(testRun, context.getContext().asValue(e).as(PolyglotException.class));
success = true;
} catch (PolyglotException pe) {
TestUtil.validateResult(testRun, null, pe, true);
} catch (PolyglotException e) {
TestUtil.validateResult(testRun, e);
success = true;
}
if (result != null) {
TestUtil.validateResult(testRun, result, true);
success = true;
}
} catch (PolyglotException | AssertionError e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -43,6 +43,7 @@
import com.oracle.truffle.tck.common.inline.InlineVerifier;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Comparator;
import java.util.Objects;
import java.util.TreeSet;

Expand All @@ -66,7 +67,7 @@ public class InlineExecutionTest {
@Parameterized.Parameters(name = "{0}")
public static Collection<InlineTestRun> createScriptTests() {
context = new TestContext(InlineExecutionTest.class);
final Collection<InlineTestRun> res = new TreeSet<>((a, b) -> a.toString().compareTo(b.toString()));
final Collection<InlineTestRun> res = new TreeSet<>(Comparator.comparing(TestRun::toString));
for (String lang : TestUtil.getRequiredLanguages(context)) {
for (InlineSnippet snippet : context.getInlineScripts(lang)) {
res.add(new InlineTestRun(new AbstractMap.SimpleImmutableEntry<>(lang, snippet.getScript()), snippet));
Expand Down Expand Up @@ -110,12 +111,18 @@ public void testInline() throws Exception {
context.getContext().initialize(testRun.getID());
context.setInlineSnippet(testRun.getID(), inlineSnippet, verifier);
try {
Value result = null;
try {
final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
TestUtil.validateResult(testRun, result, null, true);
result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
} catch (IllegalArgumentException e) {
TestUtil.validateResult(testRun, context.getContext().asValue(e).as(PolyglotException.class));
success = true;
} catch (PolyglotException pe) {
TestUtil.validateResult(testRun, null, pe, true);
} catch (PolyglotException e) {
TestUtil.validateResult(testRun, e);
success = true;
}
if (result != null) {
TestUtil.validateResult(testRun, result, true);
success = true;
}
if (verifier != null && verifier.exception != null) {
Expand Down Expand Up @@ -143,14 +150,14 @@ private class TestResultVerifier implements InlineVerifier.ResultVerifier {
public void verify(Object ret) {
Value result = context.getValue(ret);
InlineSnippet inlineSnippet = testRun.getInlineSnippet();
TestUtil.validateResult(inlineSnippet.getResultVerifier(), testRun, result, null, true);
TestUtil.validateResult(inlineSnippet.getResultVerifier(), testRun, result, true);
}

@Override
public void verify(PolyglotException pe) {
InlineSnippet inlineSnippet = testRun.getInlineSnippet();
try {
TestUtil.validateResult(inlineSnippet.getResultVerifier(), testRun, null, pe, true);
TestUtil.validateResult(inlineSnippet.getResultVerifier(), testRun, pe);
} catch (Exception exc) {
exception = exc;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -42,6 +42,7 @@

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Comparator;
import java.util.Objects;
import java.util.TreeSet;

Expand All @@ -63,7 +64,7 @@ public class InvalidSyntaxTest {
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> createInvalidSyntaxTests() {
context = new TestContext(InvalidSyntaxTest.class);
final Collection<Object[]> result = new TreeSet<>((a, b) -> ((String) a[0]).compareTo(((String) b[0])));
final Collection<Object[]> result = new TreeSet<>(Comparator.comparing(a -> ((String) a[0])));
for (String language : TestUtil.getRequiredLanguages(context)) {
for (Source src : context.getInstalledProviders().get(language).createInvalidSyntaxScripts(context.getContext())) {
result.add(new Object[]{
Expand Down
Loading

0 comments on commit 394b04a

Please sign in to comment.