-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add jpa query timeout and tests for it (#244)
* add jpa query timeout and tests for it * add check exception message, unit for timeout, checkstyle --------- Co-authored-by: m.zharinova <[email protected]> Co-authored-by: Ivan Vakhrushev <[email protected]>
- Loading branch information
1 parent
e6f05e1
commit 5279eb8
Showing
3 changed files
with
48 additions
and
0 deletions.
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
37 changes: 37 additions & 0 deletions
37
src/test/java/com/mfvanek/salary/calc/repositories/JpaPropertiesTest.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,37 @@ | ||
package com.mfvanek.salary.calc.repositories; | ||
|
||
import com.mfvanek.salary.calc.support.TestBase; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.Query; | ||
import jakarta.persistence.QueryTimeoutException; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class JpaPropertiesTest extends TestBase { | ||
|
||
@Autowired | ||
EntityManager entityManager; | ||
|
||
@Test | ||
void failsWhenQueryTooLong() { | ||
final Query query = entityManager | ||
.createNativeQuery( | ||
"SELECT pg_sleep(5)"); | ||
assertThatThrownBy(query::getResultList) | ||
.isInstanceOf(QueryTimeoutException.class) | ||
.hasMessageContaining("ERROR: canceling statement due to user request"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Does not throw exception when query does not exceed timeout") | ||
void exceptionWithNotLongQuery() { | ||
final Query query = entityManager | ||
.createNativeQuery( | ||
"SELECT pg_sleep(3.9)"); | ||
assertThatNoException().isThrownBy(query::getResultList); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,10 @@ | ||
logging: | ||
level: | ||
org.springframework.orm.jpa.JpaTransactionManager: DEBUG | ||
spring: | ||
jpa: | ||
properties: | ||
jakarta: | ||
persistence: | ||
query: | ||
timeout: 4000 # milliseconds |