From 6256638746fbdacd854cd057e357255202dc7dd3 Mon Sep 17 00:00:00 2001 From: andrecs <12188364+andrecsilva@users.noreply.github.com> Date: Tue, 17 Dec 2024 08:40:22 -0300 Subject: [PATCH 1/2] Added utility to execute functions within timeout --- .../pixee/security/ExecuteWithTimeout.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/io/github/pixee/security/ExecuteWithTimeout.java diff --git a/src/main/java/io/github/pixee/security/ExecuteWithTimeout.java b/src/main/java/io/github/pixee/security/ExecuteWithTimeout.java new file mode 100644 index 0000000..dcf0f78 --- /dev/null +++ b/src/main/java/io/github/pixee/security/ExecuteWithTimeout.java @@ -0,0 +1,25 @@ +package io.github.pixee.security; + +import java.util.concurrent.Callable; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +/** + * Holds utilities for executing methods and functions within a timeout. + */ +public class ExecuteWithTimeout{ + + + /** + * Tries to execute a given {@link Callable} within a given timeout in milliseconds. Returns the result if successful, otherwise throws a {@link RuntimeException}. + */ + public E executeWithTimeout(final Callable action, final int timeout) { + Future maybeResult = Executors.newSingleThreadExecutor().submit(action); + try { + return maybeResult.get(timeout, TimeUnit.MILLISECONDS); + } catch (Exception e) { + throw new RuntimeException("Failed to execute within time limit."); + } + } +} From 91c0c21d3f29c829eb201f82242aa787a2d579aa Mon Sep 17 00:00:00 2001 From: andrecs <12188364+andrecsilva@users.noreply.github.com> Date: Tue, 17 Dec 2024 09:01:16 -0300 Subject: [PATCH 2/2] Added test --- .../pixee/security/ExecuteWithTimeout.java | 2 +- .../security/ExecuteWithTimeoutTest.java | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/github/pixee/security/ExecuteWithTimeoutTest.java diff --git a/src/main/java/io/github/pixee/security/ExecuteWithTimeout.java b/src/main/java/io/github/pixee/security/ExecuteWithTimeout.java index dcf0f78..35643f0 100644 --- a/src/main/java/io/github/pixee/security/ExecuteWithTimeout.java +++ b/src/main/java/io/github/pixee/security/ExecuteWithTimeout.java @@ -14,7 +14,7 @@ public class ExecuteWithTimeout{ /** * Tries to execute a given {@link Callable} within a given timeout in milliseconds. Returns the result if successful, otherwise throws a {@link RuntimeException}. */ - public E executeWithTimeout(final Callable action, final int timeout) { + public static E executeWithTimeout(final Callable action, final int timeout) { Future maybeResult = Executors.newSingleThreadExecutor().submit(action); try { return maybeResult.get(timeout, TimeUnit.MILLISECONDS); diff --git a/src/test/java/io/github/pixee/security/ExecuteWithTimeoutTest.java b/src/test/java/io/github/pixee/security/ExecuteWithTimeoutTest.java new file mode 100644 index 0000000..46c46c3 --- /dev/null +++ b/src/test/java/io/github/pixee/security/ExecuteWithTimeoutTest.java @@ -0,0 +1,36 @@ +package io.github.pixee.security; + +import org.junit.jupiter.api.Test; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Stream; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertThrows; + +final class ExecuteWithTimeoutTest { + + @Test + void it_executes_within_timeout_and_returns(){ + Pattern pat = Pattern.compile("string"); + String input = "some very long string"; + Matcher matcher = pat.matcher(input); + boolean result = ExecuteWithTimeout.executeWithTimeout(() -> matcher.find(), 5000); + assertThat(result, is(equalTo(true))); + } + + @Test + void it_throws_exception_due_to_timeout(){ + Pattern pat = Pattern.compile("string"); + String input = "some very long string"; + Matcher matcher = pat.matcher(input); + RuntimeException exception = assertThrows( + RuntimeException.class, + () -> ExecuteWithTimeout.executeWithTimeout(() -> matcher.find(), 0) + ); + assertThat(exception.getMessage(), is(equalTo("Failed to execute within time limit."))); + } + +}