Repository navigation
Expand file tree
/
Copy pathSampleServiceTest.java
More file actions
69 lines (63 loc) · 2.68 KB
/
Copy pathSampleServiceTest.java
File metadata and controls
69 lines (63 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.transactiontimeoutsamplejdbc;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.QueryTimeoutException;
import org.springframework.test.context.jdbc.Sql;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Sql(
scripts = {"classpath:schema.sql", "classpath:data.sql"},
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD
)
public class SampleServiceTest {
@Autowired
SampleService sampleService;
@Nested
@DisplayName("registerWithSleep()")
class RegisterWithSleepTest {
// PostgreSQLの場合
@Test
@DisplayName("DataAccessResourceFailureException in 2 seconds on PostgreSQL")
void postgres() {
long startTime = System.currentTimeMillis();
DataAccessResourceFailureException exception = assertThrows(
DataAccessResourceFailureException.class, () -> {
sampleService.registerWithSleep(new Sample(4, "Sample4"), 3); // timeout = 2
}
);
long processSeconds = (System.currentTimeMillis() - startTime) / 1000;
assertEquals(2, processSeconds); // timeout should be 2 seconds
// PostgreSQL's code that means query_canceled
// see https://www.postgresql.org/docs/16/errcodes-appendix.html
SQLException sqlException = (SQLException) exception.getCause();
assertEquals("57014", (sqlException).getSQLState());
assertEquals("org.postgresql.util.PSQLException", sqlException.getClass().getName());
}
// MySQLの場合
@Disabled
@Test
@DisplayName("QueryTimeoutException in 2 seconds on MySQL")
void mysql() {
long startTime = System.currentTimeMillis();
QueryTimeoutException exception = assertThrows(
QueryTimeoutException.class, () -> {
sampleService.registerWithSleep(new Sample(4, "Sample4"), 3); // timeout = 2
}
);
long processSeconds = (System.currentTimeMillis() - startTime) / 1000;
assertEquals(2, processSeconds); // timeout should be 2 seconds
SQLTimeoutException sqlException = (SQLTimeoutException) exception.getCause();
assertEquals(0, sqlException.getErrorCode());
assertNull(sqlException.getSQLState());
}
}
}