Permalink
Browse files

Small patches for timed-calc post-1.12-merge

  • Loading branch information...
kenzierocks committed Oct 1, 2018
1 parent 9f3027a commit 5b3c9a39204acb1cd63d93c3fbdb19aca11f2b7b
@@ -136,7 +136,7 @@ files:
history:
size: 15
expiration: 10
calculation:
timeout: 100
@@ -146,4 +146,4 @@ no-double-slash: false
no-op-permissions: false
debug: false
show-help-on-first-use: true
server-side-cui: true
server-side-cui: true
@@ -84,6 +84,17 @@
.setNameFormat("worldedit-expression-eval-%d")
.build());
// A workaround for testing -- we can't easily create a LocalConfiguration
static int testCalculationTimeout = Integer.MIN_VALUE;
private static int getCalcTimeout() {
if (testCalculationTimeout != Integer.MIN_VALUE) {
return testCalculationTimeout;
}
return WorldEdit.getInstance().getConfiguration().calculationTimeout;
}
private final Map<String, RValue> variables = new HashMap<>();
private final String[] variableNames;
private RValue root;
@@ -139,7 +150,7 @@ public Double call() throws Exception {
}
});
try {
return result.get(WorldEdit.getInstance().getConfiguration().calculationTimeout, TimeUnit.MILLISECONDS);
return result.get(getCalcTimeout(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
@@ -181,20 +192,20 @@ public static Expression getInstance() {
}
private void pushInstance() {
Stack<Expression> foo = instance.get();
if (foo == null) {
instance.set(foo = new Stack<>());
Stack<Expression> threadLocalExprStack = instance.get();
if (threadLocalExprStack == null) {
instance.set(threadLocalExprStack = new Stack<>());
}
foo.push(this);
threadLocalExprStack.push(this);
}
private void popInstance() {
Stack<Expression> foo = instance.get();
Stack<Expression> threadLocalExprStack = instance.get();
foo.pop();
threadLocalExprStack.pop();
if (foo.isEmpty()) {
if (threadLocalExprStack.isEmpty()) {
instance.set(null);
}
}

This file was deleted.

Oops, something went wrong.
@@ -22,13 +22,12 @@
import static java.lang.Math.atan2;
import static java.lang.Math.sin;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.event.platform.PlatformReadyEvent;
import com.sk89q.worldedit.internal.expression.lexer.LexerException;
import com.sk89q.worldedit.internal.expression.parser.ParserException;
import com.sk89q.worldedit.internal.expression.runtime.EvaluationException;
@@ -37,8 +36,7 @@
public class ExpressionTest {
@Before
public void setup() {
WorldEdit.getInstance().getPlatformManager().register(new ExpressionPlatform());
WorldEdit.getInstance().getPlatformManager().handlePlatformReady(new PlatformReadyEvent());
Expression.testCalculationTimeout = 100;
}
@Test
@@ -172,6 +170,16 @@ public void testQuery() throws Exception {
assertEquals(1, simpleEval("!queryRel(3,4,5,100,200)"), 0);
}
@Test
public void testTimeout() throws Exception {
try {
simpleEval("for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){for(l=0;l<256;l++){ln(pi)}}}}");
fail("Loop was not stopped.");
} catch (EvaluationException e) {
assertTrue(e.getMessage().contains("Calculations exceeded time limit"));
}
}
private double simpleEval(String expressionString) throws ExpressionException {
final Expression expression = compile(expressionString);

0 comments on commit 5b3c9a3

Please sign in to comment.