Skip to content

Commit

Permalink
Merge pull request #824 from vicmosin/main
Browse files Browse the repository at this point in the history
feat: I can interrupt an expression evaluation
  • Loading branch information
saig0 authored Apr 18, 2024
2 parents d20bc24 + 41131fe commit 24ba9cb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ import java.time.{Duration, Period}
*/
class FeelInterpreter {

def eval(expression: Exp)(implicit context: EvalContext): Val =
def eval(expression: Exp)(implicit context: EvalContext): Val = {
// Check if the current thread was interrupted, otherwise long-running evaluations can not be interrupted and fully block the thread
if (Thread.interrupted()) {
throw new InterruptedException()
}

expression match {

// literals
Expand Down Expand Up @@ -269,6 +274,7 @@ class FeelInterpreter {
case exp => error(EvaluationFailureType.UNKNOWN, s"Unsupported expression '$exp'")

}
}

private def mapEither[T, R](
it: Iterable[T],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.feel.impl.interpreter

import org.camunda.feel.impl.FeelEngineTest
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import java.util.concurrent.{Executors, TimeUnit}
import concurrent.ExecutionContext

/** @author
* Victor Mosin
*/
class InterpreterInterruptionTest extends AnyFlatSpec with Matchers with FeelEngineTest {

protected implicit val context =
ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor())

"A long-running evaluation invocation" should "be interrupted after X time" in {
val countDownLatch = new java.util.concurrent.CountDownLatch(1)
val thread = new Thread {
override def run {
try {
evaluateExpression(
expression = "count(for x in 1..(2 ** 16) return {\"power\": 2 ** x}) > 0"
)
} catch {
case _: InterruptedException =>
countDownLatch.countDown()
}
}
}

thread.start()
Thread.sleep(100) // Let evaluation start
thread.interrupt()
val wasInterrupted = countDownLatch.await(1, TimeUnit.SECONDS)

wasInterrupted should be(true)
}
}

0 comments on commit 24ba9cb

Please sign in to comment.