Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: I can interrupt an expression evaluation #824

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
vicmosin marked this conversation as resolved.
Show resolved Hide resolved
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()
}
vicmosin marked this conversation as resolved.
Show resolved Hide resolved

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)
}
}