Skip to content

Commit

Permalink
onDeniedThreadAccess can return to request a retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaroslav Tulach committed Feb 12, 2024
1 parent b2d6f34 commit f91faeb
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext;
import com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.ThreadRequest;
import com.oracle.truffle.tck.tests.TruffleTestAssumptions;
import static org.junit.Assert.assertFalse;

public class MultiThreadedLanguageTest {

Expand Down Expand Up @@ -182,6 +183,45 @@ public void testNoThreadAllowedWithSecurityException() {
context.close();
}

@Test
public void testNoThreadAllowedRetry() {
boolean[] isThreadAccessAllowed = {false};
int[] state = {0};
Context context = Context.newBuilder(MultiThreadedLanguage.ID).onDeniedThreadAccess((t) -> {
switch (state[0]) {
case 0 -> {
// retry
assertFalse("Thread access wasn't allowed", isThreadAccessAllowed[0]);
state[0] = 1;
return;
}
case 1 -> {
// allow & retry
assertFalse("Thread access wasn't allowed", isThreadAccessAllowed[0]);
isThreadAccessAllowed[0] = true;
state[0] = 2;
return;
}
default -> // just forbid with an exception
throw t;
}
}).build();

MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
return isThreadAccessAllowed[0];
};

Value res = eval(context, (env) -> 42);
assertEquals("There were two queries - e.g. one retry", 2, state[0]);
assertEquals("Fourty two is reaturned", 42, res.asInt());

// allow again so we can close
MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
return true;
};
context.close();
}

private static void assertMultiThreadedError(Value value, Consumer<Value> valueConsumer) {
try {
valueConsumer.accept(value);
Expand Down

0 comments on commit f91faeb

Please sign in to comment.