-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6256638
commit 91c0c21
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/java/io/github/pixee/security/ExecuteWithTimeoutTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."))); | ||
} | ||
|
||
} |